local mmi, mma = math.min, math.max
local n, k = io.read("*n", "*n")
local h = {0}
for i = 1, n do
  h[i + 1] = io.read("*n")
end
h[n + 2] = 0
local hsum = {h[1]}
for i = 2, n + 2 do
  hsum[i] = hsum[i - 1] + h[i]
end
local t = {}
t[1] = {}
for i = 1, k + 1 do
  t[1][i] = 0
end
for i = 2, n + 1 do
  t[i] = {}
  for j = 1, k + 1 do
    t[i][j] = t[i - 1][j]
  end
  if h[i - 1] < h[i] and h[i + 1] < h[i] then
    local right = h[i + 1]
    local left = h[i]
    for j = i - 1, 1, -1 do
      if left <= h[j] then
        break
      end
      local len = i - j
      local score = hsum[i] - hsum[j]
      if h[j] <= right then
        score = score - len * right
        for z = len + 1, k + 1 do
          t[i][z] = mma(t[i][z], t[j][z - len] + score)
        end
        break
      else
        score = score - len * h[j]
        for z = len + 1, k + 1 do
          t[i][z] = mma(t[i][z], t[j][z - len] + score)
        end
      end
      left = h[j]
    end
  end
  for j = 2, k + 1 do
    t[i][j] = mma(t[i][j], t[i][j - 1])
  end
end
local ret = -t[n + 1][k + 1]
for i = 2, n + 1 do
  ret = ret + mma(0, h[i] - h[i - 1])
end
print(ret)
