Problem:


Score : 300 points

Problem Statement

In a flower bed, there are N flowers, numbered 1,2,......,N. Initially, the heights of all flowers are 0.
You are given a sequence h=\{h_1,h_2,h_3,......\} as input. You would like to change the height of Flower k to h_k for all k (1 \leq  k \leq N), by repeating the following "watering" operation:

Specify integers l and r. Increase the height of Flower x by 1 for all x such that l \leq x \leq r.

Find the minimum number of watering operations required to satisfy the condition.

Constraints

1 \leq N  \leq 100

0 \leq h_i \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
h_1 h_2 h_3 ...... h_N

Output

Print the minimum number of watering operations required to satisfy the condition.

Sample Input 1

4
1 2 2 1

Sample Output 1

2

The minimum number of watering operations required is 2.
One way to achieve it is:

Perform the operation with (l,r)=(1,3).

Perform the operation with (l,r)=(2,4).

Sample Input 2

5
3 1 2 3 1

Sample Output 2

5

Sample Input 3

8
4 23 75 0 23 96 50 100

Sample Output 3

221



Sample input:
4
1 2 2 1


Sample output:
2

Submission status: Runtime Error

Solution code:
N = int(input())
listh = list(map(int, input().split()))
listans = [listh[0]]
for n in range(1,N-1):
    if (listh[n+1]-listh[n])*(listh[n]-listh[n-1])<=0:
        listans.append(listh[n])
listans.append(listh[N-1])        
#print(listans)
l =len(listans)
listans2 = []
for n in range(l-1):
    if listans[n] == listans[n+1]:
        pass
    else:
        listans2.append(listans[n])
listans2.append(listans[l-1])        
#print(listans2)    
L = len(listans2)
#print(L)
listodd = [listans2[x] for x in range(L) if x%2 ==1]
#print(listodd)
listeven = [listans2[x] for x in range(L) if x%2 ==0]
#print(listeven)
if L%2 == 0:
    if listeven[0]>listodd[0]:
        del listodd[-1]
        ans = sum(listeven)-sum(listodd)
        print(ans)

    else:  
        del listeven[0]
        ans = sum(listodd)-sum(listeven)  
        print(ans)

else:     
    if listeven[0]>listodd[0]:
        ans = sum(listeven)-sum(listodd)
        print(ans)

    else:
        del listeven[-1]
        if len(listeven) >= 1:
            del listeven[0]
        ans = sum(listodd)-sum(listeven)
        print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
listh = list(map(int, input().split()))
listans = [listh[0]]
for n in range(1,N-1):
    if (listh[n+1]-listh[n])*(listh[n]-listh[n-1])<=0:
        listans.append(listh[n])
listans.append(listh[N-1])        
#print(listans)
l =len(listans)
listans2 = []
for n in range(l-1):
    if listans[n] == listans[n+1]:
        pass
    else:
        listans2.append(listans[n])
listans2.append(listans[l-1])        
#print(listans2)    
L = len(listans2)
#print(L)
listodd = [listans2[x] for x in range(L) if x%2 ==1]
#print(listodd)
listeven = [listans2[x] for x in range(L) if x%2 ==0]
#print(listeven)
if L%2 == 0:
    if listeven[0]>listodd[0]:
        del listodd[-1]
        ans = sum(listeven)-sum(listodd)
        print(ans)

    else:  
        del listeven[0]
        ans = sum(listodd)-sum(listeven)  
        print(ans)

else:     
    if listeven[0]>listodd[0]:
        ans = sum(listeven)-sum(listodd)
        print(ans)

    else:
        del listeven[-1]
        if len(listeven)>0
        del listeven[0]
        ans = sum(listodd)-sum(listeven)
        print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
listh = list(map(int, input().split()))
listans = [listh[0]]
for n in range(1,N-1):
    if (listh[n+1]-listh[n])*(listh[n]-listh[n-1])<=0:
        listans.append(listh[n])
listans.append(listh[N-1])        
#print(listans)
l =len(listans)
listans2 = []
for n in range(l-1):
    if listans[n] == listans[n+1]:
        pass
    else:
        listans2.append(listans[n])
