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



Sample input:
4 1
2 3 4 1


Sample output:
3

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split( ))
h = list(map(int, input().split( )))
#h = [0]+h#h[0]=0追加
inf = 10**15

dp = [[inf]*n for _ in range(n)] #dp[i][j]右端がi、j本残す
mn = h[0]
for i in range(n):
    mn = min(mn,h[i])
    dp[i][0] = min(mn,h[i])

for i in range(n):##右端
    for j in range(i+1):#本数
        for t in range(j-1,i):#右から2番目
            dp[i][j] = min(dp[i][j],dp[t][j-1] +max(h[i]-h[t], 0))##t>=j-1

###print(dp[n-1][n-k-1])これでは意味が無い
ans = inf 

for i in range(n):
    ans = min(ans, dp[i][n-k-1])
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N, K = (int(i) for i in input().split())
    H = [0] + [int(i) for i in input().split()]
    if N == K:
        return print(0)
    dp = [[10**10]*(N-K+1) for i in range(N+1)]
    dp[0][0] = 0
    for i, h in enumerate(H):
        if i == 0:
            continue
        dp[i][1] = h
    for i in range(1, N+1):
        for j in range(1, N-K+1):
            for x in range(1, i):
                dp[i][j] = min(dp[i][j], dp[x][j-1] + max(0, H[i] - H[x]))
    ans = 10**10
    for i in range(N+1):
        # print(dp[i])
        ans = min(ans, dp[i][N-K])
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N, K = (int(i) for i in input().split())
    H = [int(i) for i in input().split()]
    diff = [[0, i] for i in range(N)]
    if N == K:
        return print(0)
    for i, h in enumerate(H):
        if i == 0:
            diff[i][0] = max(H[i] - 0, H[i] - H[i+1])
        elif i == N - 1:
            diff[i][0] = max(H[i] - H[i-1], H[i] - 0)
        else:
            diff[i][0] = max(H[i] - H[i-1], H[i] - H[i+1])

    diff.sort(reverse=True)
    task = [[d, i] for d, i in diff[:K]]
    task.sort()
    for d, i in task:
        if i == 0 or i == N - 1:
            H[i] = 0
        else:
            H[i] = min(H[i-1], H[i+1])
    ans = 0
    for i, h in enumerate(H):
        if i == 0:
            ans += H[i]
        else:
            ans += max(H[i] - H[i-1], 0)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, K = map(int, input().split())
    H = list(map(int, input().split()))
    s = sorted(set(H+[0]))
    d = {i: j for j, i in enumerate(s)}
    l = len(s)
    m = max(s)
    a = [m-i for i in s]
    dp = [[10**13]*l for _ in range(K+1)]
    dp[0][0] = 0
    for h in H:
        dh = d[h]
        dp2 = [[0]*l for _ in range(K+1)]
        dp2[0] = [10**13]*l
        t = [[0]*l for _ in range(K)]
        for i in range(K):
            for j in range(l):
                t[i][j] = dp[i][j]+a[j]
        min_s = [[0]*l for i in range(K)]

        for i in range(K):
            for j in range(l-2, -1, -1):
                min_s[i][j] = min(dp[i][j+1], dp[i][j])
            for j in range(1, l):
                t[i][j] = min(t[i][j-1], t[i][j])
            for j in range(l):
                t[i][j] = t[i][j]-a[j]

        for i in range(K):
            for j in range(l):
                dp2[i+1][j] = min(min_s[i][j], t[i][j])

        for i in range(K+1):
            temp = 10**13
            for j in range(l):
                temp = min(temp, dp[i][j]+max(h-s[j], 0))
            dp2[i][dh] = min(dp2[i][dh], temp)
        dp = dp2
    print(min([min(i) for i in dp]))


main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n,k = LI()
h = LI()
if n == 1 and k == 1:
    print(0)
    quit()
elif n == 1 and k == 0:
    print(h[0])
    quit()
dp = [[inf]*(k+1) for _ in range(n)]
lst = [0]*n
for i in range(n):
    if i == 0:
        lst[i] = h[i]
    else:
        lst[i] = max(0,h[i]-h[i-1]) 
s = sum(lst)
for i in range(n):
    dp[i][0] = s

if k != 0:
    dp[0][1] = s - lst[0] - lst[1] + h[1]
for i in range(1,n):
    for j in range(1,k+1):
        for l in range(i):
            if i+1 >= j:
                dp[i][j] = min(dp[i][j],dp[i][j-1],dp[l][j-1] - max(0,h[i]-h[i-1]))

