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())
dp=[]
for i in range(n+2):
  dp.append([])
  for j in range(n+2):
    dp[-1].append([])
    for l in range(k+1):
      dp[-1][-1].append(10**12)
for j in range(n+2):
  for l in range(k+1):
    dp[0][j][l]=0
h=list(map(int,input().split()))
h.append(0)
h.reverse()
h.append(0)
h.reverse()
for i in range(1,n+2):
  for j in range(n+2):
    for l in range(k+1):
      dp[i][i][l]=min(dp[i][i][l],dp[i-1][j][l]+max(0,h[j]-h[i]))
      if l>0:
        dp[i][j][l]=min(dp[i][j][l],dp[i-1][j][l-1])
        dp[i][0][l]=min(dp[i][0][l],dp[i-1][j][l-1]+h[j])
ans=dp[n+1][0][0]
for j in range(n+2):
  for l in range(k+1):
    ans=min(ans,dp[n+1][j][l])
print(ans)

Submission status: Wrong Answer

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

H = list(map(int,input().split()))

HSorted = sorted(H)

HIndexToHeight = []
HHeightToIndex = {}

for i in range(N):
    if i != 0 and HSorted[i] == HSorted[i-1]:
        continue
    HIndexToHeight.append(HSorted[i])
    HHeightToIndex[HSorted[i]] = len(HIndexToHeight) - 1

INF = 10 ** 12

heightCount = len(HIndexToHeight)

DP = []
for i in range(N):
    DP.append([])
    for j in range(K + 1):
        DP[i].append([])
        for k in range(heightCount):
            DP[i][j].append(INF)

for k in range(heightCount):
    if k == HHeightToIndex[H[0]]:
        DP[0][0][k] = H[0]
    elif K != 0:
        DP[0][1][k] = HIndexToHeight[k]

for i in range(1,N):
    currentHeight = HHeightToIndex[H[i]]
    for j in range(K + 1):
        for k in range(heightCount):
            if currentHeight > k:
                if j > 0:
                    DP[i][j][k] = min(DP[i-1][j-1][k], DP[i][j][k])
                DP[i][j][currentHeight] = min(DP[i-1][j][k] + HIndexToHeight[currentHeight] - HIndexToHeight[k], DP[i][j][currentHeight])
            elif currentHeight == k:
                DP[i][j][currentHeight] = min(DP[i-1][j][k], DP[i][j][currentHeight])
            else:
                if j > 0:
                    DP[i][j][k] = min(DP[i-1][j-1][k], DP[i][j][k])
                DP[i][j][currentHeight] = min(DP[i-1][j][k], DP[i][j][currentHeight])
ans = INF

for j in range(K + 1):
    for k in range(heightCount):
        if ans > DP[N-1][j][k]:
            ans = DP[N-1][j][k]

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    N, K = map(int, readline().split())
    H = [0] + list(map(int, readline().split())) + [0]

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

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

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

    print(dp[N][N - K])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    N, K = map(int, readline().split())
    H = [0] + list(map(int, readline().split())) + [0]

    ans = 0
    used = [0, N + 1]

    for i in range(1, N - K + 1):
        score = INF
        min_idx = - 1
        for j in range(i):
            l_idx = used[j]
            r_idx = used[j + 1]
            l = H[l_idx]
            r = H[r_idx]
            for k in range(l_idx + 1, r_idx):
                cur = H[k]
                diff = min(cur - l, cur - r)
                if diff < score:
                    score = min(score, diff)
                    min_idx = k
        ans += score
        used.append(min_idx)
        used.sort()
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def main():
    n, k = map(int, input().split())
    h = [0] + list(map(int, input().split()))
    
    dp = [[[0]*2 for _ in range(k+1)] for _ in range(n+1)]
    
    for i in range(n):
        dp[i+1][0][0] += dp[i][0][0]
        if h[i] < h[i+1]:
            dp[i+1][0][0] += h[i+1] - h[i]
        dp[i+1][0][1] = i+1
    
    for i in range(1, k+1):
        for j in range(i, n):
            sub, bef = dp[j][i]
            if h[bef] < h[j+1]:
                sub += h[j+1] - h[bef]
            
            if sub < dp[j][i-1][0]:
                dp[j+1][i][0] = sub
                dp[j+1][i][1] = j+1
            elif sub > dp[j][i-1][0]:
                dp[j+1][i][0] = dp[j][i-1][0]
                dp[j+1][i][1] = dp[j][i-1][1]
            else:
                dp[j+1][i][0] = sub
                if h[j+1] < h[dp[j][i-1][1]]:
                    dp[j+1][i][1] = dp[j][i-1][1]
                else:
                    dp[j+1][i][1] = j+1

    print(dp[n][k][0])
    
    
    
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

