{ tennis2.pas }
{ Solution for the TENNIS problem of BOI 2002 }
{ Ahto Truu }
{ Tested with FPC 1.0.4/GO32V2 }

{ This is the naive inefficient implementation, for comparison only }

const
   iname = 'TENNIS.IN'; { input file name }
   oname = 'TENNIS.OUT'; { output file name }
   pos = 'SOLUTION'; { "positive" answer }
   neg = 'NO SOLUTION'; { "negative" answer }
   maxn = 1000; { max allowed number of players }

type
   player = record { this is the data we keep on each player }
      num : integer; { sequence number of this player }
      gam : integer; { number of games still available }
   end;

{ exchanges two players' data }
procedure swap(var a, b : player);
var c : player;
begin
   c := a; a := b; b := c;
end;

var
{ global data }
   n : integer; { number of players }
   p : array [1..maxn] of player; { player data }
   g : array [1..maxn, 1..maxn] of boolean; { "game matrix" }
                         { g[i, j] iff players i and j play }
   ok : boolean; { do we have a solution? }
{ temporary variables }
   f : text;
   i, j, k : integer;
   s : boolean;

begin
   { load the input data }
   assign(f, iname); reset(f);
   readln(f, n);
   for i := 1 to n do
      readln(f, p[i].gam);
   close(f);

   { assign the sequence numbers to players }
   for i := 1 to n do
      p[i].num := i;

   { reset the matrix }
   for i := 1 to n do
      for j := 1 to n do
         g[i, j] := false;

   { fill the matrix in a greedy way }
   { on each step, the player with highest number of available games }
   { is set to play with the players with the second, third, etc }
   ok := true; { assume we'll find a solution }
   while true do begin
      { sort the players in descending order of number of games }
      for i := 1 to n do begin
         k := i;
         for j := i + 1 to n do
            if p[k].gam < p[j].gam then
               k := j;
         if k > i then
            swap(p[i], p[k]);
      end;
      { no more games to match, we're done }
      if p[1].gam = 0 then
         break;
      { match the first player with the following ones }
      i := 2;
      while (p[1].gam > 0) and (p[i].gam > 0) do begin
         g[p[1].num][p[i].num] := true;
         g[p[i].num][p[1].num] := true;
         p[1].gam := p[1].gam - 1;
         p[i].gam := p[i].gam - 1;
         i := i + 1;
      end;
      { some games were left for first player, we have a problem }
      if p[1].gam > 0 then begin
         ok := false;
         break;
      end;
   end;

   { output the answer }
   assign(f, oname); rewrite(f);
   if ok then begin
      writeln(f, pos);
      for i := 1 to n do begin
         s := false;
         for j := 1 to n do
            if g[i, j] then begin
               if s then
                  write(f, ' ');
               write(f, j);
               s := true;
            end;
         writeln(f);
      end;
   end else
      writeln(f, neg);
   close(f);
end.