listans2.append(listans[l-1])        
#print(listans2)    
L = len(listans2)
#print(L)
listodd = [listans2[x] for x in range(L) if x%2 ==1]
#print(listodd)
listeven = [listans2[x] for x in range(L) if x%2 ==0]
#print(listeven)
if L%2 == 0:
    if listeven[0]>listodd[0]:
        del listodd[-1]
        ans = sum(listeven)-sum(listodd)
        print(ans)

    else:  
        del listeven[0]
        ans = sum(listodd)-sum(listeven)  
        print(ans)

else:     
    if listeven[0]>listodd[0]:
        ans = sum(listeven)-sum(listodd)
        print(ans)

    else:   
        del listeven[0]
        del listeven[-1] 
        ans = sum(listodd)-sum(listeven)
        print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

N = int(input())
h = np.array([int(i) for i in input().split()])
flag = [0] * N

thrs = 0

i = 0
ans = 0
l, r = (0, 0)
while sum(flag) != N:
  j = 0
  while j < l:
    if h[j] == 0:
      flag[j] = 1
    j += 1
  
  if h[l] == 0:
    l += 1
    flag[l] = 1
  
  i = 0
  i += l
  r = i
  while h[i] > thrs:
    r += 1
    i += 1
    if i == N:
      break
  
  if l == r:
    continue
  ans += min(h[l:r])
  h[l:r] = h[l:r] - min(h[l:r])
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
h=list(map(int,input().split()))
f=[0]*n
cnt=0
while 1:
    flag=0
    for i in range(n):
        if h[i]!=f[i]:
            f[i]+=1
            flag=1
        elif h[i]==f[i] and flag==1:
            cnt+=1
            flag=0
    if flag:==1
        cnt+=1

    if h == f:
        break

print(cnt)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
cnt=0
#print(cnt,a)
while True:
    my_bool=True
    for i in range(n):
        if my_bool:
            if a[i]!=0:
                my_bool=False
                ch2=True
                a[i]-=1
        else:
            if a[i]!=0:
                a[i]-=1
                ch=True
                ch2=True
            else:
                if ch2:
                    cnt+=1
                ch=False
                ch2=False
    if ch:
        if my_bool:
            pass
        else:
            #print("ch",cnt,a)
            cnt+=1
    #print(cnt,a)
    if my_bool:
        break
print(cnt)

Submission status: Runtime Error

Solution code:
N = int(input())
array = input().split(' ')
harray = []
for z in array:
    z = int(z)
    harray.append(z)
counter = 0
while harray != [0]*N:
    if 0 not in harray:
        a = min(harray)
        for i in range(len(harray)):
            harray[i] -= a
        counter += a
    else:
        zeroarray = [j for j, x in enumerate(harray) if x == 0]
        if len(zeroarray) == 1:
            if zeroarray[0] == 0:
                a = min(harray[1:])
                for y in range(1,N+1):
                    harray[y] -= a
                counter += a
            elif zeroarray[0] == N-1:
                a = min(harray[:N-2])
                for y in range(N-1):
                    harray[y] -= a
                counter += a
            else:
                a = min(harray[:zeroarray[0]])
                for y in range(zeroarray[0]):
                    harray[y] -= a
                counter += a
                a = min(harray[zeroarray[0]+1:])
                for y in range(zeroarray[0]+1,N):
                    harray[y] -= a
                counter += a
        else:
            for k in range(len(zeroarray)-1):
                if zeroarray[0] != 0:
                    a = min(harray[:zeroarray[0]])
                    for y in range(zeroarray[0]):
                        harray[y] -= a
                    counter += a
                if zeroarray[k+1] - zeroarray[k] == 1:
                    continue
                else:
                    a = min(harray[zeroarray[k]+1:zeroarray[k+1]])
                    for y in range(zeroarray[k]+1,zeroarray[k+1]):
                        harray[y] -= a
                    counter += a
                if zeroarray[-1] != N-1:
                    a = min(harray[zeroarray[-1]+1:])
                    for y in range(zeroarray[-1]+1,N):
                        harray[y] -= a
                    counter += a
        

print(counter)

Submission status: Runtime Error

Solution code:
n=int(input())
h=[int(i) for i input().split()]
j=0
ans=0
while True:
  if h[0]>j:
    ans+=1
  for i in range(1,n):
    if h[i-1]<=j and h [i]>j:
      ans+=1
  j+=1
  if j==max(h):
    break
print(ans)

Submission status: Runtime Error

Solution code:
n=ini(input())
h=[int(i) for i input().split()]
j=0
ans=0
while True:
  if h[0]>j:
    ans+=1
  for i in range(1,n):
    if h[i-1]<=j and h [i]>j:
      ans+=1
  j+=1
  if j==max(h):
    break
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
h = list(map(int, input().split()))
h.append(0)