def main():
    n, k = map(int, input().split())
    h = [0] + list(map(int, input().split()))
    
    dp = [[[0]*2 for _ in range(k+1)] for _ in range(n+1)]
    
    for i in range(n):
        dp[i+1][0][0] += dp[i][0][0]
        if h[i] < h[i+1]:
            dp[i+1][0][0] += h[i+1] - h[i]
        dp[i+1][0][1] = i+1
    
    for i in range(1, k+1):
        for j in range(i, n):
            sub, bef = dp[j][i]
            if h[bef] < h[j+1]:
                sub += h[j+1] - h[bef]
            
            if sub < dp[j][i-1][0]:
                dp[j+1][i][0] = sub
                dp[j+1][i][1] = j+1
            elif sub > dp[j][i-1][0]:
                dp[j+1][i][0] = dp[j][i-1][0]
                dp[j+1][i][1] = dp[j][i-1][1]
            else:
                dp[j+1][i][0] = sub
                if h[j+1] > h[dp[j][i-1][1]]:
                    dp[j+1][i][1] = dp[j][i-1][1]
                else:
                    dp[j+1][i][1] = j+1

    print(dp[n][k][0])
    
    
    
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

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

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

dp = [[10**9*N for _ in range(N-K+2)] for _ in range(N+1)]

dp[0][0] = 0
for i, h in enumerate(H[1:], start=1):
    for k in range(1, min2(i+1, N-K+1)):
        dp[i][k] = min(dp[j][k-1] + max2(0, h-H[j]) for j in range(i))
print(min(dp[-1][N-K:]))

Submission status: Wrong Answer

Solution code:
def examA():
    N = DI()/dec(7)
    ans = N
    print(N)
    return

def examB():
    ans = 0
    print(ans)
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    N, K = LI()
    H = LI()
    H.insert(0,0)
    dp = [[[inf]*(K+1)for _ in range(N+1)] for _ in range(N+1)]
    dp[0][0][0] = 0
    for i in range(N):
        for j in range(i+1):
            for k in range(K+1):
                dp[i+1][i+1][k] = min(dp[i+1][i+1][k],dp[i][j][k]+max(0,H[i+1]-H[j]))
                if k<K:
                    dp[i+1][j][k+1] = min(dp[i+1][j][k+1],dp[i][j][k])

    ans = dp[-1][-1][-1]
    #for v in dp:
    #    print(v)
    print(ans)
    return

from decimal import getcontext,Decimal as dec
import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(input())
def LI(): return list(map(int,sys.stdin.readline().split()))
def DI(): return dec(input())
def LDI(): return list(map(dec,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = dec("0.000000000001")
alphabet = [chr(ord('a') + i) for i in range(26)]
alphabet_convert = {chr(ord('a') + i): i for i in range(26)}

getcontext().prec = 28

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examF()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
h = [0] + list(map(int, input().split()))
inf = 10 ** 13
dp = [[0] * (k + 1)]
for i in range(1, n + 1):
    dp.append([inf] * (k + 1))
    for j in range(i):
        for u in range(i - j - 1, k + 1):
            dp[-1][u] = min(dp[-1][u], dp[j][u - i + j + 1] + max(h[i] - h[j], 0))
print(min(dp[-1]))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
h = list(map(int, input().split()))
inf = 10 ** 13
dp = [[h[0]] * (k + 1)]
for i in range(1, n):
    dp.append([inf] * (k + 1))
    for j in range(i):
        for u in range(i - j - 1, k + 1):
            dp[-1][u] = min(dp[-1][u], dp[j][u - i + j + 1] + max(h[i] - h[j], 0))
print(min(dp[-1]))

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+1)] #dp[i][j]右端がi、j+1本残す
mn = h[0]
for i in range(n):
    #mn = min(mn,h[i])
    dp[i][0] = h[i]#min(mn,h[i])

