cloned from srcbox.
[furry-nemesis.git] / river.pas
blob765b3aa5dc85ee72448902318c036b2ace128424
1 var
2 n,i,time,j:longint;
3 last,now:array [-1001..1001] of boolean;
4 a,b:array [0..1001] of longint;
5 f:boolean;
7 function up(i,time:longint):boolean;
8 begin
9 time:=time mod (a[i]+b[i]);
10 if (time>=1) and (time<=a[i]) then exit(true) else exit(false);
11 end;
13 begin
14 readln(n);
15 for i:=1 to n do readln(a[i],b[i]);
16 last[0]:=true;
17 repeat
18 f:=false;
19 inc(time);
20 if last[n+1] then begin writeln(time-1); close(input); close(output); halt; end;
21 fillchar(now,sizeof(now),false);
22 for i:=0 to n+1 do
23 if (i=0) or (i=n+1) or (up(i,time)) then
24 for j:=i-5 to i+5 do
25 if (last[j]) and (j>=0) and (j<=n+1) then
26 begin
27 f:=true;
28 now[i]:=true;
29 break;
30 end;
31 last:=now;
32 until (time>2520) or (not f);
33 if not f then writeln('NO');
34 end.