res = 0
while True:
    for i in range(N):
        if h[i] > 0:
            l = i
            break
    for j in range(i+1, N+1):
        if h[j] == 0:
            r = j
            break
    
    x = min(h[l:r])
    for i in range(l, r):
        h[i] -= x
    res += x

    if max(h) == 0:
        break

print(res)

Submission status: Runtime Error

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

counter = 0


while h_L != []:

    for i in range(n):
        if h_L[i] == 0:
            counter += 1
            break
        else:
            h_L[i] -= 1

    if h_L[0] == 0:
        h_L.pop(0)

print(counter-1)

Submission status: Runtime Error

Solution code:
# 2019-11-12 01:01:32(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    n, *h = [int(x) for x in sys.stdin.read().split()]
    if n == 1:
        print(h[0])
        sys.exit()

    mountain = [0]
    valley = [0]
    flat = 0
    i = 0
    while True:
        if h[i] == h[i+1]:
            if i < n-2:
                i += 1
                continue
            else:
                print(h[i])
                sys.exit()
        elif h[i] < h[i+1]:
            break
        else:
            mountain.append(h[0])
            break
    i = -1
    while True:
        if h[i-1] == h[i]:
            if abs(i) < n-1:
                i -= 1
                continue
            else:
                print(h[i])
                sys.exit()
        elif h[i-1] > h[i]:
            break
        else:
            mountain.append(h[-1])
            break
        
    for i in range(1, n-1):
        if flat == 0:
            if h[i-1] < h[i] > h[i+1]:
                mountain.append(h[i])
            elif h[i-1] > h[i] < h[i+1]:
                valley.append(h[i])
            elif h[i-1] < h[i] == h[i+1]:
                flat = 1
            elif h[i-1] > h[i] == h[i+1]:
                flat = -1
        else:
            if flat == 1:
                if h[i] > h[i+1]:
                    mountain.append(h[i])
                    flat = 0
                elif h[i] < h[i+1]:
                    flat = 0
                else:
                    continue
            elif flat == -1:
                if h[i] < h[i+1]:
                    valley.append(h[i])
                    flat = 0
                elif h[i] > h[h+1]:
                    flat = 0
                else:
                    continue

    
    ans = sum(mountain) - sum(valley)
    print(ans)
    

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
# 2019-11-12 01:01:32(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    n, *h = [int(x) for x in sys.stdin.read().split()]
    if n == 1:
        print(h[0])
        sys.exit()
        
    mountain = [0]
    valley = [0]
    flat = 0
    i = 0
    while True:
        if h[i] == h[i+1]:
            if i < n-2:
                i += 1
                continue
            else:
                print(h[i])
                sys.exit()
        elif h[i] < h[i+1]:
            break
        else:
            mountain.append(h[0])
            break
    i = -1
    while True:
        if h[i-1] == h[i]:
            i -= 1
            continue
        elif h[i-1] > h[i]:
            break
        else:
            mountain.append(h[-1])
            break
    for i in range(1, n-1):
        if flat == 0:
            if h[i-1] < h[i] > h[i+1]:
                mountain.append(h[i])
            elif h[i-1] > h[i] < h[i+1]:
                valley.append(h[i])
            elif h[i-1] < h[i] == h[i+1]:
                flat = 1
            elif h[i-1] > h[i] == h[i+1]:
                flat = -1
        else:
            if flat == 1:
                if h[i] > h[i+1]:
                    mountain.append(h[i])
                    flat = 0
                elif h[i] < h[i+1]:
                    flat = 0
                else:
                    continue
            elif flat == -1:
                if h[i] < h[i+1]:
                    valley.append(h[i])
                    flat = 0
                elif h[i] > h[h+1]:
                    flat = 0
                else:
                    continue

    
    ans = sum(mountain) - sum(valley)
    print(ans)
    

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
# 2019-11-12 01:01:32(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    n, *h = [int(x) for x in sys.stdin.read().split()]
    
    mountain = [0]
    valley = [0]
    flat = 0
    i = 0
    while True:
        if i < n-2:
            if h[i] == h[i+1]:
                i += 1
                continue
            elif h[i] < h[i+1]:
                break
            else:
                mountain.append(h[0])
                break
        else:
            if h[i] == h[i+1]:
                print(h[i])
                sys.exit()
    i = -1
    while True:
        if h[i-1] == h[i]:
            i -= 1
            continue
        elif h[i-1] > h[i]:
            break
        else:
            mountain.append(h[-1])
            break
    for i in range(1, n-1):
        if flat == 0:
            if h[i-1] < h[i] > h[i+1]:
                mountain.append(h[i])
            elif h[i-1] > h[i] < h[i+1]:
                valley.append(h[i])
            elif h[i-1] < h[i] == h[i+1]:
                flat = 1
            elif h[i-1] > h[i] == h[i+1]:
                flat = -1
        else:
            if flat == 1:
                if h[i] > h[i+1]:
                    mountain.append(h[i])
                    flat = 0
                elif h[i] < h[i+1]:
                    flat = 0
                else:
                    continue
            elif flat == -1:
                if h[i] < h[i+1]:
                    valley.append(h[i])
                    flat = 0
                elif h[i] > h[h+1]:
                    flat = 0
                else:
                    continue

    
    ans = sum(mountain) - sum(valley)
    print(ans)
    

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
# 2019-11-12 01:01:32(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    n, *h = [int(x) for x in sys.stdin.read().split()]
    
    mountain = []
    valley = []
    flat = 0
    i = 0
    while True:
        if h[i] == h[i+1]:
            i += 1
            continue
        elif h[i] < h[i+1]:
            break
        else:
            mountain.append(h[i])
            break
    i = -1
    while True:
        if h[i-1] == h[i]:
            i -= 1
            continue
        elif h[i-1] > h[i]:
            break
        else:
            mountain.append(h[i])
            break
    for i in range(1, n-1):
        if flat == 0:
            if h[i-1] < h[i] > h[i+1]:
                mountain.append(h[i])
            elif h[i-1] > h[i] < h[i+1]:
                valley.append(h[i])
            elif h[i-1] < h[i] == h[i+1]:
                flat = 1
            elif h[i-1] > h[i] == h[i+1]:
                flat = -1
        else:
            if flat == 1:
                if h[i] > h[i+1]:
                    mountain.append(h[i])
                    flat = 0
                elif h[i] < h[i+1]:
                    flat = 0
                else:
                    continue
            elif flat == -1:
                if h[i] < h[i+1]:
                    valley.append(h[i])
                    flat = 0
                elif h[i] > h[h+1]:
                    flat = 0
                else:
                    continue

    
    ans = sum(mountain) - sum(valley)
    print(ans)
    

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
# 2019-11-12 01:01:32(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    n, *h = (int(x) for x in sys.stdin.read().split())
    
    mountain = []
    valley = []
    flat = 0
    i = 0
    while True:
        if h[i] == h[i+1]:
            i += 1
            continue
        elif h[i] < h[i+1]:
            break
        else:
            mountain.append(h[i])
            break
    i = -1
    while True:
        if h[i-1] == h[i]:
            i -= 1
            continue
        elif h[i-1] > h[i]:
            break
        else:
            mountain.append(h[i])
            break
    for i in range(1, n-1):
        if flat == 0:
            if h[i-1] < h[i] > h[i+1]:
                mountain.append(h[i])
            elif h[i-1] > h[i] < h[i+1]:
                valley.append(h[i])
            elif h[i-1] < h[i] == h[i+1]:
                flat = 1
            elif h[i-1] > h[i] == h[i+1]:
                flat = -1
        else:
            if flat == 1:
                if h[i] > h[i+1]:
                    mountain.append(h[i])
                    flat = 0
                elif h[i] < h[i+1]:
                    flat = 0
            elif flat == -1:
                if h[i] < h[i+1]:
                    valley.append(h[i])
                    flat = 0
                elif h[i] > h[h+1]:
                    flat = 0

    
    ans = sum(mountain) - sum(valley)
    print(ans)
    

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
N = int(input())

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