for i in range(1,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))

ans = inf 

for i in range(n):
    ans = min(ans, min(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+1)] #dp[i][j]右端がi、j+1本残す
mn = h[0]
for i in range(n):
    #mn = min(mn,h[i])?
    dp[i][0] = h[i]#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))

ans = inf 

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N,K=map(int,input().split())
    N+=2
    H=[0]+list(map(int,input().split()))+[0]
    edge=[[] for _ in range(N*(K+1))]
    for i in range(N):
        for j in range(K+1):
            f=0
            for k in range(K+1-j):
                if i+k+1<N and N-1-i>=K-(k+j):
                    if k==0 or f:
                        edge[i*(K+1)+j].append((max(H[i+k+1]-H[i],0),(i+k+1)*(K+1)+j+k))
                    if H[i]<H[i+k+1]:
                        f=1
    from heapq import heappop,heappush
    def dijkstra(start,n,edges):
        d=[INF]*n
        used=[False]*n
        d[start]=0
        used[start]=True
        edgelist=[]
        for edge in edges[start]:
            heappush(edgelist,edge)
        while edgelist:
            minedge=heappop(edgelist)
            if used[minedge[1]]:
                continue
            v=minedge[1]
            d[v]=minedge[0]
            used[v]=True
            for edge in edges[v]:
                if not used[edge[1]]:
                    heappush(edgelist,(edge[0]+d[v],edge[1]))
        return d
    d=dijkstra(0,N*(K+1),edge)
    print(min(d[(N-1)*(K+1):]))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N,K=map(int,input().split())
    N+=1
    H=[0]+list(map(int,input().split()))
    edge=[[] for _ in range(N*(K+1))]
    for i in range(N):
        for j in range(K+1):
            for k in range(K+1-j):
                if i+k+1<N:
                    edge[i*(K+1)+j].append((max(H[i+k+1]-H[i],0),(i+k+1)*(K+1)+j+k))
    from heapq import heappop,heappush
    def dijkstra(start,n,edges):
        d=[INF]*n
        used=[False]*n
        d[start]=0
        used[start]=True
        edgelist=[]
        for edge in edges[start]:
            heappush(edgelist,edge)
        while edgelist:
            minedge=heappop(edgelist)
            if used[minedge[1]]:
                continue
            v=minedge[1]
            d[v]=minedge[0]
            used[v]=True
            for edge in edges[v]:
                if not used[edge[1]]:
                    heappush(edgelist,(edge[0]+d[v],edge[1]))
        return d
    d=dijkstra(0,N*(K+1),edge)
    print(min(d[(N-1)*(K+1):N*(K+1)]))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

dp1=[(10**18,0)for i in range(k+2)]
dp2=[(10**18,0)for i in range(k+2)]
dp1[0]=(0,0)
dp2[0]=(0,0)

for t,h in enumerate(A):
    for i in range(min(t+1,k),-1,-1):
        ca1,la1=dp1[i]
        cb1,lb1=dp2[i]
        ca2,la2=dp1[i-1]
        cb2,lb2=dp2[i-1]
        #使わない
        t1=(ca1+h-la1,h) if la1<h else (ca1,h)
        t2=(cb1+h-lb1,h) if lb1<h else (cb1,h)
        #使う
        t3=(ca2,min(h,la2))
        t4=(cb2,min(h,lb2))
        T=[t1,t2,t3,t4]
        dp1[i]=min(T,key=lambda x: (x[0],-x[1]))
        dp2[i]=min(T,key=lambda x: (-x[1],x[0]))

