Bracket Sequence With Depth Pattern
You are given a sequence of n target nesting depths. Construct a valid parentheses string where the nesting depth at each position matches the target depths, or report that it is impossible.
The nesting depth at position i is defined as the number of unclosed opening brackets '(' after reading character i.
A valid parentheses string is one where:
- Every opening bracket '(' has a matching closing bracket ')'
- At no point while reading left-to-right do we have more ')' than '(' seen so far
Input
The first line contains n (1 ≤ n ≤ 10^5), the number of positions.
The second line contains n integers d_1, d_2, ..., d_n (0 ≤ d_i ≤ 10^5), the target nesting depths at each position.
Output
If it is possible to construct a valid bracket string matching the target depths, print the string. (The depths determine it: each character must be ( if the depth rises by one there and ) if it falls by one, so there is at most one such string.)
Otherwise, print -1.
Notes
- The output string must have exactly
ncharacters. - If no valid string exists, print
-1.