ans = inf
for i in range(n):
    ans = min(ans,dp[i][-1])
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n,k = LI()
h = LI()
dp = [[inf]*(k+1) for _ in range(n)]

tmp = 0
for i in range(n):
    if i == 0:
        tmp += h[i]
    else:
        tmp += max(0,h[i] - h[i-1])

for i in range(n):
    dp[i][0] = tmp
for i in range(1,k+1):
    dp[0][i] = max(0,tmp - abs(h[1]-h[0]))
for i in range(1,n):
    for j in range(1,k+1):
        for l in range(i):
            dp[i][j] = min(dp[i][j],dp[i][j-1],dp[l][j-1] - max(0,h[i] - h[i-1])) 
ans = inf
for i in range(n):
    ans = min(ans,dp[i][k])
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n,k = LI()
h = LI()
dp = [[inf]*(k+1) for _ in range(n)]

tmp = 0
for i in range(n):
    if i == 0:
        tmp += h[i]
    else:
        tmp += max(0,h[i] - h[i-1])

for i in range(n):
    dp[i][0] = tmp
for i in range(1,k+1):
    dp[0][i] = tmp - min(h[0],abs(h[1]-h[0]))
for i in range(1,n):
    for j in range(1,k+1):
        for l in range(i):
            dp[i][j] = min(dp[i][j],dp[i][j-1],dp[l][j-1] - max(0,h[i] - h[i-1])) 
ans = inf
for i in range(n):
    ans = min(ans,min(dp[i]))
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
H = [0] + [int(i) for i in input().split()]

dp = [[float('inf')] * (N - K + 1) for _ in range(N + 1)]
dp[0][0] = 0

# 右端がi、サイズがj
for i in range(1, N + 1) :
  for j in range(N - K + 1) :
    dp[i][j] = min(dp[k][j-1] + max(0, H[i] - H[k]) for k in range(i))
    
print(dp[i][N-K])

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
h = list(map(int, input().split()))

#dp[a][b][c]: aまで見てb個消していて消してない右端がcのときの最小値(ぜんぶ1-indexed)
dp = [[[10**12 for _ in range(n+1)] for _ in range(k+1)] for _ in range(n+1)]
dp[0][0][0] = 0

for a in range(n):
	for b in range(k+1):

		for c in range(a+1):
			#h[a]を消す場合
			if b < k:
				dp[a+1][b+1][c] = min(dp[a+1][b+1][c], dp[a][b][c])

			#h[a]を消さない場合
			dp[a+1][b][a+1] = min(dp[a+1][b][a+1], dp[a][b][c] + max(0, h[a] - h[c-1]))

print(min(dp[n][k])+1)

Submission status: Wrong Answer

Solution code:
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
    print(min(dp1[N-K-1:]))

solve()

Submission status: Wrong Answer

Solution code:
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()))
    
    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
    print(dp1[-1])

solve()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
H = [0] + list(map(int, input().split()))
dp = [[10 ** 100] * (N-K+1) for _ in range(N+1)]
dp[0][0] = 0
for x in range(1, N+1):
    for y in range(1, min(x+1, N-K+1)):
        dp[x][y] = min([dp[i][y-1] + max(H[x] - H[i], 0) for i in range(x)])
print(min([dp[i][N-K] for i in range(1, N+1)]))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
H = [0] + list(map(int, input().split()))
dp = [[10 ** 100] * (N-K+1) for _ in range(N+1)]
dp[0][0] = 0
for x in range(1, N+1):
    for y in range(1, min(x+1, N-K+1)):
        dp[x][y] = min([dp[i][y-1] + max(0, H[x] - H[i]) for i in range(x)])
print(min([dp[i][N-K] for i in range(1, N+1)]))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
H = [0] + list(map(int, input().split()))
dp = [[10 ** 100] * (N-K+1) for _ in range(N+1)]
dp[0][0] = 0
for x in range(1, N+1):
    for y in range(1, min(x+1, N-K+1)):
        dp[x][y] = min(dp[i][y-1] + max(0, H[x] - H[i]) for i in range(x))
print(min(dp[i][N-K] for i in range(1, N+1)))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
H = [0]+list(map(int, input().split()))
dp = [[10**100] * (N-K+1) for _ in range(N+1)]
dp[0][0] = 0
for x in range(1, N+1):
    for y in range(1, min(x+1, N-K+1)):
        dp[x][y] = min(dp[i][y-1] + max(0, H[x]-H[i]) for i in range(x))