dp=[dp1[i][0] for i in range(k+1)]+[dp2[i][0] for i in range(k+1)]
print(min(dp))

"""
0 2
100 100000 1000021 100

"""

Submission status: Wrong Answer

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

dp1=[(10**18,0)for i in range(k+2)]
dp2=[(10**18,0)for i in range(k+2)]
dp1[0]=(0,0)
dp2[0]=(0,0)

for t,h in enumerate(A):
    for i in range(min(t+1,k),-1,-1):
        ca1,la1=dp1[i]
        cb1,lb1=dp2[i]
        ca2,la2=dp1[i-1]
        cb2,lb2=dp2[i-1]
        #使わない
        t1=(ca1+h-la1,h) if la1<h else (ca1,h)
        t2=(cb1+h-lb1,h) if lb1<h else (cb1,h)
        #使う
        t3=(ca2,min(h,la2))
        t4=(cb2,min(h,lb2))
        T=[t1,t2,t3,t4]
        dp1[i]=min(T,key=lambda x: (x[0],-x[1]))
        dp2[i]=min(T,key=lambda x: (-x[1],x[0]))

print(min(dp1[k][0],dp2[k][0]))

"""
0 2
100 100000 1000021 100

"""

Submission status: Wrong Answer

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

dp1=[(10**18,0)for i in range(k+2)]
dp2=[(10**18,0)for i in range(k+2)]
dp1[0]=(0,0)
dp2[0]=(0,0)

for t,h in enumerate(A):
    for i in range(min(t+1,k),-1,-1):
        ca1,la1=dp1[i]
        cb1,lb1=dp2[i]
        ca2,la2=dp1[i-1]
        cb2,lb2=dp2[i-1]
        #使わない
        t1=(ca1+h-la1,h) if la1<h else (ca1,h)
        t2=(cb1+h-lb1,h) if lb1<h else (cb1,h)
        #使う
        t3=(ca2,min(h,la2))
        t4=(cb2,min(h,lb2))
        T=[t1,t2,t3,t4]
        dp1[i]=min(T,key=lambda x: x[0]*10**9-x[1])
        dp2[i]=min(T,key=lambda x: -x[1]*10**12+x[0])

print(min(dp1[k][0],dp2[k][0]))

Submission status: Wrong Answer

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

def main():
    N,K = map(int,input().split())
    H = list(map(int,input().split()))
    H = [0]+H
    INF = 10**10
    
    dp = [INF]*(N+1)
    dp[0] = 0
    for i in range(N-K):
        use = [INF]*(N+1)
        for j in range(i,N+1):
            for k in range(j+1,N+1):
                use[k] = min(use[k],dp[j]+max(0,H[k]-H[j]))
        dp = use
    
    print(dp[-1])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,K = map(int,input().split())
a = [0]+list(map(int,input().split()))+[0]
INF = float("inf")
dp = [[[INF for i in range(K+1)] for j in range(n+1)] for k in range(n+1)]
dp[0][0][0] = 0
mx = 0
for i in range(1,n+1):
  x = a[i]
  for j in range(i):
    for k in range(1,min(K,i)+1):
      dp[i][j][k] = dp[i-1][j][k-1]
  for k in range(min(K,i)+1):
    for l in range(1,i+1):
      dp[i][i][k] = min(dp[i][i][k],dp[i-1][i-l][k]+max(0,x-a[i-l]))
