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: Accepted

Solution code:
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
# MOD = 998244353

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

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

# H = np.array(H, dtype=float)

# # @debug
# def count(heights, changes):
#     H = heights.copy()
#     H = H[~changes]
#     ret = 0
#     cnt = 1
#     height = H.min()
#     # 普通に数えてみる
#     while height != np.inf:
#         H -= height
#         ret += cnt * height
#         H[H == 0.0] = np.inf
#         cnt = np.count_nonzero(np.diff((H != np.inf).astype(int)) == 1)
#         cnt += H[0] != np.inf
#         height = H.min()
#     return int(ret)
#
#
# # 上から何個変えるか、下から何個変えるかを全探索
# ans = INF
# orders = H.argsort()
# for _ in range(K + 1):
#     # 変えるやつ
#     changes = np.zeros(N, dtype=bool)
#     changes[orders[:K]] = True
#     ans = min(ans, count(H, changes))
#     orders = np.roll(orders, shift=1)
# print(ans)


# dp[i][k][h]: i まで見た、k 個無視した、今の高さが h であるときの塗る数
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))

Submission status: Accepted

Solution code:
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)))

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

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

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)

Submission status: Accepted

Solution code:
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)

Submission status: Runtime Error

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]
    else:
        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: Runtime Error

Solution code:
n,k = map(int, input().split( ))
if n==k:
    print(0)
    exit()
#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: Runtime Error

Solution code:
from time import time

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

def dpinit(ps, val=0):
    res = [val for i in [0]*ps[-1]]
    for i in ps[:-1][::-1]:
        res = [res.copy() for k in [0]*i]
    return res

H = [0] + H + [0]
dp = dpinit((N+2,K+2))

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

print(dp[N+1][K])

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
def main():
    import numpy as np
    n, K = map(int, input().split())
    H = list(map(int, input().split()))
    s = np.array(sorted(set(H+[0])))
    d = {i: j for j, i in enumerate(s)}
    l = len(s)
    a = max(s)-s
    dp = np.full((K+1, l), 10**13, dtype=np.int64)
    dp[0, 0] = 0
    for h in H:
        dh = d[h]
        dp2 = np.full((K+1, l), 10**13, dtype=np.int64)
        t = np.zeros((K, l), dtype=np.int64)
        s_plus_a = a+dp
        min_s = np.array([[j for j in i] for i in dp[:-1]], dtype=np.int64)
        for j in range(l-2, -1, -1):
            min_s[:, j] = np.minimum(min_s[:, j+1], min_s[:, j])
        for j in range(1, l):
            s_plus_a[:, j] = np.minimum(s_plus_a[:, j-1], s_plus_a[:, j])
        t = s_plus_a[:-1] - a
        dp2[1:, :] = np.minimum(min_s, t)
        for j in range(l):
            dp2[:, dh] = np.minimum(dp2[:, dh], dp[:, j]+max(h-s[j], 0))
        dp = dp2
    print(np.min(dp))


main()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, m=DVSR): return pow(x, m - 2, m)
def DIV(x, y, m=DVSR): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())
def FLIST(n):
    res = [1]
    for i in range(1, n+1): res.append(res[i-1]*i%DVSR)
    return res


N,K=LI()
MP=[0] + LI()
LG = 10**15
DP=[[LG]*(N+1) for i in range(N-K+1)]
DP[0][0] = 0

for i in range(1, N+1):
    DP[1][i] = MP[i]

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

res = 10**15
for i in range(1, N+1):
    res = min(res, DP[N-K][i])
print(res)

Submission status: Runtime Error

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

dp = [[0 for _ in range(n)] for _ in range(n-k)]

dp[0] = [] + h

for x in range(1, n-k):
    for y in range(n):
        temp = float('inf')
        for i in range(y):
            temp = min(temp, dp[x-1][i] + max(0, h[y] - h[i]))
        dp[x][y] = temp

#for x in dp: print(x)

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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)
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: Runtime Error

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)]
dp[1][1]=G[1]
for jtemp in range(N-1):
    j=jtemp+2
    dp[1][j]=G[j]#min(dp[1][j-1],G[j])
tempa=0
if K<N-1:
    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: Runtime Error

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)]
dp[1][1]=G[1]
for jtemp in range(N-1):
    j=jtemp+2
    dp[1][j]=G[j]#min(dp[1][j-1],G[j])
if K<N-1:
    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
    temp=dp[N-K][-1]
    for jtemp in range(N):
        j=jtemp+1
        if temp>dp[N-K][j] and dp[N-K][j]!=0:
            temp=dp[N-K][j]
elif K==1:
    temp=min(H[:])
else:
    temp=0
print(temp)

Submission status: Runtime Error

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)]
dp[1][1]=G[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
temp=dp[N-K][-1]
for jtemp in range(N):
    j=jtemp+1
    if temp>dp[N-K][j] and dp[N-K][j]!=0:
        temp=dp[N-K][j]
print(temp)

Submission status: Runtime Error

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

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)
[h.pop(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(h)
print(count)

Submission status: Runtime Error

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

l=[]

for i in range(n):
    fast = max(h)
    last = max(h)
    if 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)
[h.pop(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(h)
print(count)

Submission status: Runtime Error

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

l=[]

for i in range(n):
    fast = max(h)
    last = max(h)
    if 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]])