print(min(dp[i][-1] for i in range(1, N+1)))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
H = [0]+list(map(int, input().split()))
dp = [[1<<100] * (N-K+1) for _ in range(N+1)]
dp[0][0] = 0
for x in range(1, N+1):
    for y in range(1, min(x+1, N-K+1)):
        dp[x][y] = min(dp[i][y-1] + max(0, H[x]-H[i]) for i in range(x))
print(min(dp[i][N-K] for i in range(1, N+1)))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
H = [0]+list(map(int, input().split()))
dp = [[10**100] * (N-K+1) for _ in range(N+1)]
dp[0][0] = 0
for x in range(1, N+1):
    for y in range(1, min(x+1, N-K+1)):
        dp[x][y] = min(dp[i][y-1] + max(0, H[x]-H[i]) for i in range(x))
print(min(dp[i][N-K] for i in range(1, N+1)))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
h=list(map(int,input().split()))
h.insert(0,0)
dp=[[float("inf")]*(n-k+1) for _ in range(n+1)]
dp[0][0]=0
for i in range(1,n+1):
    for j in range(1,n-k+1):
        now=float("inf")
        for l in range(i):
            now=min(dp[l][j-1]+max(0,h[i]-h[l]),now)
        dp[i][j]=now

ans=float("inf")
for i in range(1,n+1):
    ans=min(ans,dp[i][n-k])

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
H = list(map(int, input().split()))
H = [0] + H


dp = [[float('inf')] * (N + 1) for i in range(N + 1)]
dp[0][0] = 0

for i in range(1, N + 1):
    for j in range(1, N + 1):
        for x in range(i):
            dp[i][j] = min(dp[i][j], dp[x][j - 1] + max(0, H[i] - H[x]))


ans = float('inf')
for i in range(1, N + 1):
    ans = min(ans, dp[i][max(0,  N - K)])

print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split( ))
h = list(map(int, input().split( )))
#h = [0]+h#h[0]=0追加
inf = 10**15

dp = [[inf]*n for _ in range(n)] #dp[i][j]右端がi、j本残す
mn = h[0]
for i in range(n):
    mn = min(mn,h[i])
    dp[i][0] = min(mn,h[i])

for i in range(n):##右端
    for j in range(i+1):#本数
        for t in range(i):#右から2番目
            dp[i][j] = min(dp[i][j],dp[t][j-1] +max(h[i]-h[t], 0))

###print(dp[n-1][n-k-1])これでは意味が無い
ans = inf 

for i in range(n):
    ans = min(ans, dp[i][n-k-1])
print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split( ))
h = list(map(int, input().split( )))
#h = [0]+h#h[0]=0追加
inf = 10**15

dp = [[inf]*n for _ in range(n)] #dp[i][j]右端がi、j本残す
mn = h[0]
for i in range(n):
    mn = min(mn,h[i])
    dp[i][0] = min(mn,h[i])

for i in range(1,n):##右端
    for j in range(i+1):#本数
        for t in range(i):#右から2番目
            dp[i][j] = min(dp[i][j],dp[t][j-1] +max(h[i]-h[t], 0))
#for i in range(n):
#    print(*dp[i])
print(dp[n-1][n-k-1])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-


N,K=map(int, input().split())
H=list(map(int, input().split()))
G=[0]
G.extend(H[:])

dp=[[0]*(N+1) for i in range(N-K+1)]


tempa=0
if K<N-1:
    for jtemp in range(N-1):
        j=jtemp+2
        dp[1][j]=G[j]#min(dp[1][j-1],G[j])
    for itemp in range(N-K-1):
        i=itemp+2
        for jtemp in range(N-(i-1)):
            j=jtemp+i
            temp=int(N*1e9)
            
            for ktemp in range(j-(i-1)):
                k=ktemp+i-1
                val=G[j]-G[k]
                if val<0:
                    val=0
                if temp>dp[i-1][k]+val:
                    temp=dp[i-1][k]+val
            dp[i][j]=temp
    tempa=dp[N-K][-1]
    for jtemp in range(N):
        j=jtemp+1
        if tempa>dp[N-K][j] and dp[N-K][j]!=0:
            tempa=dp[N-K][j]
elif N-K==1:
    tempa=min(H[:])
else:
    tempa=0
print(tempa)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
h = list(map(int, input().split()))

if n == k:
    print(0)
    exit()

l=[]

for i in range(n):
    fast = max(h)
    last = max(h)
    if i == 0 and i == n-1:
        pass
    elif i==0:
        fast = h[i]
        last = abs(h[i] - h[i+1])
    elif i==n-1:
        fast = abs(h[i-1] - h[i])
        last = h[i]
    else:
        fast = abs(h[i-1] - h[i])
        last = abs(h[i] - h[i+1])

    if fast > last:
        l.append([last, i, h[i]])
    else:
        l.append([fast, i, h[i]])
l = sorted(l, key=lambda x:(x[0],x[2]), reverse=True)
del l[:k]
hk = [h[s[1]] for s in sorted(l, key=lambda x:x[1])]

def painting(h):
    count = min(h)
    h = [s - min(h) for s in h]
    temp = 0
    for i,c in enumerate(h):
        if not c == 0:
            if i+1 == len(h):
                count += painting(h[temp:])
            continue
        b = h[temp:i]
        temp = i+1
        if not b == []:
            count += painting(b)
    return count

count = 0
count = painting(hk)
print(count)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
h = list(map(int, input().split()))

if n == k:
    print(0)
    exit()

l=[]

for i in range(n):
    fast = max(h)
    last = max(h)
    if i == 0 and i == n-1:
        pass
    elif i==0:
        fast = h[i]
        last = abs(h[i] - h[i+1])
    elif i==n-1:
        fast = abs(h[i-1] - h[i])
        last = h[i]
    else:
        fast = abs(h[i-1] - h[i])
        last = abs(h[i] - h[i+1])

    if fast > last:
        l.append([last, i, h[i]])
    else:
        l.append([fast, i, h[i]])
l = sorted(l, key=lambda x:(x[0],x[2]), reverse=True)
hk = [h[s[1]] for s in l[k:]]

def painting(h):
    count = min(h)
    h = [s - min(h) for s in h]
    temp = 0
    for i,c in enumerate(h):
        if not c == 0:
            if i+1 == len(h):
                count += painting(h[temp:])
            continue
        b = h[temp:i]
        temp = i+1
        if not b == []:
            count += painting(b)
    return count

count = 0
count = painting(hk)
print(count)

Submission status: Wrong Answer

Solution code:
n,k = map(int, input().split())
h = list(map(int, input().split()))

if n == k:
    print(0)
    exit()

l=[]

for i in range(n):
    fast = max(h)
    last = max(h)
    if i == 0 and i == n-1:
        pass
    elif i==0:
        fast = h[i]
        last = abs(h[i] - h[i+1])
    elif i==n-1:
        fast = abs(h[i-1] - h[i])
        last = h[i]
    else:
        fast = abs(h[i-1] - h[i])
        last = abs(h[i] - h[i+1])

    if fast > last:
        l.append([last, i, h[i]])
    else:
        l.append([fast, i, h[i]])
l = sorted(l, key=lambda x:(x[0],x[2]), reverse=True)
print(l)
hk = [h[s[1]] for s in l[k:]]

print(hk)

def painting(h):
    count = min(h)
    h = [s - min(h) for s in h]
    temp = 0
    for i,c in enumerate(h):
        if not c == 0:
            if i+1 == len(h):
                count += painting(h[temp:])
            continue
        b = h[temp:i]
        temp = i+1
        if not b == []:
            count += painting(b)
    return count

count = 0
count = painting(hk)
print(count)

Submission status: Wrong Answer

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N,K = MI()
    H=LI()
    
    dp = [[0]+[inf]*(N) for _ in range(N)] #dp[right][size]: 最も右の項right, いじってないやつの数size
    for i in range(N):
        dp[i][1] = H[i]
    dp[0][1]=H[0]
    for right in range(N):
        for size in range(2,N+1):

            for i in range(right):
                dp[right][size] = min(dp[right][size], dp[i][size-1]+max(0, H[right]-H[i]))
    ans = dp[N-1][N-K]
    # print(N-K)
    # print(*dp,sep="\n")
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N,K = MI()
    H=LI()
    if K == 0:
        ans = H[0]
        for i,h in enumerate(H[1:],start=1):
            ans += max(0, h-H[i-1])
        print(ans)
        return
    
    dp = [[0]+[inf]*(N-1) for _ in range(N)] #dp[right][size]: 最も右の項right, いじってないやつの数size
    for i in range(N):
        dp[i][1] = H[i]
    dp[0][1]=H[0]
    for right in range(N):
        for size in range(2,N):

            for i in range(right):
                dp[right][size] = min(dp[right][size], dp[i][size-1]+max(0, H[right]-H[i]))
    ans = dp[N-1][N-K]
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
h = list(map(int, input().split()))
d = [[i, 0] for i in range(n)]

def cal_dif(h, idx):
  if idx == 0:
    return [i, max(h[idx]-h[idx+1], 0)]
  elif idx == len(h)-1:
    return [i, max(h[idx]-h[idx-1], 0)]
  else:
    dif = max(h[idx]-h[idx-1], 0) \
        + max(h[idx+1]-h[idx], 0) \
        - max(h[idx+1]-h[idx-1], 0)
    return [i, dif]
# cal max possible difference
for i in range(n):
  d[i] = cal_dif(h, i)
d2 = sorted(d, key = lambda x:x[1])
#cal optimal
for i in range(k):
  idx = d2[n-1][0]
  d[idx][1] = 0
  if idx == 0:
    h[idx] = h[idx+1]
    d[idx+1] = cal_dif(h, idx+1)
  elif idx == n-1:
    h[idx] = h[idx-1]
    d[idx-1] = cal_dif(h, idx-1)
  else:
    h[idx] = int((h[idx-1]+h[idx+1])/2)
    d[idx-1] = cal_dif(h, idx-1)
    d[idx+1] = cal_dif(h, idx+1)
  d2 = sorted(d, key = lambda x:x[1])
# cal optimal num
s = h[0]
for i in range(1, n):
  s += max(h[i]-h[i-1], 0)
print(s)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
H = list(map(int, input().split()))

N += 1
H = [0] + H

inf = 10 ** 15
DP = [[inf] * N for _ in range(N)]
DP[0][0] = 0
for i in range(1, N):
  DP[i][1] = H[i]
  for j in range(2, i + 1): 
    for k in range(1, j + 1):
      DP[i][j] = min(DP[i][j], DP[i - k][j - 1] + max(0, H[i] - H[i - k]))

ans = inf
for i in range(N):
  for j in range(K + 1):
    ans = min(ans, DP[i][N - j - 1])

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
H = list(map(int, input().split()))

N += 1
H = [0] + H

inf = 10 ** 15
DP = [[inf] * N for _ in range(N)]
DP[0][0] = 0
for i in range(1, N):
  DP[i][1] = H[i]
  for j in range(2, i + 1): 
    for k in range(1, j + 1):
      DP[i][j] = min(DP[i][j], DP[i - k][j - 1] + max(0, H[i] - H[i - k]))

ans = inf
for i in range(N):
  ans = min(ans, DP[i][N - K - 1])

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = list(map(int, input().split()))
H = list(map(int, input().split()))

N += 1
H = [0] + H

DP = [[0] * N for _ in range(N)]

for i in range(1, N):
  DP[i][i - 1] = max(0, H[i] - H[0])
  for j in range(i - 1):
    DP[i][j] = DP[i - 1][j] + max(0, H[i] - H[i - 1])
    for k in range(j + 1):
      DP[i][j] = min(DP[i][j], DP[i - k - 1][j - k] + max(0, H[i] - H[i - k - 1]))

ans = DP[-1][K]
for i in range(K - 1, -1, -1):
  ans = min(ans, DP[N - K + i][K])

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

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

DP=[[1<<60]*(K+1) for i in range(N+1)]

for k in range(K+1):
    DP[0][k]=0

for i in range(1,N+1):
    
    for k in range(K+1):
        for j in range(k+1):
            DP[i][k]=min(DP[i][k],DP[i-j-1][k-j]+max(0,H[i]-H[i-j-1]))
        
print(min(DP[-1]))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
H = [0] + list(map(int, input().split()))

"""
unique = {}
for i in range(H):
    unique.add(H)

H = [num for num in sorted(unique)]
"""

# dp[i][j] = minimum operations using i moves, and at col j
dp = [[float('inf') for i in range(N+1)] for j in range(K+1)]
height = [list(H) for j in range(K+1)]

for i in range(K+1): dp[i][0] = 0

for col in range(1, N+1):
    for move in range(K+1):
        useMove = dp[move - 1][col - 1] if move != 0 else float('inf')
        dontUseMove = dp[move][col - 1] + max(0, height[move][col] - height[move][col-1])
        
        if useMove < dontUseMove:
            dp[move][col] = useMove
            height[move][col] = height[move-1][col-1]
        else:
            dp[move][col] = dontUseMove

print(dp[-1][-1])

Submission status: Wrong Answer

Solution code:
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:])
else:
  print(0)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
H = [int(h) for h in input().split()]

dp = [[10**13]*N for _ in range(N)]
dp[0][0] = H[0]
    
for i in range(1, N):
    for j in range(i+1):
        if j == 0:
            dp[i][j] = min(dp[i-1][j], H[i])
            continue
        dp[i][j] = dp[i-1][j]
        for k in range(i):
            dp[i][j] = min(dp[i][j], dp[k][j-1]+max(0, H[i]-H[k]))
            
if N == K:
    ans = 0
else:
    ans = 10**13
    for i in range(N):
        ans = min(ans, dp[i][N-K-1])
        
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline
MOD = 10**9 + 7
INF = 10**13

n, K = [int(item) for item in input().split()]
h = [0] + [int(item) for item in input().split()]

# dp[i][j][k] i:till ith, j:skipped j, k: previous index
dp = [[[INF] * (n + 1) for _ in range(n + 1)] for _ in range(n + 1)]
for i, item in enumerate(h):
    if i == 0:
        dp[0][0][0] = 0
        continue
    for j in range(n-1, -1, -1):
        for k in range(n-1, -1, -1):
            if dp[i-1][j][k] == INF:
                continue
            # skip
            dp[i][j+1][k] = min(dp[i][j+1][k], dp[i-1][j][k])
            # use
            dp[i][j][i] = min(dp[i][j][i], dp[i-1][j][k] + max(0, h[i] - h[k]))
            
ans = INF
for j in range(n):
    ans = min(ans, dp[n][K][j+1])

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    inf = 10 ** 16
    n, k = map(int, input().split())
    hh = [0] + list(map(int, input().split()))
    dp = [[0] * (k + 1) for _ in range(n + 1)]
    for i in range(1, n + 1):
        h = hh[i]
        for j in range(k + 1):
            v = inf
            for nd in range(k + 1):
                if i - 1 - nd < 0 or j - nd < 0: break
                v = min(v, dp[i - 1 - nd][j - nd] + max(0, h - hh[i - 1 - nd]))
            dp[i][j] = v
    print(min(dp[n]))

main()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    inf = 10 ** 12
    n, k = map(int, input().split())
    hh = [0] + list(map(int, input().split()))
    dp = [[0] * (k + 1) for _ in range(n + 1)]
    for i in range(1, n + 1):
        h = hh[i]
        for j in range(k + 1):
            v = inf
            for nd in range(k + 1):
                if i - 1 - nd < 0 or j - nd < 0: break
                v = min(v, dp[i - 1 - nd][j - nd] + max(0, h - hh[i - 1 - nd]))
            dp[i][j] = v
    print(min(dp[n]))

main()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    inf = 10 ** 12
    n, k = map(int, input().split())
    hh = [0] + list(map(int, input().split()))
    dp = [[0] * (k + 1) for _ in range(n + 1)]
    for i in range(1, n + 1):
        h = hh[i]
        for j in range(k + 1):
            v = inf
            for nd in range(k + 1):
                if i - 1 - nd < 0 or j - nd < 0: break
                v = min(v, dp[i - 1 - nd][j - nd] + max(0, h - hh[i - 1 - nd]))
            dp[i][j] = v
    print(dp[n][k])

main()

Submission status: Wrong Answer

Solution code:
def solve(n, k, h_list):
    # dp table
    large = sum(h_list) + 1
    dp = [[large] * n for _ in range(n + 1)]
    dp[1][0] = h_list[0]
    # update
    for i in range(1, n):
        for s in range(2, n + 1):
            dp[s][i] = min([dp[s - 1][j] + max(0, h_list[i] - h_list[j]) for j in range(i)])
        dp[1][i] = h_list[i]
    #print(dp)
    return min(dp[n - k])


def main():
    n, k = map(int, input().split())
    h_list = list(map(int, input().split()))
    res = solve(n, k, h_list)
    print(res)


def test():
    assert solve(4, 1, [2, 3, 4, 1]) == 3
    assert solve(6, 2, [8, 6, 9, 1, 2, 1]) == 7
    assert solve(10, 0, [1, 1000000000, 1, 1000000000, 1, 1000000000, 1, 1000000000, 1, 1000000000]) == 4999999996


if __name__ == "__main__":
    # test()
    main()