Prefix Medians

Time limit1sMemory limit128 MB

Summary
Given the prefix medians B of an unknown permutation of 1 to 2N-1, reconstruct the lexicographically smallest permutation that produces exactly those medians.
Level

Hard8 of 10

Topics
Greedy, Implementation, Sorting, Brute force
Solved
No attempts yet

Problem

Let AA be a permutation of 1,2,3,…,2N−11, 2, 3, \ldots, 2N-1.

Define the prefix medians of AA as an array BB with NN elements, where BiB_i is the median of the first 2i−12i-1 elements A1,A2,…,A2i−1A_1, A_2, \ldots, A_{2i-1}.

The median of a list of MM numbers, where MM is odd, is the middle value after the list is sorted.

You are given NN and the array BB. Reconstruct a permutation AA whose prefix medians are exactly BB.

Input

The first line contains one integer NN.

The second line contains NN integers B1,B2,…,BNB_1, B_2, \ldots, B_N separated by spaces.

Output

Print AA as a single line of 2N−12N-1 integers separated by spaces.

Several permutations may produce the same array BB; among them, print the lexicographically smallest one. It is guaranteed that at least one valid permutation exists.

Constraints

  • 1≤N≤100 0001 \le N \le 100\,000
  • 1≤Bi≤2N−11 \le B_i \le 2N-1 for each ii (1≤i≤N1 \le i \le N)
  • A permutation AA with prefix medians BB is guaranteed to exist.

Examples5

  1. Example 1

    Input
    5
    1 3 3 4 5
    
    Expected output
    1 3 4 2 5 6 7 8 9
    
  2. Example 2

    Input
    1
    1
    
    Expected output
    1
    
  3. Example 3

    Input
    4
    1 2 3 4
    
    Expected output
    1 2 3 4 5 6 7
    
  4. Example 4

    Input
    2
    3 2
    
    Expected output
    3 1 2
    
  5. Example 5

    Input
    3
    5 3 3
    
    Expected output
    5 1 3 2 4