Restoring a Permutation

Time limit2sMemory limit512 MB

Summary
Given arrays a and b, construct a permutation p where a[i] is the LIS ending at i and b[i] is the longest decreasing subsequence starting at i.
Level

Hard8 of 10

Topics
Greedy, Sorting, Array, Combinatorics
Solved
No attempts yet

Problem

You are given a positive integer nn and two arrays aa and bb containing nn integers each.

You need to find a permutation pp of length nn such that for each i∈{1,2,…,n}i \in \{1, 2, \ldots, n\} the following two conditions are satisfied:

  • the length of the longest increasing subsequence of pp ending at position ii is equal to aia_i,
  • the length of the longest decreasing subsequence of pp starting at position ii is equal to bib_i.

Input

The first line of input contains a positive integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5), the length of the permutation.

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n, where aia_i is the length of the longest increasing subsequence ending at position ii (1≤ai≤n1 \le a_i \le n).

The third line contains nn integers b1,b2,…,bnb_1, b_2, \ldots, b_n, where bib_i is the length of the longest decreasing subsequence starting at position ii (1≤bi≤n1 \le b_i \le n).

Output

Print a line containing nn space-separated integers p1,p2,…,pnp_1, p_2, \ldots, p_n: the desired permutation.

It is guaranteed that the answer exists. If there are multiple solutions, you may print any one of them.

Examples2

  1. Example 1

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

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