Cod sursa(job #517375)

Utilizator vendettaSalajan Razvan vendetta Data 28 decembrie 2010 16:17:33
Problema Transport Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.63 kb
const f='transport.in';h='transport.out';
    nmax=20000;
var
    v:array[1..nmax] of longint;
    min,t,u,p,m,nr,max,s,k1,k,n,i:longint;


{function rez(g:longint):longint;
    var
        nr,s,j:longint;
    begin
        nr:=1;s:=0;
        for j:=1 to n do
            if a[j]>g then begin rez:=k+1; exit;end
            else
                begin
                    s:=s+a[j];
                    if (s>g) then begin inc(nr);s:=a[j];end
                end;
    rez:=nr;
    end;

function cauta(p:longint;u:longint):longint;
    var
        m:longint;
    begin
        m:=(p+u) div 2;
        if (p=u) and (rez(m)<=k) then begin cauta:=p; exit;end;
        if (p>u) then begin cauta:=p;exit;end;
        if (rez(m)<=k) then cauta:=cauta(p,m-1)
                        else cauta:=cauta(m+1,u);
    end;
}
begin
    assign(input,f);reset(input);
    assign(output,h);rewrite(output);
    readln(n,k);
    for i:=1 to n do
        begin
        readln(v[i]);
        k1:=k1+v[i];
        if (v[i]>max) then max:=v[i];
        end;
    u:=max;
    p:=k1;
    min:=16001;
    while (u<=p) do
        begin
        m:=(u+p) div 2;
        nr:=0;
        i:=1;
        while i<=n do
            begin
            s:=0;
            while (s+v[i]<=m) and (i<=n) do
                begin
                s:=s+v[i];
                i:=i+1;
                end;
            inc(nr);
            end;
        if (nr>k) then u:=m+1
        else
            begin
            if (nr<=k) then begin min:=m;p:=m-1;end;
            end;
        end;
        write(min);
    close(input);close(output);
end.