uses testlib;
const nmax=2000;
var n:integer;
    a,b:array[1..nmax] of integer;
    ua,ub,uc:integer;
    i:integer;
    s:string;
//    o:integer;
    k,l:integer;
begin
n:=inf.readinteger;
for i:=1 to n do
  a[i]:=inf.readinteger;

s:=ans.readstring;
if s='NO' then if ouf.readinteger<>0 then quit(_WA,'0 expected')
               else quit(_OK,'');
if s<>'YES' then quit(_fail,'NO or YES in ans-file expected');
ua:=0;
ub:=0;
uc:=0;
while not ouf.seekeof do begin
  k:=ouf.readinteger;
  if (k=0) and (ua=0) then quit(_WA,'0 when answer is YES');
  l:=ouf.readinteger;
  if (k<>1) and (k<>2) then quit(_PE,'1 or 2 expected');
  if l<1 then quit(_PE,'Wrong count');
  if k=1 then begin
     if n-ua<l then quit(_WA,'Not enough cars on track 1');
     for i:=1 to l do begin
       ua:=ua+1;
       ub:=ub+1;
       b[ub]:=a[ua];
       end;
     end;
  if k=2 then begin
     if ub<l then quit(_WA,'Not enough cars on dead-end siding');
     for i:=1 to l do begin
       if b[ub]<>uc+1 then quit(_WA,'Wrong order of cars on track 2');
       ub:=ub-1;
       uc:=uc+1;
       end;
     end;
  end;
if uc=n then quit(_OK,'');
quit(_WA,'Wrong end position');
end.
