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 math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
import numpy as np



#  inf = 10 ** 20
#  eps = 1.0 / 10**10
#  mod = 10**9+7
#  dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
#  ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]


def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)

def main():
    N = I()
    h = LI()
    h = np.array(h)
    #  print(N, h)
    
    
    
    count = 0
    while True:
        #  print(h)
        first_non_zero_idx = [idx for idx, i in enumerate(h) if i != 0]
        if len(first_non_zero_idx) == 0:
            
            print(count)
            return
        else:
            first_non_zero_idx = first_non_zero_idx[0]

        first_zero_idx = [idx for idx, i in enumerate(h) if idx > first_non_zero_idx and i == 0]
        if len(first_zero_idx) == 0:
            
            first_zero_idx = len(h)
        else:
            first_zero_idx = first_zero_idx[0]

        
        h[first_non_zero_idx:first_zero_idx] -= 1
        count += 1





main()


import numpy as np
N = int(input())
h = list(map(int, input().split()))
diff = np.array([i for i in h])




def find_min_idx(l):
    
    non_0_list = [i for i in l if i != 0]
    min_value = min(non_0_list)
    min_idx = np.where(l == min_value)[0][0]
    print('---', min_value, min_idx)
    return min_idx


def get_min_value(l, min_idx):
    return l[min_idx]


def get_end_idx(l, min_idx, min_value):
    for idx, i in enumerate(l):
        if idx > min_idx:
            if i == 0:
                return idx - 1
            if i < min_value or i == 0:
                return idx - 1
    else:
        return len(l) - 1


def find_start_idx(l, min_idx):
    if len(l[:min_idx]) == 0:
        return 0
    for idx, i in enumerate(reversed(l[:min_idx])):
        if i == 0:
            return idx
    else:
        return len(l[:min_idx])-1


count = 0
while not all([i == 0 for i in diff]):
    print(count, diff)
    #  import pdb
    #  pdb.set_trace()
    min_idx = find_min_idx(diff)
    min_value = get_min_value(diff, min_idx)
    end_idx = get_end_idx(diff, min_idx, min_value)
    start_idx = find_start_idx(diff, min_idx)
    print('min_idx, end_idx, start_idx', min_idx, end_idx, start_idx)
    #  import pdb
    #  pdb.set_trace()
    for idx, i in enumerate(diff):
        if i == 0:
            continue

        if idx <= end_idx:
            if idx >= min_idx - start_idx - 1:
                diff[idx] -= min_value
            elif idx >= min_idx:
                diff[idx] -= min_value

    count += min_value

    print(count, diff, '\n')

print(count)


import numpy as np


sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]


def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)

def main():
    N = I()
    h = LI()
    h = np.array(h)
    #  print(N, h)
    
    
    
    count = 0
    while True:
        #  print(h)
        first_non_zero_idx = [idx for idx, i in enumerate(h) if i != 0]
        if len(first_non_zero_idx) == 0:
            
            print(count)
            return
        else:
            first_non_zero_idx = first_non_zero_idx[0]

        first_zero_idx = [idx for idx, i in enumerate(h) if idx > first_non_zero_idx and i == 0]
        if len(first_zero_idx) == 0:
            
            first_zero_idx = len(h)
        else:
            first_zero_idx = first_zero_idx[0]

        
        h[first_non_zero_idx:first_zero_idx] -= 1
        count += 1





main()


#  import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
import sys
import numpy as np



#  inf = 10 ** 20
#  eps = 1.0 / 10**10
#  mod = 10**9+7
#  dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
#  ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]


def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)

def main():
    N = I()
    h = LI()
    h = np.array(h)
    #  print(N, h)
    
    
    
    count = 0
    while True:
        #  print(h)
        first_non_zero_idx = [idx for idx, i in enumerate(h) if i != 0]
        if len(first_non_zero_idx) == 0:
            
            print(count)
            return
        else:
            first_non_zero_idx = first_non_zero_idx[0]

        first_zero_idx = [idx for idx, i in enumerate(h) if idx > first_non_zero_idx and i == 0]
        if len(first_zero_idx) == 0:
            
            first_zero_idx = len(h)
        else:
            first_zero_idx = first_zero_idx[0]

        
        min_value = min(h[first_non_zero_idx:first_zero_idx])
        h[first_non_zero_idx:first_zero_idx] -= min_value
        count += min_value





main()


Verdicts: Runtime Error

Wrong Answer

Runtime Error

Accepted