atcoder#ARC141C. [ARC141C] Bracket and Permutation
[ARC141C] Bracket and Permutation
Score : points
Problem Statement
A string of length , , is said to be a parenthesis sequence when is composed of (
s and )
s.
Additionally, a parenthesis sequence is said to be correct when it is one of the following.
- An empty string.
- The concatenation of
(
, ,)
in this order, where is a correct parenthesis sequence. - The concatenation of , in this order, where and are non-empty correct parenthesis sequences.
You are given two permutations and of the integers from to .
Determine whether there exists a parenthesis sequence that satisfies the following conditions.
- is the lexicographically smallest permutation of the integers from to such that is a correct parenthesis sequence.
- is the lexicographically largest permutation of the integers from to such that is a correct parenthesis sequence.
If such a parenthesis sequence exists, find one.
Constraints
- Each of and is a permutation of to .
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
If there exists a parenthesis sequence that satisfies the conditions above, print one such sequence. If there are multiple such sequences, you may print any of them.
If there is no such sequence, print -1
.
2
1 2 4 3
4 3 1 2
())(
For ())(
, some of the permutations such that is a correct parenthesis sequence are . The lexicographically smallest and largest among them are and , satisfying the conditions.
2
1 3 2 4
4 3 2 1
-1
No satisfies the conditions.
3
2 1 5 3 4 6
6 5 3 4 2 1
-1