CF BUDDY
← Problems·

1906E · Merge Not Sort

1900 · constructive algorithms, dp

Problem: You are currently researching the Merge Sort algorithm. Merge Sort is a sorting algorithm that is based on the principle of Divide and Conquer. It works by dividing an array into two subarrays of equal length, sorting each subarrays, then merging the sorted subarrays back together to form the final sorted array.

You are particularly interested in the merging routine. Common merge implementation will combine two subarrays by iteratively comparing their first elements, and move the smaller one to a new merged array. More precisely, the merge algorithm can be presented by the following pseudocode.

During your research, you are keen to understand the behaviour of the merge algorithm when arrays AA and BB are not necessarily sorted. For example, if A=[3,1,6]A = [3, 1, 6] and B=[4,5,2]B = [4, 5, 2], then Merge(A,B)=[3,1,4,5,2,6]\text{Merge}(A, B) = [3, 1, 4, 5, 2, 6].

To further increase the understanding of the merge algorithm, you decided to work on the following problem. You are given an array CC of length 2N2 \cdot N such that it is a permutation of 11 to 2N2 \cdot N. Construct any two arrays AA and BB of the same length NN, such that Merge(A,B)=C\text{Merge}(A, B) = C, or determine if it is impossible to do so.

Input Format: The first line consists of an integer NN (1N10001 \leq N \leq 1000).

The following line consists of 2N2 \cdot N integers CiC_i. The array CC is a permutation of 11 to 2N2 \cdot N.

Output Format: If it is impossible to construct two arrays AA and BB of length NN such that Merge(A,B)=C\text{Merge}(A, B) = C, then output -1.

Otherwise, output the arrays AA and BB in two lines. The first line consists of NN integers AiA_i. The second line consists of NN integers BiB_i. If there are several possible answers, output any of them.

Note: Explanation for the sample input/output #1

The solution A=[3,1,4]A = [3, 1, 4] and B=[5,2,6]B = [5, 2, 6] is also correct.

Explanation for the sample input/output #2

The solution A=[1,2,3,4]A = [1, 2, 3, 4] and B=[5,6,7,8]B = [5, 6, 7, 8] is also correct.

Sample Cases

Case 1

Input

3
3 1 4 5 2 6

Output

3 1 6
4 5 2

Case 2

Input

4
1 2 3 4 5 6 7 8

Output

2 3 5 7
1 4 6 8

Case 3

Input

2
4 3 2 1

Output

-1

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback