Pagini recente » Cod sursa (job #721079) | Cod sursa (job #2612840) | Cod sursa (job #597913) | Cod sursa (job #2700016) | Cod sursa (job #82736)
Cod sursa(job #82736)
program ferma;
const
fin='ferma.in';
fout='ferma.out';
nmax=10000;
kmax=1000;
var
best:array[0..1,0..nmax] of longint;
s:array[0..nmax] of longint;
old,new:integer;
i,j,k,x,y,max,n:longint;
function maxim(x,y:longint):longint;
begin
if x>y then
maxim:=x
else
maxim:=y;
end;
procedure init_best;
begin
fillchar(best,sizeof(best),0);
end;
begin
assign(input,fin);
reset(input);
readln(n,k);
s[0]:=0;
for i:=1 to n do
begin
read(x);
s[i]:=s[i-1]+x;
end;
new:=1;old:=0;
init_best;
for i:=1 to k+1 do
begin
old:=new;new:=old xor 1;
max:=0;
for j:=1 to n do
begin
best[new,j]:=maxim(best[new,j-1],max+s[j]);
max:=maxim(max,best[old,j]-s[j]);
end;
end;
close(input);
assign(output,fout);
rewrite(output);
writeln(best[new,n]);
close(output);
end.