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)

def solve():

    INF = 10**12



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

    def min2(x, y): return x if x <= y else y



    N, K = list(map(int, input().split()))

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



    dp = [[INF]*(N+1) for _ in range(N+1)]

    dp[0][0] = 0

    for i in range(1, N+1):

        H = Hs[i]

        diffs = []

        for iPrev in range(i):

            diff = max2(0, H-Hs[iPrev])

            diffs.append(diff)

        dp[i][0] = 0

        dp[i][1] = H

        for j in range(2, min(i, N-K)+1):

            for iPrev in range(i):

                dp[i][j] = min2(dp[i][j], dp[iPrev][j-1]+diffs[iPrev])



    ans = min(dpi[N-K] for dpi in dp)

    print(ans)





solve()

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)

def solve():
    INF = 10**12

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

    def min2(x, y):
        return x if x <= y else y

    N, K = list(map(int, input().split()))
    Hs = [0] + list(map(int, input().split()))
    dp = [[INF] * (N + 1) for _ in range(N + 1)]
    dp[0][0] = 0
    for i in range(1, N + 1):
        H = Hs[i]
        diffs = []
        for iPrev in range(i):
            diff = max2(0, H - Hs[iPrev])
            diffs.append(diff)
        dp[i][0] = 0
        dp[i][1] = H
        for j in range(2, min(i, N - K) + 1):
            for iPrev in range(i):
                dp[i][j] = min2(dp[i][j], dp[iPrev][j - 1] + diffs[iPrev])
    ans = min(dpi[N - K] for dpi in dp)
    print(ans)


solve()

-    INF = float("inf") +    INF = 10**12 +    def min2(x, y): +        return x if x <= y else y + -    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) +    dp = [[INF] * (N + 1) for _ in range(N + 1)] +    dp[0][0] = 0 +    for i in range(1, N + 1): +        H = Hs[i] +        diffs = [] +        for iPrev in range(i): +            diff = max2(0, H - Hs[iPrev]) +            diffs.append(diff) +        dp[i][0] = 0 +        dp[i][1] = H +        for j in range(2, min(i, N - K) + 1): +            for iPrev in range(i): +                dp[i][j] = min2(dp[i][j], dp[iPrev][j - 1] + diffs[iPrev]) +    ans = min(dpi[N - K] for dpi in dp) +    print(ans) -ans = solve() -print(ans) +solve()