Problem: 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.

Submission attempts (in order):
import sys 
sys.setrecursionlimit(1000)
n=int(input())
H=list(map(int,input().split()))
def bunkai(H):
    B=[]
    b=0
    f=0
    for i in range(len(H)):
        if H[i]==0:
            if i!=0:
                B.append(H[b:i-1+1])
                b=i+1
            else:
                b=i+1
        elif i==len(H)-1:
            B.append(H[b:])
    return B
def min(lis):
    min=999999999999999
    for i in range(len(lis)):
        if lis[i]<min:
            min=lis[i]
    return min
def mizu(lis,n):
    for i in range(len(lis)):
        lis[i]+=-n
    return lis
def sso(lis,sum):
    #print(sum)
    B=bunkai(lis)
    
    if len(B)==0:
        #print("return: "+str(sum))
        return sum
    for l in B:
        #print(l)
        x=min(l)
        sum+=x
        li=mizu(l,x)
        sum=sso(li,sum)
    
    return sum


sum=0
x=sso(H,sum)

    
print(x)


import sys 
sys.setrecursionlimit(1000)
n=int(input())
H=list(map(int,input().split()))
def bunkai(H):
    B=[]
    b=0
    f=0
    for i in range(len(H)):
        if H[i]==0:
            if i!=0:
                B.append(H[b:i-1+1])
                b=i+1
            else:
                b=i+1
        elif i==len(H)-1:
            B.append(H[b:])
    return B
def min(lis):
    min=999999999999999
    for i in range(len(lis)):
        if lis[i]<min:
            min=lis[i]
    return min
def mizu(lis,n):
    for i in range(len(lis)):
        lis[i]+=-n
    return lis
def sso(lis,sum):
    #print(sum)
    B=bunkai(lis)
    
    if len(B)==0:
        #print("return: "+str(sum))
        return sum
    for l in B:
        #print(l)
        x=min(l)
        sum+=x
        li=mizu(l,x)
        sum=sso(li,sum)
    
    return sum


sum=0
x=sso(H,sum)

    
print(x)


import sys 
sys.setrecursionlimit(1000)
n=int(input())
H=list(map(int,input().split()))
def bunkai(H):
    B=[]
    b=0
    f=0
    for i in range(len(H)):
        if H[i]==0:
            if i!=0:
                B.append(H[b:i-1+1])
                b=i+1
            else:
                b=i+1
        elif i==len(H)-1:
            B.append(H[b:])
    return B
def min(lis):
    min=999999999999999
    for i in range(len(lis)):
        if lis[i]<min:
            min=lis[i]
    return min
def mizu(lis,n):
    for i in range(len(lis)):
        lis[i]+=-n
    return lis
def sso(lis,sum):
    #print(sum)
    B=bunkai(lis)
    
    if len(B)==0:
        #print("return: "+str(sum))
        return sum
    for l in B:
        #print(l)
        x=min(l)
        sum+=x
        li=mizu(l,x)
        sum=sso(li,sum)
    
    return sum


sum=0
x=sso(H,sum)

import sys 
sys.setrecursionlimit(1000)
n=int(input())
H=list(map(int,input().split()))
def bunkai(H):
    B=[]
    b=0
    f=0
    for i in range(len(H)):
        if H[i]==0:
            if i!=0:
                B.append(H[b:i-1+1])
                b=i+1
            else:
                b=i+1
        elif i==len(H)-1:
            B.append(H[b:])
    return B
def min(lis):
    min=101
    if len(lis)==0:
        return 0
    for i in range(len(lis)):
        if lis[i]<min:
            min=lis[i]
    return min
def mizu(lis,n):
    for i in range(len(lis)):
        lis[i]+=-n
    return lis
def sso(lis,sum):
    #print(sum)
    B=bunkai(lis)
    
    if len(B)==0:
        #print("return: "+str(sum))
        return sum
    for l in B:
        #print(l)
        x=min(l)
        #print("+"+str(x))
        sum+=x
        li=mizu(l,x)
        #print("li"+str(li))
        sum=sso(li,sum)
    
    return sum

#print(min([]))
sum=0
x=sso(H,sum)

    
print(x)


Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Accepted