Cod sursa(job #301200)

Utilizator costyv87Vlad Costin costyv87 Data 7 aprilie 2009 23:46:02
Problema Transport Scor 50
Compilator fpc Status done
Runda all.you.can.happily.code Marime 0.65 kb
var v:array[1..15100] of longint;
    i,j,con,s,x,k,n,dif:longint;
    f,g:text; ok:boolean;
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;
ok:=true;
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);
close(g);
end.