print(dp[n][n][K])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = [0]+list(map(int,input().split()))
dp = [[(10**18,0) for i in range(k+1)] for j in range(n+1)]
dp[0][0] = (0,0)
mx = 0
for i in range(1,n+1):
  x = a[i]
  dp[i][0] = (dp[i-1][0][0]+max(0,a[i]-a[i-1]),x)
  for j in range(1,k+1):
    pr = dp[i-1][j][1]
    y = max(0,x-pr)
    if dp[i-1][j][0]+y > dp[i-1][j-1][0] or (dp[i-1][j][0]+y == dp[i-1][j-1][0] and dp[i-1][j-1][1] > x):
      dp[i][j] = dp[i-1][j-1][:]
    else:
      dp[i][j] = (dp[i-1][j][0]+y,x)
print(dp[n][k][0])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = [0]+list(map(int,input().split()))
dp = [[(10**18,0) for i in range(k+1)] for j in range(n+1)]
dp[0][0] = (0,0)
mx = 0
for i in range(1,n+1):
  x = a[i]
  dp[i][0] = (dp[i-1][0][0]+max(0,a[i]-a[i-1]),x)
  for j in range(1,k+1):
    pr = dp[i-1][j][1]
    y = max(0,x-pr)
    if dp[i-1][j][0]+y >= dp[i-1][j-1][0]:
      dp[i][j] = dp[i-1][j-1][:]
    else:
      dp[i][j] = (dp[i-1][j][0]+y,x)
print(dp[n][k][0])

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = [0]+list(map(int,input().split()))
dp = [[(10**18,0) for i in range(k+1)] for j in range(n+1)]
dp[0][0] = (0,0)
mx = 0
for i in range(1,n+1):
  x = a[i]
  dp[i][0] = (dp[i-1][0][0]+max(0,a[i]-a[i-1]),x)
  for j in range(1,k+1):
    pr = dp[i-1][j][1]
    y = max(0,x-pr)
    if dp[i-1][j][0]+y > dp[i-1][j-1][0]:
      dp[i][j] = (dp[i-1][j-1][0],dp[i-1][j-1][1])
    else:
      dp[i][j] = (dp[i-1][j][0]+y,x)
print(dp[n][k][0])

Submission status: Wrong Answer

Solution code:
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

n,K = mi()
hlis = li()

inf = 10 **13 + 7
dp = [[inf]*n for _ in range(n)]
for i in range(n):
    dp[0][i] = hlis[i]

for i in range(1,n):
    for k in range(i,n):
        #dp[i][k] = min(dp[i][k],dp[i-1][l] + abs)
        for l in range(k):
            dp[i][k] = min(dp[i][k],dp[i-1][l] + max(0,hlis[k] - hlis[l]))

tukau = n - K
ans = inf
if K == n:
    print(0)
    exit()
for i in range(K,n):
    ans = min(ans,dp[tukau-1][i])
print(ans)

Submission status: Wrong Answer

Solution code:
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

n,K = mi()
hlis = li()

inf = 10 **13 + 7
dp = [[inf]*n for _ in range(n)]
for i in range(n):
    dp[0][i] = hlis[i]

for i in range(1,n):
    for k in range(i,n):
        #dp[i][k] = min(dp[i][k],dp[i-1][l] + abs)
        for l in range(k):
            dp[i][k] = min(dp[i][k],dp[i-1][l] + max(0,hlis[k] - hlis[l]))

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

Submission status: Wrong Answer

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

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

