This page is still under construction.

Parts of this page are still being built. What you see may change.

Dr Who's Banquet

Time limit1sMemory limit256 MB

Summary
Build a chat graph whose vertex degrees equal the given wishes with the stated greedy construction, or print fail.
Level

Medium4 of 10

Topics
Graph, Greedy, Sorting
Solved
No attempts yet

Problem

Dr Who is holding a banquet and invites several guests. Guest ii is happy when he chats with exactly aia_i of the other guests. A guest never chats with himself, and any two guests either chat with each other or they do not. Pair the guests so that every guest is happy, or report that no such arrangement exists.

Input

The input holds several data sets, one data set per line. A line holds the integers a1,a2,…,ana_1, a_2, \dots, a_n separated by single spaces, where aia_i is the number of chat partners guest ii wants. The guests on a line are numbered 11 to nn in the order they appear. 1≤n≤100001 \le n \le 10000 and 1≤ai≤10001 \le a_i \le 1000. The input ends at the end of the file.

Output

Print one block for each data set. If every guest can be made happy, print the n×nn \times n matrix mm, where m[i][j]=m[j][i]=1m[i][j] = m[j][i] = 1 when guests ii and jj chat and 00 otherwise. Print each row on its own line and separate the values of a row by single spaces. If no arrangement makes every guest happy, print fail. Print one empty line after each block.

Several matrices can satisfy the same wish list, so only the matrix produced by the construction below counts as correct.

Give each guest a counter rir_i set to aia_i and put every guest in the pool. While some guest in the pool has ri>0r_i > 0, repeat this. Take the guest vv of the pool with the largest rvr_v, and among ties the one with the smaller number. Order the remaining guests of the pool by rr from large to small, breaking ties by the smaller number, and pair vv with the first rvr_v of them. Subtract 11 from the counter of each new partner, set rvr_v to 00, and remove vv from the pool. The matrix is complete once every guest left in the pool has ri=0r_i = 0. This construction pairs everybody whenever a valid arrangement exists.

Examples3

  1. Example 1

    Input
    3 3 1 1
    4 4 3 3 2 2 2
    3 3 1 1
    2 2 2 2
    
    Expected output
    fail
    
    0 1 1 1 1 0 0
    1 0 1 1 0 1 0
    1 1 0 0 0 0 1
    1 1 0 0 0 0 1
    1 0 0 0 0 1 0
    0 1 0 0 1 0 0
    0 0 1 1 0 0 0
    
    fail
    
    0 1 1 0
    1 0 0 1
    1 0 0 1
    0 1 1 0
  2. Example 2

    Input
    1 1
    
    Expected output
    0 1
    1 0
  3. Example 3

    Input
    1
    
    Expected output
    fail