Under construction

Still being built. Things may change

Pearl Pairing

Interview

Time limit1sMemory limit128 MB

Summary
Given counts of each pearl color, output the canonical pairing that matches the pearl at position i with the pearl at position i + N/2 in sorted order.
Level

Medium5 of 10

Topics
Greedy, Sorting, Implementation, Math
Solved
No attempts yet

Problem

Bessie received NN pearls for her birthday (2≤N≤100,0002 \le N \le 100{,}000, with NN always even). Each pearl is painted one of CC different colors (1≤C≤N1 \le C \le N); the colors are numbered 11 through CC, and color ii is used on exactly CiC_i pearls, so C1+C2+⋯+CC=NC_1 + C_2 + \cdots + C_C = N.

Because NN is even, Bessie wants to arrange all of the pearls into N/2N/2 pairs so that the two pearls in each pair have different colors. It is guaranteed that at least one such pairing exists for the given input.

Because many valid pairings may exist, you must output one specific canonical pairing, defined precisely in the output section below.

Input

  • Line 11: two space-separated integers NN and CC.
  • Lines 2…C+12 \dots C+1: line i+1i+1 contains a single integer CiC_i, the number of pearls of color ii.

Output

List the pearls in non-decreasing order of color: first all C1C_1 pearls of color 11, then all C2C_2 pearls of color 22, and so on through color CC. Number the pearls in this order from 11 to NN.

Form N/2N/2 pairs by matching the pearl in position ii with the pearl in position i+N/2i + N/2, for i=1,2,…,N/2i = 1, 2, \dots, N/2. Because a valid pairing is guaranteed to exist, the two pearls of every such pair always have different colors.

Print N/2N/2 lines. On line ii, print the two colors of the ii-th pair, the smaller color first, separated by a single space.

Hint

In the sample, color 33 is used on 44 pearls, and colors 11 and 22 are each used on 22 pearls. Under the canonical construction, each of the four color-33 pearls is paired with a pearl of color 11 or color 22.

Examples5

  1. Example 1

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

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

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

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

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