inf = 10**15 * (n+1)
dp = [[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):
        if(j==1):
            dp[i][j] = h[i-1]
        else:
            tmp = inf
            for l in range(1,i):
                tmp = min(tmp, dp[l][j-1] + max(0, h[i-1]- h[l-1])  )
            dp[i][j] = tmp

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

Submission status: Wrong Answer

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

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

inf = 10**9 * (n+1)
dp = [[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):
        if(j==1):
            dp[i][j] = h[i-1]
        else:
            tmp = inf
            for l in range(1,i):
                tmp = min(tmp, dp[l][j-1] + max(0, h[i-1]- h[l-1])  )
            dp[i][j] = tmp

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

Submission status: Wrong Answer

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

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

inf = 10**9 * (n+1)
dp = [[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):
        if(j==1):
            dp[i][j] = h[i-1]
        else:
            tmp = inf
            for l in range(0,i):
                tmp = min(tmp, dp[l][j-1] + max(0, h[i-1]- h[l-1])  )
            dp[i][j] = tmp

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

Submission status: Wrong Answer

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

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

inf = 10**9 * (n+1)
dp = [[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):
        tmp = inf
        for l in range(0,i):
            tmp = min(tmp, dp[l][j-1] + max(0, h[i]- h[l])  )
        dp[i][j] = tmp

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

Submission status: Wrong Answer

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

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

def calc(h_ind):
    hlen = len(h_ind)
    ans = h[h_ind[0]]
    for i in range(1,hlen):
        ans += max(h[h_ind[i]]-h[h_ind[i-1]], 0)

    return ans

inf = 10**9 * (n+1)
dp = [[inf] * (n-k+1) for _ in range(n+1)]
dp_element = []
for i in range(n+1):
    tmp = [[] for _ in range(n-k+1)]
    dp_element.append(tmp)
dp[0][0] = 0

for i in range(1,n+1):
    for j in range(1,n-k+1):
        # print('{} {}'.format(i,j))
        h_ind = dp_element[i-1][j-1][::]
        if(len(h_ind) != j-1):
            continue
        h_ind.append(i-1)
        tmp = calc(h_ind)
        if( dp[i-1][j] < tmp):
            dp[i][j] = dp[i-1][j]
            dp_element[i][j] = dp_element[i-1][j][::]
        elif(dp[i-1][j] == tmp):
            h_ind_b = dp_element[i-1][j][-1]
            h_ind_n = h_ind[-1]
            if(h[h_ind_b] < h[h_ind_n]):
                dp[i][j] = dp[i-1][j]
                dp_element[i][j] = dp_element[i-1][j][::]
            else:
                dp[i][j] = tmp
                dp_element[i][j] = h_ind[::]

        else:
            dp[i][j] = tmp
            dp_element[i][j] = h_ind[::]

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

Submission status: Wrong Answer

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

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

def calc(h_ind):
    hlen = len(h_ind)
    ans = h[h_ind[0]]
    for i in range(1,hlen):
        ans += max(h[h_ind[i]]-h[h_ind[i-1]], 0)

    return ans

inf = 10**9 * (n+1)
dp = [[inf] * (n-k+1) for _ in range(n+1)]
dp_element = []
for i in range(n+1):
    tmp = [[] for _ in range(n-k+1)]
    dp_element.append(tmp)
dp[0][0] = 0

for i in range(1,n+1):
    for j in range(1,n-k+1):
        # print('{} {}'.format(i,j))
        h_ind = dp_element[i-1][j-1][::]
        if(len(h_ind) != j-1):
            continue
        h_ind.append(i-1)
        tmp = calc(h_ind)
        if( dp[i-1][j] < tmp):
            dp[i][j] = dp[i-1][j]
            dp_element[i][j] = dp_element[i-1][j][::]
        elif(dp[i-1][j] == tmp):
            h_ind_b = dp_element[i-1][j][-1]
            h_ind_n = h_ind[-1]
            if(h[h_ind_b] > h[h_ind_n]):
                dp[i][j] = dp[i-1][j]
                dp_element[i][j] = dp_element[i-1][j][::]
            else:
                dp[i][j] = tmp
                dp_element[i][j] = h_ind[::]

        else:
            dp[i][j] = tmp
            dp_element[i][j] = h_ind[::]

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

Submission status: Wrong Answer

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

def calc(h_ind):
    hlen = len(h_ind)
    ans = h[h_ind[0]]
    for i in range(1,hlen):
        ans += max(h[h_ind[i]]-h[h_ind[i-1]], 0)

    return ans

inf = 10**9 * (n+1)
dp = [[inf] * (n-k+1) for _ in range(n+1)]
dp_element = []
for i in range(n+1):
    tmp = [[] for _ in range(n-k+1)]
    dp_element.append(tmp)
dp[0][0] = 0

for i in range(1,n+1):
    for j in range(1,n-k+1):
        # print('{} {}'.format(i,j))
        h_ind = dp_element[i-1][j-1][::]
        if(len(h_ind) != j-1):
            continue
        h_ind.append(i-1)
        tmp = calc(h_ind)
        if( dp[i-1][j] < tmp):
            dp[i][j] = dp[i-1][j]
            dp_element[i][j] = dp_element[i-1][j][::]
        else:
            dp[i][j] = tmp
            dp_element[i][j] = h_ind[::]

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

Submission status: Wrong Answer

Solution code:
n,K=map(int,input().split())
l=list(map(int,input().split()))
INF=float('inf')
dp=[[[INF,INF] for j in range(K+1)] for i in range(n+1)]
dp[0][0]=[0,0]
for i,j in enumerate(l,1):
    a1,b1=dp[i-1][0]
    a1+=max(0,j-b1)
    dp[i][0]=[a1,j]
for i,j in enumerate(l,1):
    for k in range(1,min(K+1,i+1)):
        a1,b1=dp[i-1][k]
        a1+=max(0,j-b1)
        a2,b2=dp[i-1][k-1]
        dp[i][k]=sorted([[a1,j],[a2,b2]])[0]
print(dp[-1][-1][0])

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+1)] #dp[i][j]右端がi、j+1本残す
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))

