Cod sursa(job #301641)

Utilizator costyv87Vlad Costin costyv87 Data 8 aprilie 2009 12:38:55
Problema Transport Scor 0
Compilator fpc Status done
Runda all.you.can.happily.code Marime 1.04 kb
var v:array[1..15100] of longint;
    i,x,k,n,s:longint;
    f,g:text; ok:boolean;
procedure cauta(x:longint);
var i,j,con,dif:longint;
    ok:boolean;
begin
dif:=x*k-s;
i:=1;
ok:=true;
while i<=n do begin
con:=0;
j:=i;
while (con<x) and (j<=n) do begin con:=con+v[j]; inc(j); end;
if con>x then begin dec(j); con:=con-v[j]; end;
dif:=dif-(x-con);
if dif<0 then begin break; ok:=false; end
         else i:=j;
end;
if not ok then cauta(x+1)
          else write(g,x);
end;
begin
assign(f,'transport.in'); reset(f);
assign(g,'transport.out'); rewrite(g);
read(f,n,k);
s:=0;
for i:=1 to n do begin
read(f,v[i]);
s:=s+v[i];
end;
if s mod k=0 then x:=s div k
             else x:=(s div k)+1;
{i:=1;
dif:=x*k-s;
while ok do begin
con:=0;
j:=i;
while (con<x) and (j<=n) do begin con:=con+v[j]; inc(j); end;
if con>x then begin dec(j); con:=con-v[j]; end;
dif:=dif-(x-con);
if dif<0 then begin i:=1; inc(x); dif:=x*k-s; end
         else i:=j;
if (dif>=0) and (i>=n) then ok:=false;
end;
write(g,x)  }
cauta(x);
close(g);
end.