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

Building Bracket Strings

Back to problem

Editorial

Let SiS_iSi​ be the balance of the first iii characters. An interval [l,r][l,r][l,r] is regular exactly when

Sl−1=Sr,Sj≥Sl−1(l≤j≤r).S_{l-1}=S_r,\qquad S_j\geq S_{l-1}\quad(l\leq j\leq r).Sl−1​=Sr​,S

Treat boundaries 0,…,N0,\ldots,N0,…,N as vertices and unite l−1,rl-1,rl−1,r for every required interval using a DSU.

Also merge components whose boundaries alternate as x1<y1<x2<y2x_1<y_1<x_2<y_2x1​<y1​<x2​. To do so, scan boundaries left to right, keeping active components on a stack. Whenever a component reappears, merge all components above it into it. Remove a component upon reaching its last boundary.

Consecutive boundaries of the resulting components define intervals that are disjoint or nested. Remove each interval's immediate children and concatenate its remaining positions in order. Make every residual group regular independently. At positions outside all intervals, take max⁡(ai,bi)\max(a_i,b_i)max(ai​,bi​).

To optimize a group of length 2k2k2k, initially use only closing brackets, earning ∑bi\sum b_i∑bi​. Changing position iii to an opening bracket gains ai−bia_i-b_ia. Scan the group, inserting these gains into a max-heap. At each odd position, extract the largest gain and select that position as an opening bracket.

The groups contain at most NNN positions altogether. Total time is O((N+M)α(N)+Nlog⁡N)O((N+M)\alpha(N)+N\log N)O((N+M)α(N)+NlogN) and space is O(N+M)O(N+M), where is the inverse Ackermann function.

Proof

Boundaries in one DSU component have equal heights. Its original intervals cover the component's span without dropping below this common height, so substrings between consecutive boundaries must be regular. If two components cross, their prefix conditions force each component's height to be at least the other's. Their heights are equal, so merging them preserves the constraints.

After resolving all crossings, intervals are disjoint or nested. Inserting or deleting a regular substring preserves the outer string's regularity. Thus, the original constraints are equivalent to requiring every residual group to be regular independently.

Within a group, the first 2j−12j-12j−1 positions need at least jjj openings. An optimum containing the previous greedy choices must select an additional position in this prefix. Exchange that selection for the largest available gain. Earlier prefix requirements are already satisfied by previous choices; later prefixes contain both exchanged positions. Feasibility is preserved and reward cannot decrease, proving optimality.

j​
≥
Sl−1​(l≤
j≤
r).
<
y2​
i
​
−
bi​
O(N+M)
α\alphaα