program peaks;
var
h,q:array[-1..100010] of longint;
sum:array[-1..100010] of int64;
n,k,i,j,t,last,k1,p,l,r,minl,minr,minh,hh:longint;
bool:boolean;
ans,temp,min:int64;
begin
assign(input,'peaks.in');
reset(input);
assign(output,'peaks.out');
rewrite(output);
readln(n,k);
sum[0]:=0;
for i:=1 to n do
begin
read(h[i]);
sum[i]:=sum[i-1]+h[i];
end;
h[0]:=0;
h[n+1]:=0;
last:=0;
k1:=0;
for i:=1 to n do
begin
if (last<h[i]) and (h[i]>h[i+1]) then
begin
inc(k1);
q[k1]:=i;
end;
if h[i]<>h[i+1] then last:=h[i];
end;
t:=k1;
ans:=0;
for p:=1 to k1-k do
begin
min:=-1;
for i:=1 to t do
begin
j:=q[i];
bool:=true;
while bool do
begin
if ((last>h[j]) and (h[j]<h[j+1])) or (j=n+1) then
begin
bool:=false;
r:=j;
end;
if h[j]<>h[j+1] then last:=h[j];
j:=j+1;
end;
j:=q[i];
bool:=true;
while bool do
begin
if ((last>h[j]) and (h[j]<h[j-1])) or (j=0) then
begin
bool:=false;
l:=j;
end;
if h[j]<>h[j-1] then last:=h[j];
j:=j-1;
end;
if h[l]>h[r] then
begin
hh:=h[l];
while h[r-1]<h[l] do
dec(r);
end
else
begin
hh:=h[r];
while h[l+1]<h[r] do
inc(l);
end;
temp:=sum[r-1]-sum[l]-hh*(r-l-1);
if (temp<min) or (min=-1) then
begin
min:=temp;
minl:=l;
minr:=r;
minh:=hh;
end;
end;
ans:=ans+min;
for i:=minl+1 to minr-1 do
begin
h[i]:=minh;
sum[i]:=sum[i-1]+h[i];
end;
for i:=minr to n do
sum[i]:=sum[i-1]+h[i];
t:=0;
last:=0;
for i:=1 to n do
begin
if (last<h[i]) and (h[i]>h[i+1]) then
begin
inc(t);
q[t]:=i;
end;
if h[i]<>h[i+1] then last:=h[i];
end;
end;
writeln(ans);
close(input);
close(output);
end.