print(l)
l = sorted(l, key=lambda x:(x[0],x[2]), reverse=True)
[h.pop(s[1]) for s in l[:k]]

# print(h)

def painting(h):
    count = min(h)
    
    print("h : ",h, " count : ", count)
    h = [s - min(h) for s in h]
    # l = sorted(l, key=lambda x:x[1])
    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 i == 0:
        #     b = l[:c[1]]
        # elif i == len(l)-1:
        #     b = l[ l[i][1] + 1 : ]
        # else:
        #     b = l[ l[i][1] + 1 : l[i+1][1] ]
        if not b == []:
            count += painting(b)
    return count

count = 0
count = painting(h)
print(count)

Submission status: Runtime Error

Solution code:

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

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


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

for i in range(N):

    nh = H[i]

    dp[i][1] = nh

    for j in range(i):
        for k in range(N-K):

            if dp[j][k] != None:

                if dp[i][k+1] == None:
                    
                    dp[i][k+1] = dp[j][k] + max(0, H[i]-H[j])

                else:

                    dp[i][k+1] = min(dp[i][k+1] , dp[j][k] + max(0, H[i]-H[j]))

ans = float("inf")
for i in range(N):

    if dp[i][-1] != None:
        ans = min(ans,dp[i][-1])

print (ans)

Submission status: Runtime Error

Solution code:
N,T=map(int,input().split())
x=[]
for i in range(N):
    Ai,Bi=map(int,input().split())
    x.append((Ai,Bi))

x.sort(key=lambda x:(x[1],x[0]))

dp=[[0 for j in range(T)] for i in range(N)] # i番目までの料理（最後の料理を含まない）でj分後（<=T-1分後）までに得られる満足度の最大値（単純dp）

# 単純dp
for i in range(N-1):
    for j in range(T):
        if x[i][0]>j:
            dp[i+1][j]=dp[i][j]
        else:
            dp[i+1][j]=max(dp[i][j],dp[i][j-x[i][0]]+x[i][1])

print(dp[N-1][T-1]+x[-1][1])

Submission status: Runtime Error

Solution code:
n, k=map(int, input().split())
hlist = list(map(int, input().split()))
cost=[]
dead=[0]*n
if n!=1:
    for i in range(n):
        if i==0:
            if hlist[i]>=hlist[i+1]:
                cost.append(hlist[i]-hlist[i+1])
            else:
                cost.append(0)
        elif i==n-1:
            if hlist[i]>=hlist[i-1]:
                cost.append(hlist[i]-hlist[i-1])
            else:
                cost.append(0)
        else:
            if hlist[i]>=hlist[i-1] and hlist[i]>=hlist[i+1]:
                cost.append(hlist[i]-max(hlist[i-1], hlist[i+1]))
            elif hlist[i]<=hlist[i-1] and hlist[i]<=hlist[i+1]:
                cost.append(min(hlist[i-1], hlist[i+1]) - hlist[i])
            else:
                cost.append(0)
    
    #print(cost)
    rest=k
    while rest>0:
        bango=cost.index(max(cost))
        hlist=hlist[:bango]+hlist[bango+1:]
        cost=cost[:bango]+cost[bango+1:]
        if len(hlist)>=2:
            for i in [bango-1, bango]:
                if i==0:
                    if hlist[i]>=hlist[i+1]:
                        cost[i]=hlist[i]-hlist[i+1]
                    else:
                        cost[i]=0
                elif i==len(hlist)-1:
                    if hlist[i]>=hlist[i-1]:
                        cost[i]=hlist[i]-hlist[i-1]
                    else:
                        cost[i]=0
                else:
                    if hlist[i]>=hlist[i-1] and hlist[i]>=hlist[i+1]:
                        cost[i]=hlist[i]-max(hlist[i-1], hlist[i+1])
                    elif hlist[i]<=hlist[i-1] and hlist[i]<=hlist[i+1]:
                        cost[i]=min(hlist[i-1], hlist[i+1]) - hlist[i]
                    else:
                        cost[i]=0
            rest-=1
        else:
            if rest>1:
                hlist=[0]
                break
            else:
                break
        
        #print(cost, hlist)
    
    nul=0
    mae=0
    for i in range(len(hlist)):
        if hlist[i]>=mae:
            nul+=hlist[i]-mae
        mae=hlist[i]
    print(nul)
else:
    if k>=1:
        print(0)
    else:
        print(hlist[0])

Submission status: Runtime Error

Solution code:
from random import random

print(rand())

Submission status: Runtime Error

Solution code:
from random import random
while(x != 1000000)
print(rand())
x += 1

Submission status: Runtime Error

Solution code:
from random import random

for x 
x = rand()
print(x)