Cod sursa(job #301684)

Utilizator costyv87Vlad Costin costyv87 Data 8 aprilie 2009 13:02:41
Problema Transport Scor 0
Compilator fpc Status done
Runda all.you.can.happily.code Marime 0.76 kb
var f,g:text;  i,sol,n,max,s,k:Longint;
    v:array[1..16000] of longint;
function ok(x:longint):boolean;
var i,t,con:longint;
begin
i:=0; t:=0;
while i<=n do begin
con:=0;
while con<x do begin  inc(i); con:=con+v[i]; end;
if con>x then begin dec(con,v[i]); dec(i); end;
inc(t);
end;
ok:=(t<=k);
end;
procedure cauta(st,dr:longint);
var x:longint;
begin
x:=(st+(dr-st)) div 2;
if ok(x) then begin
              sol:=x;
              if st<=x-1 then cauta(st,x-1);
              end
          else cauta(x+1,dr);
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];
if v[i]>max then max:=v[i];
end;
cauta(max,s);
write(g,sol);
close(g);
end.