Pagini recente » Cod sursa (job #2843600) | Cod sursa (job #1169743) | Cod sursa (job #1152764) | Rating Matraguna Andrei (Andreicm12) | Cod sursa (job #428938)
Cod sursa(job #428938)
program p1;
var f,g:text;
a:array[1..16000]of integer;
i,n,c,k,s,max:longint;
function posibilitati(x:longint):longint;
var i,cam,sum:longint;
begin
cam:=0;sum:=0;
for i:=1 to n do
if sum+a[i]<=x then sum:=sum+a[i]
else begin
sum:=a[i];
cam:=cam+1;
end;
posibilitati:=cam+1;
end;
function cauta(x,y:longint):integer;
var st,dr,mij,p,sol:longint;
begin
st:=x;dr:=y;
while st<=dr do
begin
mij:=(st+dr) div 2;
p:=posibilitati(mij);
if p>k then st:=mij+1
else begin dr:=mij-1;sol:=mij;end;
end;
cauta:=sol;
end;
begin
assign(f,'transport.in');reset(f);
assign(g,'transport.out');rewrite(g);
read(f,n,k);
read(f,a[1]);
if k>n then k:=n;
s:=a[1];max:=a[1];
for i:=2 to n do
begin
read(f,a[i]);
s:=s+a[i];
if a[i]>max then max:=a[i];
end;
c:=cauta(max,s);
writeln(g,c);
close(f);
close(g);
end.