You are given a permutation of . Now you need to construct a valid bracket sequence of length such that the following holds: if we create an undirected graph with vertices numbered , and add an edge from to if and only if the -th (with index starting from 1) character of the bracket sequence is a (
, the resulting graph satisfies the condition that all vertices have degree .
It is guaranteed that for all in the permutation, . Furthermore, all test cases are guaranteed to have a valid bracket sequence satisfying the above constraint.
Here are the formal definitions of "permutation" and "valid bracket sequence".
- Permutation: A sequence of length is a permutation of if and only if for each , appears exactly once in .
- Valid bracket sequence: A valid bracket sequence can be defined using the three rules: (1) an empty sequence is a valid bracket sequence; (2) if
A
is a valid bracket sequence, then(A)
is a valid bracket sequence; (3) ifA
andB
are both valid bracket sequences, thenAB
is a valid bracket sequence.
An example of a valid bracket sequence is (())()
, and an example of an invalid bracket sequence is ())
since the last )
does not have a matching (
.
Input Specification
The first line of the input is an integer denoting the length of the permutation.
In the following line, there are positive integers, separated by single spaces, denoting the permutation .
Output Specification
Output a line with a valid bracket sequence of length denoting a solution satisfying the above constraints.
If there are multiple solutions, you may output any.
Note: the sample output is just a reference solution. The solution might be not unique.
Sample Input
4
2 3 4 1
Sample Output
()()
Explanation
Notice there are two left brackets at position and . So we will add edge and edge to the graph. All vertices now have degree .
Constraints
For all test cases, is a permuation of .
- Subtask 1 (30 points): .
- Subtask 2 (30 points): . Depends on subtask 1.
- Subtask 3 (40 points): . Depends on subtask 1 and 2.
Update: For those experiencing presentation errors: it is likely that your output contains invalid characters, or you don't have output at all.
Comments