Under construction

Still being built. Things may change

Berland Railroads

Time limit1sMemory limit512 MB

Summary
Given the required railroad count d_i for each of n cities, build a connected tree with exactly those degrees that minimizes the maximum number of train changes.
Level

Medium6 of 10

Topics
Greedy, Tree, Sorting
Solved
No attempts yet

Problem

The Berland government has decided to build a system of high speed railroads in the country. There are nn cities in Berland, numbered from 11 to nn. Some pairs of cities will be connected by a high speed railroad. Travelers can move by train in both directions along each railroad.

High speed trains are the future of transport, so the plan must allow travel from any city to any other city by rail. To save money, the government builds the fewest railroads that satisfy this condition.

The government of city ii wants to support exactly did_i trains. That is, exactly did_i railroads must connect city ii to other cities. It is known that d1+d2+…+dn=2n−2d_1 + d_2 + \ldots + d_n = 2n - 2.

The government wants people to change trains as few times as possible. Build the plan so that the maximum number of changes needed to travel from one city to another is as small as possible.

Input

The first line contains an integer nn, the number of cities (2≤n≤2000002 \leq n \leq 200000). The next line contains nn integers d1,d2,…,dnd_1, d_2, \ldots, d_n (1≤di<n1 \leq d_i < n). City ii must be connected to exactly did_i other cities. It is guaranteed that d1+d2+…+dn=2n−2d_1 + d_2 + \ldots + d_n = 2n - 2.

Output

Print n−1n-1 lines describing the railroads in the optimal plan. Each line contains two integers sis_i and fif_i, the cities to connect (1≤si,fi≤n1 \leq s_i, f_i \leq n, si≠fis_i \neq f_i). The number of railroads at city ii must equal did_i for every ii. The maximum number of changes needed to travel between any two cities must be as small as possible. If several plans are optimal, print any of them. It is guaranteed that at least one valid plan exists.

Hint

The optimal answer for the first sample is shown below.

City 1 is connected to three cities: 2, 4, and 6 (d1=3d_1 = 3). The maximum number of changes is 3, for cities 3 and 5. Travel from city 3 to city 2, then from city 2 to city 1, then from city 1 to city 4, and finally from city 4 to city 5. No plan requires fewer changes for every pair of cities.

Examples2

  1. Example 1

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

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