We will create an artwork by painting black some squares in a white square grid with 10^9 rows and N columns.

The current plan is as follows: for the i-th column from the left, we will paint the H_i bottommost squares and will not paint the other squares in that column.

Before starting to work, you can choose at most K columns (possibly zero) and change the values of H_i for these columns to any integers of your choice between 0 and 10^9 (inclusive).

Different values can be chosen for different columns.

Then, you will create the modified artwork by repeating the following operation:

 - Choose one or more consecutive squares in one row and paint them black. (Squares already painted black can be painted again, but squares not to be painted according to the modified plan should not be painted.)
Find the minimum number of times you need to perform this operation.

-----Constraints-----
 - 1 \leq N \leq 300
 - 0 \leq K \leq N
 - 0 \leq H_i \leq 10^9
 - All values in input are integers.

-----Input-----
Input is given from Standard Input in the following format:
N K
H_1 H_2 ... H_N

-----Output-----
Print the minimum number of operations required.

-----Sample Input-----
4 1
2 3 4 1

-----Sample Output-----
3

For example, by changing the value of H_3 to 2, you can create the modified artwork by the following three operations:
 - Paint black the 1-st through 4-th squares from the left in the 1-st row from the bottom.
 - Paint black the 1-st through 3-rd squares from the left in the 2-nd row from the bottom.
 - Paint black the 2-nd square from the left in the 3-rd row from the bottom.

Difficulty: interview

Starter code:

Solutions:
import numpy as np
INF = 10**15
def solve(n, k, h):
    dp = np.full((n+1, n+1), INF, dtype=int)
    dp[0, 0] = 0
    h = np.array([0] + h, dtype=int)
    for i, h_i in enumerate(h[1:], 1):
        t = np.maximum(h_i - h[:i], 0)
        dp[i,1:] = np.min(dp[:i,:-1]+t[:,None], axis=0)
    return np.min(dp[:, n-k])

n, k = map(int, input().split())
h = list(map(int, input().split()))
print(solve(n, k, h))

INF = float("inf")

N, K, *H = map(int, open(0).read().split())
H = [0] + H

if N == K:
    print(0)
    return

dp = [0] + [INF] * N
for i in range(1, N - K + 1):
    dp = [INF] * i + [min(dp[k] + max(0, h - H[k]) for k in range(i - 1, j)) for j, h in enumerate(H[i:], i)]

print(min(dp))

import numpy as np

n, k = map(int, input().split())
h = np.array(input().split(), dtype=np.int)

if k < n:
  dp = np.zeros((n - k, n), dtype=np.int)
  dp[0] = h
  for i in range(n - k - 1):
    for j in range(i + 1, n):
      dp[i+1, j] = np.min(dp[i][i:j] + np.maximum(h[j] - h[i:j], 0))
  print(dp[n-k-1, n-k-1:].min())
else:
  print(0)

def solve():
    from sys import stdin
    f_i = stdin
    
    N, K = map(int, f_i.readline().split())
    H = tuple(map(int, f_i.readline().split()))
    
    if N == K:
        return 0
    
    dp1 = list(H)
    for i in range(1, N - K):
        dp2 = dp1[:i]
        for x, H_x in enumerate(H[i:], start=i):
            v = min(dp_j + max(0, H_x - H_j) for dp_j, H_j in zip(dp1[i-1:x], H[i-1:x]))
            dp2.append(v)
        dp1 = dp2
    return min(dp1[N-K-1:])

print(solve())

def main():
    import numpy as np
    n, k = list(map(int, input().split()))
    H = list(map(int, input().split()))
    s = np.array(sorted(set(H+[0])), dtype=np.int64)
    d = {i: j for j, i in enumerate(s)}
    l = len(s)
    dp = np.full((k+1, l), 10**13, dtype=np.int64)
    dp2 = np.zeros((k+1, l), dtype=np.int64)
    dp[0, 0] = 0

    for h in H:
        dh = d[h]
        dp2 = np.zeros((k+1, l), dtype=np.int64)
        dp2[0] = np.full(l, 10**13, dtype=np.int64)
        dp2[1:, :] = dp[:-1, :]
        temp = np.min(
            dp + np.concatenate([(h-s)[:dh], np.zeros(l-dh, dtype=np.int64)]), axis=1)
        dp2[:, dh] = np.minimum(dp2[:, dh], temp[:])
        dp = dp2
    print((np.min(dp)))


main()


def solve():
    INF = float('inf')

    def max2(x, y): return x if x >= y else y

    N, K = list(map(int, input().split()))
    Hs = [0] + list(map(int, input().split()))

    if N == K:
        return 0

    dp = [INF] * (N+1)
    dp[0] = 0
    for j in range(1, N-K+1):
        dp2 = [INF] * (N+1)
        for i, H in enumerate(Hs[j:], j):
            for k in range(j-1, i):
                dp2[i] = min(dp2[i], dp[k] + max2(0, H-Hs[k]))
        dp = dp2

    return min(dp)

ans = solve()
print(ans)


import numpy as np

n, k = list(map(int, input().split()))
hhh = list(map(int, input().split()))
hhh = np.array([0] + hhh, dtype=np.int64)
INF = 10 ** 18
dp = np.full((n + 1, k + 1), fill_value=INF, dtype=np.int64)
dp[0, 0] = 0
for i, h in enumerate(hhh[1:], start=1):
    ndp = np.full((n + 1, k + 1), fill_value=INF, dtype=np.int64)
    ndp[:n, 1:] = dp[:n, :-1]
    ndp[i] = (dp + np.maximum(0, h - hhh)[:, np.newaxis]).min(axis=0)
    dp = ndp

print((dp.min()))


import numpy as np

N,K=map(int,input().split())
H=list(map(int,input().split()))
sortedH=np.hstack((np.zeros(1,dtype=np.int64), np.sort(H)))

dic={}
for i,h in enumerate(sortedH):
    dic[h]=i

inf=10**15
dp=np.full((302,len(sortedH)+1),inf,dtype=np.int64)
dp[0,0]=0

for i,h in enumerate(H):
    for k in range(i+1,-1,-1):
        dp[k+1]=np.minimum(dp[k+1],dp[k])
        idx=dic[h]
        ma=np.min(dp[k][:idx]+h-sortedH[:idx])
        mb=np.min(dp[k][idx:])
        dp[k]=np.full(len(sortedH)+1,inf,dtype=np.int64)
        dp[k][idx]=min(ma,mb)
print(np.min(dp[:K+1]))