problems / 183

Bracket Sequence With Depth Pattern

easy bracket-sequences constructive arrays 1000 ms · 256 MB

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 n characters.
  • If no valid string exists, print -1.

Example 1

Input

4
1 2 1 0

Output

(())

Example 2

Input

2
1 0

Output

()

Hints · reveal one at a time if you're stuck

Hint 1

Before the first character the depth is 0. What must the depth be after the first character, and what does each step up or down say about the bracket at that position?

Hint 2

Compare each target depth with the one before it: a rise forces '(' and a fall forces ')'. Then check that the whole sequence is consistent, including its start, its end, and the size of every step.

report an issue Prefer your own editor? sooks prep 183 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.