ans = 0
interval = 0

for i in range(N):
  #区間が足りない時
  if height[i] > interval:
　　#今の区間と必要な区間の差分を取って足し、新たな区間の数を定義する
　　##順番に注意 先にintervalに定義すると、
   ##ansがそれに影響されるためansが差分の分だけ余分にに大きくなる
    ans += height[i] - interval
    interval += height[i] - interval
  #区間が多い時、必要な数に減らす
  else:
    interval = height[i]

print(ans)

Submission status: Runtime Error

Solution code:
N=int(input())
h=list(map(int,input().split()))
p=h[0]
for i in range(N-1):
  if h[i]<h[i+1]:
    p=h[i+1]
  else:
    p=p

q=1
r=0
if p==0:
    print(0)
else:
   while q<=p:
        if h[0]>=q:
            r=r+1
        else:
            r=r
        for i in range(N-1):
              if h[i]<q and h[i+1]>=q:
                r=r+1
              else:
                r=r
    q=q+1

print(r)

Submission status: Runtime Error

Solution code:
N=int(input())
h=list(map(int,input().split()))
s=0 #回数
while sum(h[i] for i in range(N))!=0:
  s+=1
  m=0 #連続
  j=0 #リスト番号
  while j<N:
    if h[j]==0:
      j+=1
    else:
      w=j #wは始まり
      q=j+1
      while h[q]!=0 and q<N:
        m+=1
        j+=1
      for k in range(w,w+m+1):
        h[k]-=1
      break
