Under construction

Still being built. Things may change

Restore a Binary Search Tree

Interview

Time limit2sMemory limit1024 MB

Summary
Given each node's value and depth, rebuild a binary search tree from them, or report -1 when no such tree exists.
Level

Medium6 of 10

Topics
Sorting, Divide and conquer, Tree
Solved
No attempts yet

Problem

Chansol built a binary search tree in which each of the NN nodes holds an integer. In a binary search tree, for every node ii, every number in the left subtree of ii is less than the number in ii, and every number in the right subtree of ii is greater than the number in ii.

For every ii, Chansol recorded the depth HiH_i of node ii and the integer AiA_i written on node ii. All AiA_i are distinct, and the root has depth 1. (1≤i≤N1 \le i \le N)

Chansol lost the tree while attending the ICPC World Finals. Restore the binary search tree from the recorded information.

Input

The first line contains the number of nodes NN. (1≤N≤2000001 \le N \le 200000)

The next NN lines describe the nodes. For each 1≤i≤N1 \le i \le N, line (i+1)(i+1) contains the integer AiA_i written on node ii and the depth HiH_i of node ii, separated by a space. (−2⋅109≤Ai≤2⋅109-2 \cdot 10^9 \le A_i \le 2 \cdot 10^9, 1≤Hi≤N1 \le H_i \le N) All AiA_i are distinct.

Output

If the recorded information cannot be restored to a binary search tree, print -1.

Otherwise, print the structure of the tree over NN lines. Line ii contains the node numbers of the left child and the right child of node ii. If a child does not exist, print -1 in its place.

If several trees can be restored, print any of them.

Examples4

  1. Example 1

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

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

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

    Input
    3
    100 2
    200 1
    300 2
    
    Expected output
    -1 -1
    1 3
    -1 -1