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):
N = int(input())
h = list(map(int, input().split()))

initial_gr_list = [h]

num = 0
def func(gr_list):
    global num
    n_gr_list = [] 
    n2_gr_list = [] 
    
    num_ima = int(num) 
    
    
    for gr in gr_list:
        if gr == []:
            pass
        else:  
            n_gr = []
            #print(gr)
            for i, x in enumerate(gr):
                #print(n_gr)
                if x == 0:
                    n_gr_list.append(n_gr) 
                    n_gr = [] 
                elif i == len(gr)-1: 
                    n_gr.append(x)  
                    n_gr_list.append(n_gr)  
                    n_gr = [] 
                else:
                    n_gr.append(x) 
    print('n_gr_list')
    print(n_gr_list)
    
    
    for n_gr in n_gr_list:
        if n_gr ==[]:
            pass
        else:
            gm = min(n_gr)
            num += gm  
            n2_gr = [max(x - gm, 0) for x in n_gr]
            n2_gr_list.append(n2_gr)
            print('n2_gr')
            print(n2_gr)
    print('n2_gr_list')
    print(n2_gr_list)

    if sum(sum(x) for x in n2_gr_list) == 0: 
        return
    else:
        func(n2_gr_list) 

print(num)

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

initial_gr_list = [h]

num = 0
def func(gr_list):
    global num
    n_gr_list = [] 
    n2_gr_list = [] 
    
    num_ima = int(num) 
    
    
    for gr in gr_list:
        if gr == []:
            pass
        else:  
            n_gr = []
            #print(gr)
            for i, x in enumerate(gr):
                #print(n_gr)
                if x == 0:
                    n_gr_list.append(n_gr) 
                    n_gr = [] 
                elif i == len(gr)-1: 
                    n_gr.append(x)  
                    n_gr_list.append(n_gr)  
                    n_gr = [] 
                else:
                    n_gr.append(x) 
    #print('n_gr_list')
    #print(n_gr_list)
    
    
    for n_gr in n_gr_list:
        if n_gr ==[]:
            pass
        else:
            gm = min(n_gr)
            num += gm  
            n2_gr = [max(x - gm, 0) for x in n_gr]
            n2_gr_list.append(n2_gr)
            #print('n2_gr')
            #print(n2_gr)
    #print('n2_gr_list')
    #print(n2_gr_list)

    if sum(sum(x) for x in n2_gr_list) == 0: 
        return
    else:
        func(n2_gr_list) 


print(num)

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

initial_gr_list = [h]

num = 0
def func(gr_list):
    global num
    n_gr_list = [] 
    n2_gr_list = [] 
    
    num_ima = int(num) 
    
    
    for gr in gr_list:
        if gr == []:
            pass
        else:  
            n_gr = []
            #print(gr)
            for i, x in enumerate(gr):
                #print(n_gr)
                if x == 0:
                    n_gr_list.append(n_gr) 
                    n_gr = [] 
                elif i == len(gr)-1: 
                    n_gr.append(x)  
                    n_gr_list.append(n_gr)  
                    n_gr = [] 
                else:
                    n_gr.append(x) 
    #print('n_gr_list')
    #print(n_gr_list)
    
    
    for n_gr in n_gr_list:
        if n_gr ==[]:
            pass
        else:
            gm = min(n_gr)
            num += gm  
            n2_gr = [max(x - gm, 0) for x in n_gr]
            n2_gr_list.append(n2_gr)
            #print('n2_gr')
            #print(n2_gr)
    #print('n2_gr_list')
    #print(n2_gr_list)

    if sum(sum(x) for x in n2_gr_list) == 0: 
        return
    else:
        func(n2_gr_list) 

func(initial_gr_list)

print(num)


Verdicts: Wrong Answer

Wrong Answer

Accepted