{$A+,B-,D+,E+,F-,G-,I+,L+,N-,O-,P-,Q-,R+,S+,T-,V+,X+,Y+}

{$M 16384,0,655360}

unit sticks;

interface

procedure putMove(nr,label1,label2:integer);

procedure getMove(var nr,label1,label2:integer);



implementation

const vturn:integer=2;

var vx,vy,vi,vj,vk,vl,vm,vn:longint;

    vfi:text;

    vst:array[1..100]of integer;

    vpp:array[1..100,0..11]of integer;

    vxxx,vyyy,vzzz:integer;



procedure readdata;

begin

  assign(vfi,'sticks.in');

  reset(vfi);

  readln(vfi,vn);

  for vi:=1 to vn do

      read(vfi,vst[vi]);

  readln(vfi);

  readln(vfi,vturn);

  close(vfi);

  for vi:=1 to vn do

      for vj:=1 to vst[vi] do

          vpp[vi,vj]:=1;

end;



function empty:boolean;

begin

  empty:=false;

  for vi:=1 to vn do

      for vj:=1 to vst[vi] do

          if vpp[vi,vj]=1 then exit;

  empty:=true;

end;



procedure eroare(s:string);

begin

  writeln(s);

  halt;

end;



procedure putMove(nr,label1,label2:integer);

var vmm,vnn:integer;

begin

  if vturn=2 then

     begin

       readdata;

       if vturn=1 then

          eroare('Wrong player started the game');

     end;

  if vturn=1 then

     eroare('The other player''s turn');

  vturn:=1-vturn;

  if (nr<1)or(nr>vn) then

     eroare('Invalid move');

  if (label1>label2)or(label2<1)or(label1>vst[nr]) then

     eroare('Invalid move');

  if label2-label1>2 then

     eroare('Invalid move');

  for vi:=label1 to label2 do

      if vpp[nr,vi]=0 then

         eroare('Invalid move');

  for vi:=label1 to label2 do

      vpp[nr,vi]:=0;

  vnn:=label2-label1+1;

  vm:=0;

  vi:=label1;

  while vpp[nr,vi-1]=1 do

    begin

      dec(vi);

      inc(vm);

      inc(vnn);

    end;

  vm:=0;

  vi:=label2;

  while vpp[nr,vi+1]=1 do

    begin

      inc(vi);

      inc(vm);

      inc(vnn);

    end;

  if empty then

     begin

       halt;

     end;

end;



procedure getMove(var nr,label1,label2:integer);

var vm:integer;

begin

  if vturn=2 then

     begin

       readdata;

       if vturn=0 then

          eroare('Wrong player started the game');

     end;

  if vturn=0 then

     eroare('The other player''s turn');

  vturn:=1-vturn;

  for vk:=1 to 100 do

      begin

        vi:=random(vn)+1;

        vj:=random(vst[vi])+1;

        if vpp[vi,vj]=1 then

           begin

             vm:=0;

             vk:=0;

             while vpp[vi,vj+vk]=1 do

               begin

                 inc(vk);

                 inc(vm);

               end;

             vk:=0;

             while vpp[vi,vj-vk]=1 do

               begin

                 inc(vk);

                 inc(vm);

               end;

             vk:=vm;

             dec(vk);

             vpp[vi,vj]:=0;

             nr:=vi;

             label1:=vj;

             label2:=vj;

             exit;

           end;

      end;

  for vi:=1 to vn do

      for vj:=1 to vst[vi] do

          if vpp[vi,vj]=1 then

             begin

               vk:=0;

               while vpp[vi,vj+vk]=1 do inc(vk);

               vpp[vi,vj]:=0;

               nr:=vi;

               label1:=vj;

               label2:=vj;

               exit;

             end;

end;



end.