DOJ logoDOJ
Problems
Recently added problemsSaved problems
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

Dropping Monkeys

Back to problem

Editorial

Let BBB be the player who moves when the number of remaining floors is odd, and AAA the player who moves when it is even.

We can compute BBB's final score using the following greedy algorithm.

The aHa_HaH​ monkeys on the topmost floor are guaranteed to be scored by BBB. Process the remaining floors in pairs from top to bottom. Insert the monkey counts of the two newly considered floors into a max-priority queue, then extract the maximum and add it to BBB's score. Leave all unselected values in the queue. If HHH is even, the bottommost floor a1a_1a1​ is ignored.

If HHH is odd, BBB is Ishigami; otherwise, BBB is Iino. Since every monkey is eventually scored by exactly one player, AAA's score is the total number of monkeys minus BBB's score.

The time complexity is O(Hlog⁡H)O(H\log H)O(HlogH), and the space complexity is O(H)O(H)O(H).

Proof

Consider the floors in bottom-to-top order. Represent a floor assigned to AAA by ( and a floor assigned to BBB by ).

For the first kkk parentheses, define

h(k)=#(−#).h(k)=\#\texttt{(}-\#\texttt{)}.h(k)=

Solution written by GPT-6

#
(
−
#).

When the number of remaining floors is even, it is AAA's turn, and we consider a balanced parenthesis sequence satisfying

h(k)≥0,h(H)=0.h(k)\ge 0,\qquad h(H)=0.h(k)≥0,h(H)=0.

When the number of remaining floors is odd, it is BBB's turn, and we consider a sequence satisfying

h(k)≥−1,h(H)=−1.h(k)\ge -1,\qquad h(H)=-1.h(k)≥−1,h(H)=−1.

In both cases, the inequality must hold for every prefix. We call such sequences valid.

First, we show that BBB can guarantee the total weight assigned to ) in any valid sequence.

On BBB's turn, the current sequence satisfies h(k)≥−1h(k)\ge -1h(k)≥−1 and h(H)=−1h(H)=-1h(H)=−1.

Let ttt be the first position where the height reaches −1-1−1.

  • If t=1t=1t=1, the first character is ). BBB removes the bottommost floor and scores its monkeys.
  • If t>1t>1t>1, the characters at positions t−1t-1t−1 and ttt must be )). BBB removes the floor at position ttt, merging it into the floor immediately below, and marks the merged floor with ).

In either case, the resulting sequence is balanced, and no weight assigned to ) is lost. In the second case, all preceding prefix heights remain nonnegative, while all subsequent heights increase by one.

Now consider AAA's turn, when the current sequence is balanced.

We distinguish four possible moves by AAA.

  • Removing the bottommost floor: The first character must be (. Removing it produces a sequence satisfying h(k)≥−1h(k)\ge -1h(k)≥−1 and h(H)=−1h(H)=-1h(H)=−1, so BBB can apply the previous strategy.
  • Merging ((: Mark the merged floor with (. The resulting sequence satisfies the same height conditions, and the weight assigned to ) remains unchanged.
  • Merging () or )(: Mark the merged floor with ). The resulting sequence again satisfies the same height conditions. Moreover, some weight previously assigned to ( is now assigned to ), so the assigned score does not decrease.
  • Merging )): Mark the merged floor with ). We show that BBB can restore a balanced sequence by merging a suitable pair of consecutive opening parentheses ((.

For the last case, consider the substring beginning at the opening parenthesis matched with the second ) of the merged pair. This substring has the form

((X)Y),\texttt{((}X\texttt{)}Y\texttt{)},((X)Y),

where XXX and YYY are balanced parenthesis sequences, possibly empty.

After AAA merges the final )) and BBB merges the initial ((, this substring becomes

(X)Y,\texttt{(}X\texttt{)}Y,(X)Y,

which is again balanced. Since both floors merged by BBB were assigned to (, the weight assigned to ) does not decrease.

Thus, regardless of AAA's move, BBB can preserve a valid sequence without decreasing the assigned score. Repeating this strategy guarantees that BBB eventually obtains at least the total weight initially assigned to ).

Conversely, AAA can prevent BBB from exceeding the maximum weight assigned to ) over all valid sequences.

Suppose AAA always removes the bottommost remaining floor. Mark each original floor according to the player who removes it.

Among the bottommost kkk original floors, the number removed by BBB cannot exceed the number removed by AAA, except that BBB may have one extra removal if BBB moves first. Indeed, whenever BBB removes a floor in this prefix, the preceding move by AAA must also have removed a floor in this prefix, unless BBB moved first.

Therefore, the resulting parenthesis sequence is valid.

Moreover, every monkey initially on a floor removed by AAA is scored by AAA. Hence, BBB's actual score cannot exceed the total weight of floors marked with ).

Combining both directions, the optimal score of BBB equals the maximum total weight assigned to ) over all valid parenthesis sequences.

It remains to maximize this weight.

In any valid sequence, the total number of closing parentheses is

⌈H2⌉.\left\lceil\frac{H}{2}\right\rceil.⌈2H​⌉.

The height conditions are equivalent to requiring that, among the topmost kkk floors, at least

⌈k2⌉\left\lceil\frac{k}{2}\right\rceil⌈2k​⌉

floors are marked with ). Together with the total count, these conditions completely characterize valid sequences.

In particular, we must select at least one floor among the topmost 111, at least two among the topmost 333, at least three among the topmost 555, and so on.

Therefore, we first select the topmost floor, then select one additional floor whenever two more floors are considered.

The optimal choice at each step is the largest value among all considered but unselected floors.

To prove this, consider an optimal solution that agrees with the greedy algorithm on all previous selections. Let xxx be the current greedy choice.

If xxx is not selected in this optimal solution, some other value yyy among the floors considered so far must be selected instead, excluding the previously fixed selections.

By the greedy rule, x≥yx\ge yx≥y. Replacing yyy with xxx cannot decrease the score. It also preserves feasibility: all earlier requirements are already satisfied by the fixed selections, while every later requirement contains both positions and therefore retains the same number of selected floors.

Thus, an optimal solution agreeing with the greedy choice always exists. Repeating this exchange proves that the greedy algorithm is optimal.

A max-priority queue implements these choices efficiently.