ans = inf 

for i in range(n):
    ans = min(ans, min(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+1)] #dp[i][j]右端がi、j+1本残す
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):#本数+1
        for t in range(i):#右から2番目,+1
            dp[i][j] = min(dp[i][j], dp[t][j-1] + max(h[i]-h[t], 0))

ans = inf 

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

Submission status: Wrong Answer

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

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

graph = [[] for _ in range(N+2)]
for i, a in enumerate(A):
    if i == N+1: break
    r = i+1
    while r != N+1 and A[r] >= a:
        r += 1
    l = i-1
    while l != -1 and A[l] > a:
        l -= 1
    if A[l] == a: graph[l].append((i, 0))
    else:
        if A[l] <= A[r]:
            graph[r].append((i, a-A[r]))
        elif A[l] > A[r]:
            graph[l].append((i, a-A[l]))


stack = [N]
Ind = [0]*(N+1)
dp = [[0] for _ in range(N+1)]
while stack:
    p = stack[-1]
    if Ind[p] == len(graph[p]):
        stack.pop()
        if len(graph[p]) == 0: continue
        elif len(graph[p]) == 1:
            ch, w = graph[p][0]
            P = dp[ch]
            if len(P) < K+1:
                dp[p] = P + [P[-1] + w]
            else:
                dp[p] = P[:]
        else:
            ch1, w1 = graph[p][0]
            ch2, w2 = graph[p][1]
            m = 0
            if len(dp[ch1]) < K+1:
                dp[ch1].append(dp[ch1][-1]+w1)
            if len(dp[ch2]) < K+1:
                dp[ch2].append(dp[ch2][-1]+w2)
            for k in range(1, min(K+1, len(dp[ch1])+len(dp[ch2])-1)):
                for i in range(k+1):
                    tmp = 0
                    if i < len(dp[ch1]):
                        tmp += dp[ch1][i]
                    if k-i < len(dp[ch2]):
                        tmp += dp[ch2][k-i]
                    m = max(m, tmp)
                dp[p].append(m)
    elif len(stack) > 1 and stack[-2] == graph[p][Ind[p]][0]:
        Ind[p] += 1
    else:
        stack.append(graph[p][Ind[p]][0])
        Ind[p] += 1

ans = 0
for i in range(N+1):
    for _, w in graph[i]:
        ans += w

ans -= dp[N][-1]
print(ans)

Submission status: Wrong Answer

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

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

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

    if N == K:
        return 0

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

    return dp[-1][-1]

ans = solve()
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] = mn

for i in range(n):##右端
    for j in range(1,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)