print(s)

Submission status: Runtime Error

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

res=0


while True:

#       print(H)
        for l in range(N):
                if H[l]!=0:
                        break
        for r in range(l+1,N):
                if H[r]==0:
                        r-=1
                        break

#       print(l,r)

        if l>r:
                break

        if l==r:
                miin=H[l]
        else:
                miin=min(H[l:r])


#       print(min(H[l:r]))

        res+=miin

        if miin == 0:
                break

        for i in range(l,r+1):
                H[i] -= miin


res += H[N-1]
print(res)

Submission status: Runtime Error

Solution code:
N=int(input())
h=[0]+list(map(int,input().split()))
c=0
for i in range(N):
  c+=max(h[i+1]-l[i],0)
print(c)

Submission status: Runtime Error

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

ans=0
while sum(h)>0:
  for i in range(n):
    if i<n-1 and h[i]>0 and h[i+1]==0:
      ans+=1
    else:
      if i==N-1 and h[i]>0:
        ans+=1
    if h[i]>0:
      h[i]-=1
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
h=list(map(int,input().split()))
ans=0
while sum(h)>0:
  for i in range(n):
    if i<n-1 and h[i]>0 and h[i+1]==0:
      ans+=1
    else:
      if i==N-1 and h[i]>0:
        ans+=1
    if h[i]>0:
      h[i]-=1
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
h=list(map(int,input().spliy()))
ans=0
while sum(h)>0:
  for i in range(n):
    if i<n-1 and h[i]>0 and h[i+1]==0:
      ans+=1
    else:
      if i==N-1 and h[i]>0:
        ans+=1
    if h[i]>0:
      h[i]-=1
print(ans)

Submission status: Runtime Error

Solution code:
def countA(A):
    global cnt
    if len(A)==1:
        cnt += A[0]
    else:
        amin = min(A)
        A = [A[i]-amin for i in range(len(A))]
        cnt += amin
        while A:
            if A[0]==0:
                A.pop(0)
            elif A[-1]==0:
                A.pop()
            else:break
        B = []
        for i in range(1,len(A)):
            if A[i]==0 and A[i-1]==0:
                B.append(i)
        C = []
        for i in range(len(A)):
            if i not in B:
                C.append(A[i])
        C0 = []
        for i in range(len(C)):
            if C[i]==0:
                C0.append(i)
        if len(C0)==0 and len(C)>0:
            countA(C)
        else:
            cur = 0
            for c in C0:
                countA(C[cur:c])
                cur = c+1
            countA(C[cur:])
            
N = int(input())
H = list(map(int,input().split()))
cnt = 0
countA(H)
print(cnt)

Submission status: Runtime Error

Solution code:
n = int(input())
h = list(map(int,input().split()))
count = h[0]
for i in range(n-1):
    if h[i+1]>h[i]:
        count += h[i+1]-h[i]
print(cnt)

Submission status: Runtime Error

Solution code:
n = int(input())
h = list(map(int,input().split()))
count = h[0]
for i in range(N-1):
    if h[i+1]>h[i]:
        count += h[i+1]-h[i]
print(cnt)

Submission status: Runtime Error

Solution code:
h = list(map(int, input().split()))
ans = 0
while sum(h) != 0:
    left = float("inf")
    right = float("inf")
    for i in range(n):
        if h[i] > 0:
            left = i
            break
    for j in range(i + 1, n):
        if h[j] <= 0:
            right = j
            break
    if right == float("inf"):
        right = n
    for i in range(left, right):
        h[i] -= 1
    ans += 1
