Cod sursa(job #1680775)

Utilizator wilson182Alexandrina Panfil wilson182 Data 9 aprilie 2016 09:12:46
Problema Transport Scor 90
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.16 kb
program transport;
var  max, i, st, dr,s, piv, m  : longint;
     n, k, r : integer;
     a : array[1..16000] of integer;
     f : text;
function check(x:longint):boolean;
begin
    for i:=1 to n do begin
                    s:=s+a[i];
                    if s>m then begin
                                  inc(r);
                                  s:=a[i];
                                end;
                  end;
    if r<=k then check:=true else check:=false;
end;
begin max:=0;
      assign(f,'transport.in');
      reset(f);
      readln(f, n, k);
      for i:=1 to n do begin
                         readln(f, a[i]);
                         if a[i]>max then max:=a[i];
                       end;
      close(f);
      st:=max;
      dr:=256000001;
      while st<=dr do begin
                        piv:=st+(dr-st)div 2;
                        s:=0; m:=piv;
                        r:=1;
                        if check(m) then begin dr:=piv-1; end
                                    else st:=piv+1;
                      end;
      assign(f, 'transport.out');
      rewrite(f);
      writeln(f, m ); close(f);
      end.