Problem:
 Score : 600 points    Problem Statement 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 1 4 1
2 3 4 1      Sample Output 1 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.        Sample Input 2 6 2
8 6 9 1 2 1      Sample Output 2 7       Sample Input 3 10 0
1 1000000000 1 1000000000 1 1000000000 1 1000000000 1 1000000000      Sample Output 3 4999999996

Solution (Python):
import os
import sys
from collections import defaultdict

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


N, K = list(map(int, sys.stdin.readline().split()))
H = list(map(int, sys.stdin.readline().split()))

if K == N:
    print(0)
    exit()


































dp = [[defaultdict(lambda: INF) for _ in range(K + 2)] for _ in range(N + 1)]
dp[0][0][0] = 0
for i in range(N):
    for k in range(K + 1):
        for h, cnt in dp[i][k].items():
            dp[i + 1][k][H[i]] = min(dp[i + 1][k][H[i]], cnt + max(0, H[i] - h))
            dp[i + 1][k + 1][h] = min(dp[i + 1][k + 1][h], dp[i][k][h])
ans = min(dp[-1][K].values())
print(int(ans))

Solution (Python):
def main():
    n,K = map(int, input().split())
    h = list(map(int, input().split()))

    DP = [[float("inf")]*(K+1) for i in range(K+1)]
    DP[0][0] = 0
    for i in range(n):
        hi = h[i]
        nxt = [[float("inf")]*(K+1) for i in range(K+1)]
        for j in range(min(i+1, K+1)):
            for k in range(j+1):
                l = i-k-1
                if l<0:
                    left = 0
                else:
                    left = h[l]
                a = hi-left if left<hi else 0
                nxt[j][0] = min(nxt[j][0], DP[j][k]+a)
                if j == K:
                    continue
                nxt[j+1][k+1] = min(nxt[j+1][k+1], DP[j][k])
        DP = nxt

    ans = float("inf")
    for i in DP:
        ans = min(ans, min(i))
    print(ans)

main()

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n,k = LI()
    a = LI()
    c = collections.defaultdict(lambda: inf)
    c[(0,0)] = 0
    for t in a:
        nc = collections.defaultdict(lambda: inf)
        for ck,cv in c.items():
            k1,k2 = ck
            if k1 >= t:
                if nc[(t,k2)] > cv:
                    nc[(t,k2)] = cv
            else:
                if nc[(t,k2)] > cv + t-k1:
                    nc[(t,k2)] = cv + t-k1
            if k2 < k:
                if nc[(k1,k2+1)] > cv:
                    nc[(k1,k2+1)] = cv
        c = nc

    return min(c.values())


print(main())

Solution (Python):
import sys
readline = sys.stdin.readline
N, K = map(int, readline().split())
H = list(map(int, readline().split()))
H = [0] + H
N += 1
inf = 10**18
dp = [[[inf]*(K+1) for j in range(N)] for i in range(N)]
dp[0][0][0] = 0
for i in range(1, N):
    hi = H[i]
    for j in range(i):
        hj = H[j]
        for k in range(1, 1 + min(K, i)):
            dp[i][j][k] = dp[i-1][j][k-1]
    for k in range(min(K+1, i)):
        dp[i][i][k] = min(dp[i-1][j][k] + max(0, hi - H[j]) for j in range(i))

print(min(min(dp[N-1][j]) for j in range(N)))

Solution (Python):
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  

n, k = na()
h = na()

ys = h[:] + [0]
ys.sort()

ys2 = []
for v in ys:
    if len(ys2) > 0 and ys2[-1] == v:
        continue
    ys2.append(v)
ys = ys2
m = len(ys)

dp = [[999999999999999999] * m for _ in range(k+1)]
dp[0][0] = 0

for v in h:
    ndp = [[999999999999999999] * m for _ in range(k + 1)]
    for i in range(k+1):
        plus = 99999999999999999
        for j in range(m):
            plus = min(plus + (ys[j] - ys[j-1] if j > 0 else 0), dp[i][j])
            if ys[j] == v:
                ndp[i][j] = min(ndp[i][j], plus)
            elif i+1 <= k:
                ndp[i+1][j] = min(ndp[i+1][j], plus)
        plus = 99999999999999999
        for j in range(m-1, -1, -1):
            plus = min(plus, dp[i][j])
            if ys[j] == v:
                ndp[i][j] = min(ndp[i][j], plus)
            elif i+1 <= k:
                ndp[i+1][j] = min(ndp[i+1][j], plus)
    dp = ndp

ans = min(min(row) for row in dp)
print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np

N,K = map(int,readline().split())
H = np.array([0] + read().split(),np.int64)

INF = 10 ** 17
dp = np.full((N+1,N+1),INF,np.int64)
dp[0,0] = 0

for n,x in enumerate(H[1:],1):
    add = np.maximum(0, x - H[:n])
    dp[n,1:] = np.min(dp[:n,:-1] + add[:,None], axis=0)

answer = dp[:,N-K:].min()
print(answer)