print(ans)

Submission status: Runtime Error

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

mi=min(h)
h0=h[0]
c=h0-mi
ans=mi
for i in range(1,N):
    if h0<h[i]:
        c+=h[i]-h0
    elif h0>h[i]:
        ans+=c
        mi = min(h[i:])
        c=h[i]-mi
    h0=h[i]

if h[-2]<h[-1]:
    ans+=c
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
H = list(map(int, input().split()))
max_lst = []
min_lst = []
max_h = H[0]
min_h = H[0]
s = 0
for i in range(1, N):
    if H[i-1] <= H[i]:
        if H[0] not in max_lst and H[0] not in min_lst:
            min_lst.append(H[0])
        if max_h == 0:
            min_lst.append(min_h)
        if i == N-1:
            max_lst.append(H[i])
        max_h = H[i]
        min_h = 0
    elif H[i-1] >= H[i]:
        if H[0] not in max_lst and H[0] not in min_lst:
            max_lst.append(H[0])
            s = 1
        if min_h == 0:
            max_lst.append(max_h)
        if i == N-1:
            min_lst.append(H[i])
        min_h = H[i]
        max_h = 0
cnt = 0
if s == 0:
    for i in range(len(min_lst)-1):
        cnt += max_lst[i] - min(min_lst[i], min_lst[i+1])
    cnt += max_lst[-1] - min_lst[-1]
else:
    for i in range(1, len(max_lst)):
        cnt += max_lst[i] - min(min_lst[i-1], min_lst[i])
    cnt += max_lst[0] - min_lst[0]
print(cnt)

Submission status: Runtime Error

Solution code:
N = int(input())
lst = list(map(int, input().split()))
cnt = 0
for i in range(N-1):
    if lst[i] < lst[i+1]:
        cnt += 
print(cnt)




#139 c
N = int(input())
H = list(map(int, input().split()))
max_lst = []
min_lst = []
max_h = H[0]
min_h = H[0]
s = 0
for i in range(1, N):
    if H[i-1] <= H[i]:
        if H[0] not in max_lst and H[0] not in min_lst:
            min_lst.append(H[0])
        if max_h == 0:
            min_lst.append(min_h)
        if i == N-1:
            max_lst.append(H[i])
        max_h = H[i]
        min_h = 0
    elif H[i-1] >= H[i]:
        if H[0] not in max_lst and H[0] not in min_lst:
            max_lst.append(H[0])
            s = 1
        if min_h == 0:
            max_lst.append(max_h)
        if i == N-1:
            min_lst.append(H[i])
        min_h = H[i]
        max_h = 0
cnt = 0
if s == 0:
    for i in range(len(min_lst)-1):
        cnt += max_lst[i] - min(min_lst[i], min_lst[i+1])
    cnt += max_lst[-1] - min_lst[-1]
else:
    for i in range(1, len(max_lst)):
        cnt += max_lst[i] - min(min_lst[i-1], min_lst[i])
    cnt += max_lst[0] - min_lst[0]
print(cnt)

Submission status: Runtime Error

Solution code:
"""
4
1 2 2 1

2


5
3 1 2 3 1

5
"""

_ = int(input())
first = list(map(int, input().split()))
count = 0


def water(base_list):
    global count
    count += 1
    separated = []
    each = []
    for i in base_list:
        if i - 1 == 0:
            separated.append(each)
            each = []
        else:
            each.append(i-1)
    if each:
        separated.append(each)

    new_separated = []
    for i in separated:
        if i:
            new_separated.append(i)

    if new_separated:
        for i in new_separated:
            water(i)
    else:
        return


water(first)

print(count)

Submission status: Runtime Error

Solution code:
N = int(input())
height = [int(i) for i in input().split()] + [0]
k = 0
while True :
    for i in range(N):
        if height[i] > 0 :
            lv = i 
            break 

    for i in range(lv,N+1):
        if height[i] == 0 :
            uv = i 
            break 
    for i in range(lv,uv):
        height[i] -= 1
    k += 1
    if sum(height) == 0 :
        break 
print(k)

Submission status: Runtime Error

Solution code:
import sys

# お約束
args = None
INF = float("inf")
MOD = int(1e9 + 7)
def int1(n):
    return int(n) - 1
def parse(*params):
    if len(params) == 1:
        return params[0](next(args))
    return tuple(p(v) for p, v in zip(params, next(args).split()))
