atcoder#ARC141C. [ARC141C] Bracket and Permutation

[ARC141C] Bracket and Permutation

Score : 600600 points

Problem Statement

A string of length 2N2N, S=S1S2S2NS=S_1S_2\dots S_{2N}, is said to be a parenthesis sequence when SS is composed of NN (s and NN )s.

Additionally, a parenthesis sequence SS is said to be correct when it is one of the following.

  • An empty string.
  • The concatenation of (, AA, ) in this order, where AA is a correct parenthesis sequence.
  • The concatenation of AA, BB in this order, where AA and BB are non-empty correct parenthesis sequences.

You are given two permutations P=(P1, P2, , P2N)P=(P_1,\ P_2,\ \dots,\ P_{2N}) and Q=(Q1, Q2, , Q2N)Q=(Q_1,\ Q_2,\ \dots,\ Q_{2N}) of the integers from 11 to 2N2N.

Determine whether there exists a parenthesis sequence S=S1S2S2NS=S_1S_2\dots S_{2N} that satisfies the following conditions.

  • PP is the lexicographically smallest permutation pp of the integers from 11 to 2N2N such that Sp1Sp2Sp2NS_{p_1}S_{p_2}\dots S_{p_{2N}} is a correct parenthesis sequence.
  • QQ is the lexicographically largest permutation pp of the integers from 11 to 2N2N such that Sp1Sp2Sp2NS_{p_1}S_{p_2}\dots S_{p_{2N}} is a correct parenthesis sequence.

If such a parenthesis sequence exists, find one.

Constraints

  • 1N2×1051 \leq N \leq 2 \times 10^5
  • 1Pi,Qi2N1 \leq P_i,Q_i \leq 2N
  • Each of PP and QQ is a permutation of 11 to 2N2N.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

P1P_1 P2P_2 \dots P2NP_{2N}

Q1Q_1 Q2Q_2 \dots Q2NQ_{2N}

Output

If there exists a parenthesis sequence SS 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 S=S= ())(, some of the permutations pp such that Sp1Sp2Sp3Sp4S_{p_1}S_{p_2}S_{p_3}S_{p_4} is a correct parenthesis sequence are p=(1, 4, 2, 3), (1, 4, 3, 2)p=(1,\ 4,\ 2,\ 3),\ (1,\ 4,\ 3,\ 2). The lexicographically smallest and largest among them are p=(1, 2, 4, 3)p=(1,\ 2,\ 4,\ 3) and p=(4, 3, 1, 2)p=(4,\ 3,\ 1,\ 2), satisfying the conditions.

2
1 3 2 4
4 3 2 1
-1

No SS satisfies the conditions.

3
2 1 5 3 4 6
6 5 3 4 2 1
-1