Cod sursa(job #482272)

Utilizator 05_YohnE1 La5c01 05_Yohn Data 2 septembrie 2010 22:12:52
Problema Transport Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.96 kb
Program transport;
var v:array[1..16001]of integer;
n,k,i,max:longint;

function ok(x:longint):boolean;
var s,c,i:longint;
begin
s:=0;
c:=0;
i:=1;
while i<=n do begin
      if v[i]>x then break;
      if s+v[i]<=x then s:=s+v[i]
                  else begin
                       s:=v[i];
                       inc(c);
                       end;
      inc(i);
end;

if i<=n then ok:=false
        else begin
             if c<k then ok:=true
                     else ok:=false;
             end;
end;


function caut(a,b:longint):longint;
var mij:longint;
begin
while a<b do begin
      mij:=(a+b)shr 1;
      if ok(mij) then b:=mij
                 else a:=mij+1;
end;
caut:=a;
end;



begin
assign(input,'transport.in');
reset(input);
read(n,k);
max:=0;
for i:=1 to n do begin
    read(v[i]);
    if v[i]>max then max:=v[i];
end;
assign(output,'transport.out');
rewrite(output);
write(caut(max,n*max));
close(output);
end.