def debug(*v):
    if __debug__:
        print(*v, file=sys.stderr)

# エントリーポイント
def main():
    N = parse(int)
    H = (0, *map(int, next(args).split()))

    c = 0
    debug(H)
    for h in range(100):
        for n in range(1, N + 1):
            if H[n - 1] - h <= 0 and 0 < H[n] - h:
                c += 1
    print(c)

if __name__ == '__main__':
    args = iter(sys.stdin.read().split("\n"))
    main()

Submission status: Runtime Error

Solution code:
n = int(input())

l = map(int, input())

i = 0
cnt = 0
while i < len(l):
  if l[i] == 0:
    i += 1
    continue
  j = i
  height = l[i]
  while l[j] > height:
    l[k] -= height
    j += 1
  i += 1
  cnt += 1
print(cnt)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
  int n;cin>>n;
  vector<int>v(n);
  int ans = v[0];
  for(int i=0;i<n;i++){
    cin >> v[i];
  }
  
    for(int i=1;i<n;i++){
      if(v[i-1] < v[i]) ans += v[i]-v[i-1];
  }
  
  cout << ans << endl;

Submission status: Runtime Error

Solution code:
N = int(input())
h_ls = list(map(int,input().split(" ")))

h_ls = np.array(h_ls)
n = 0
h_len = len(h_ls)
while max(h_ls)!=0:
    max_ind = h_ls.argmax()
    d = 0
    while max_ind+d < h_len -1 and h_ls[max_ind+d+1]==h_ls[max_ind]:
        d+=1
    h_ls[max_ind:max_ind+d+1] =h_ls[max_ind:max_ind+d+1]-1
    n+=1

print(n)

Submission status: Runtime Error

Solution code:
N = int(input())
h_ls = list(map(int,input().split(" ")))

h_ls = np.array(h_ls)
n = 0
h_len = len(h_ls)
while max(h_ls)!=0:
    max_ind = h_ls.argmax()
    d = 0
    while max_ind+d < h_len -1 and h_ls[max_ind+d+1]==h_ls[max_ind]:
        d+=1
    h_ls[max_ind:max_ind+d+1] =h_ls[max_ind:max_ind+d+1]-1
    n+=1
    print(h_ls)

print(n)

Submission status: Runtime Error

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


# 貪欲に極大区間に対して水やりする
def f(y):
    L = next(
        (
            i
            for i, x in enumerate(y)
            if x > 0
        ),
        N
    )
    R = next(
        (
            i
            for i, x in enumerate(y[L:], L)
            if x == 0
        ),
        N
    )
    return (
        0 if L == N else
        1 + f(
            [x for x in y[:L]] +
            [x - 1 for x in y[L:R]] +
            [x for x in y[R:]]
        )
    )


ans = f(h)

print(ans)

Submission status: Runtime Error

Solution code:
# ABC 116 C

N = int(input())

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

def chomp0(arr):
    while len(arr) != 0 and arr[0] == 0:
        del arr[0]

    while len(arr) != 0 and arr[-1] == 0:
        del arr[-1]

    return arr

def solve(arr):
    if len(arr) == 0:
        return 0

    arr = chomp0(arr)

    ans = 0

    num0 = arr.count(0)
    if num0 != 0:
        for _ in range(num0):
            idx = arr.index(0)
            ans += solve(arr[:idx])
            arr = arr[idx+1:]

    minVal = min(arr)
    ans += minVal
    for i in range(len(arr)):
        arr[i] -= minVal

    if arr.count(0) != len(arr):
        ans += solve(arr)

    return ans

print(solve(h))

Submission status: Runtime Error

Solution code:
# ABC 116 C

N = int(input())

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

def chomp0(arr):
    while len(arr) != 0 and arr[0] == 0:
        del arr[0]

    while len(arr) != 0 and arr[-1] == 0:
        del arr[-1]

    return arr

def solve(arr):
    ans = 0

    num0 = arr.count(0)
    if num0 != 0:
        for _ in range(num0):
            idx = arr.index(0)
            ans += solve(arr[:idx])
            arr = arr[idx+1:]

    minVal = min(arr)
    ans += minVal
    for i in range(len(arr)):
        arr[i] -= minVal

    arr = chomp0(arr)

    if arr.count(0) != len(arr):
        ans += solve(arr)

    return ans

print(solve(h))