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

ans = 0
while max(h):
    current_hight = 0
    for i in range(len(h)):
        if not h[i]:
            if not current_hight:
                continue
            else:
                break
        elif h[i] < current_hight:
            break
        else:
            current_hight = h[i]
            h[i] -= 1
    ans += 1
    if ans == 75:

print(ans)

Submission status: Runtime Error

Solution code:
n=int(input()
h=list(map(int,input().split()))
ans=0
for i in range(max(h)):
      cnt=0
      for j in range(n):
            if h［j］>=i+1:
                  cnt+=1
            elif j==n-1:
                  ans+=cnt
            else:
                  ans+=cnt
                  cnt=0
print(ans)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
typedef long long unsigned int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1}; // 移動方向


int n;
int h[101];

void input(){
    cin >> n;
    for(int i=0;i<n;i++) cin >> h[i];
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    input();
    int cnt = 0;
    for(int l=0;l<n;l++){
        while(h[l] > 0){
            int r;
            for(r=l+1;r<n;r++){
                if (h[r] == 0) break; 
            }
            for(int x=l;x<r;x++) h[x]--;
            cnt++;
        }
    }
    cout << cnt << endl;
    return 0;
}

Submission status: Runtime Error

Solution code:
import numpy as np

N = int(input())
h_list = list(map(int, input().split(" ")))

h_dict = {}
for elm_i, elm in enumerate(h_list):
    try:
        h_dict[elm].append(elm_i)
    except:
        h_dict[elm] = [elm_i]

h_list = np.asarray(h_list)


def calc(group, past_out, group_start):
    out = 0
    while True:
        #print("group", group)
        min_val = min(group)
        group -= min_val
        out += min_val
        #zero_list = np.where(group == 0)[0]
        #print("outs", past_out, out, group)
        #print("zero0", zero_list)
        zero_list = h_dict[past_out + out]
        #print("zero_all", zero_list, group_start)
        len_group = len(group)
        zero_list = [elm_i - group_start for elm_i in zero_list if elm_i >= group_start and elm_i < group_start + len_group]
        #print("zero", zero_list)

        cond1 = group.shape[0] - 1 in zero_list
        cond2 = 0 in zero_list

        if group.shape[0] == 1:
            #assert group[0] == 0
            break
        else:
            if cond1: 
                zero_list = zero_list[:-1]
                group = group[:-1]
            if cond2:
                group_start += 1
                zero_list = zero_list[1:]
                group = group[1:]

            if not cond1 and not cond2:
                break

            if group.shape[0] == 0:
                break


    if group.shape[0] <= 1:
        pass
    else:
        start_i = -1
        past_out = out
        for end_i in zero_list:
            child_group = group[start_i+1: end_i]
            #print("child_group", child_group)
            out += calc(child_group, past_out, group_start+start_i+1)
            start_i = end_i

        child_group = group[start_i+1:]
        if child_group.shape[0] >= 1:
            #print("child_group", child_group)
            out += calc(child_group, past_out, group_start+start_i+1)

    return out

out = calc(h_list, 0, 0)
print(out)

Submission status: Runtime Error

Solution code:
import numpy as np

N = int(input())
h_list = list(map(int, input().split(" ")))

h_dict = {}
for elm_i, elm in enumerate(h_list):
    try:
        h_dict[elm].append(elm_i)
    except:
        h_dict[elm] = [elm_i]

h_list = np.asarray(h_list)


def calc(group, past_out, group_start):
    out = 0
    while True:
        #print("group", group)
        min_val = min(group)
        group -= min_val
        out += min_val
        #zero_list = np.where(group == 0)[0]
        #print("outs", past_out, out, group)
        #print("zero0", zero_list)
        zero_list = h_dict[past_out + out]
        #print("zero_all", zero_list, group_start)
        zero_list = [elm_i - group_start for elm_i in zero_list if elm_i >= group_start and elm_i < group_start + len(group)]
        #print("zero", zero_list)

        cond1 = group.shape[0] - 1 in zero_list
        cond2 = 0 in zero_list

        if group.shape[0] == 1:
            #assert group[0] == 0
            break
        else:
            if cond1: 
                zero_list = zero_list[:-1]
                group = group[:-1]
            if cond2:
                group_start += 1
                zero_list = zero_list[1:]
                group = group[1:]

            if not cond1 and not cond2:
                break

            if group.shape[0] == 0:
                break


    if group.shape[0] <= 1:
        pass
    else:
        start_i = -1
        past_out = out
        for end_i in zero_list:
            child_group = group[start_i+1: end_i]
            #print("child_group", child_group)
            out += calc(child_group, past_out, group_start+start_i+1)
            start_i = end_i

        child_group = group[start_i+1:]
        if child_group.shape[0] >= 1:
            #print("child_group", child_group)
            out += calc(child_group, past_out, group_start+start_i+1)

    return out

out = calc(h_list, 0, 0)
print(out)

Submission status: Runtime Error

Solution code:
L = [l_0]

m = None

count = 0

while L != []:
	for l in L:
		m = min(l)
		L.remove(l)
		for i in range(len(l)):
			l[i] = l[i] - m
		count += m
		l = list(map(str , l))
		l = " ".join(l)
		l = (l.split("0 "))
		for a in l:
			L.append(list(map( int, (a.split()))))
		while [] in L:
			L.remove([])

print(count)

Submission status: Runtime Error

Solution code:
num = int(input())
targets = map(int, input().split())
targets = list(targets)


water_cnt = 0
def exe_water(init_state):
    # break condition
    if sum(init_state) == 0:
        return
    temp_water_cnt = min(init_state)
    water_cnt += temp_water_cnt

    lowest = temp_water_cnt
    # init sub_state
    sub_state = []
    for h in init_state:
        substracted_h = h - lowest
        if substracted_h != 0:
            sub_state.append(substracted_h)
        else:
            exe_water(sub_state)
            # reinit for next sequence
            sub_state = []

exe_water(targets)
print(water_cnt)

Submission status: Runtime Error

Solution code:
import numpy as np

N = int(input())
h_list = list(map(int, input().split(" ")))

h_dict = {}
for elm_i, elm in enumerate(h_list):
    try:
        h_dict[elm].append(elm_i)
    except:
        h_dict[elm] = [elm_i]

h_list = np.asarray(h_list)


def calc(group, past_out, group_start):
    out = 0
    while True:
        #print("group", group)
        min_val = min(group)
        group -= min_val
        out += min_val
        zero_list = np.where(group == 0)[0]
        #print("outs", past_out, out, group)
        #print("zero0", zero_list)
        zero_list = h_dict[past_out + out]
        #print("zero_all", zero_list, group_start)
        zero_list = [elm_i - group_start for elm_i in zero_list if elm_i >= group_start and elm_i < group_start + len(group)]
        #print("zero", zero_list)

        cond1 = group.shape[0] - 1 in zero_list
        cond2 = 0 in zero_list

        if group.shape[0] == 1:
            #assert group[0] == 0
            break
        else:
            if cond1: 
                zero_list = zero_list[:-1]
                group = group[:-1]
            if cond2:
                group_start += 1
                zero_list = zero_list[1:]
                group = group[1:]

            if not cond1 and not cond2:
                break

            if group.shape[0] == 0:
                break


    if group.shape[0] <= 1:
        pass
    else:
        start_i = -1
        past_out = out
        for end_i in zero_list:
            child_group = group[start_i+1: end_i]
            #print("child_group", child_group)
            out += calc(child_group, past_out, group_start+start_i+1)
            start_i = end_i

        child_group = group[start_i+1:]
        if child_group.shape[0] >= 1:
            #print("child_group", child_group)
            out += calc(child_group, past_out, group_start+start_i+1)

    return out

out = calc(h_list, 0, 0)
print(out)

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
H = np.array(list(map(int,input().split())))

ans = 0
while np.max(H) != 0:
    min_h_p = 0
    for i in range(N):
        if H[i] == 0 and min_h_p > 0:
            ans += min_h_p
            H[st:i] -= min_h_p
            break
        elif i == N-1:
            if min_h_p == 0:
                min_h_p = H[i]
            if st == 0:
                H[i] -= min_h_p
                ans += min_h_p
            else:
                ans += min_h_p
                H[st:] -= min_h_p
            break
        else:
            if min_h_p == 0:
                min_h_p = H[i]
                st = i
            else:
                min_h_p = min(H[i],min_h_p)

print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
H = np.array(list(map(int,input().split())))

ans = 0
while np.max(H) != 0:
    min_h_p = 0
    for i in range(N):
        if H[i] == 0 and min_h_p > 0:
            ans += min_h_p
            H[st:i] -= min_h_p
            break
        elif i == N-1:
            if min_h_p == 0:
                min_h_p = H[i]
            ans += min_h_p
            H[st:] -= min_h_p
            break
        else:
            if min_h_p == 0:
                min_h_p = H[i]
                st = i
            else:
                min_h_p = min(H[i],min_h_p)

print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
H = np.array(list(map(int,input().split())))

ans = 0
while np.max(H) > 0:
    min_h_p = 0
    for i in range(N):
        if H[i] == 0 and min_h_p > 0:
            ans += min_h_p
            H[st:i] -= min_h_p
            min_h_p = 0
        elif i == N-1:
            if min_h_p == 0:
                min_h_p = H[i]
            ans += min_h_p
            H[st:] -= min_h_p
            min_h_p = 0
        else:
            if min_h_p == 0:
                min_h_p = H[i]
                st = i
            else:
                min_h_p = min(H[i],min_h_p)

print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
hight = [int(i) for i in input().split()]
# realhight = [0]*n
# ans = 0
# while True:
#     for l in range(1, n):
#         for r in range(1, n):
#             for x in range(l, r):
#                 realhight[x] += 1
#                 ans += 1
#     if realhight == hight:
#         break
# print(ans)
hight = ''.join(input().split())
# print(min(hight))
#hight [str() for i in hight]
hh = hight.split(min(hight))

while True:
  for i in hh:
      i.split(min(i))
      hh = i.split(min(i))
  if len(hh) == 1:
    break
print(len(hh))

Submission status: Runtime Error

Solution code:
import numpy as np
n=int(input())
h=list(map(int,input().split()))
ans=min(h)
h.append(101)
h= np.array(h)
h=h-[min(h)]*(n+1)
a = 0
for i in range(n-1):
  if h[i]>h[i+1] and h[i+1]<h[i+2]:
    ans += max(h[a:i+2])
    a =i+2
c = 0
for j in range(n-3):
  if h[-j-2]>h[-j-3]:
    c +=1
  else:
    break
ans += h[-2]-h[-c-3]
print(ans)

Submission status: Runtime Error

Solution code:
def mizuyari2(l):
    count = 0
    stopper = 0

    for i in l:
        stopper+=i 
    if(stopper == 0):
        return 0        

    if(l[0]!=0):
        count+=1
    for i in range(len(l)-1):
        if(l[i]==0 and l[i+1]!=0):
            count+=1
    if(l[len(l)-2]==0 and l[len(l)-1!=0]):
        count+=1

    for index,i in enumerate(l):           
        l[index]-=1
        if(l[index]<0):
            l[index]=0

    return count+mizuyari2(l)

N = int(input())
h = []

for i in range(N):
    h.append(int(input()))

print(mizuyari2(h))

Submission status: Runtime Error

Solution code:
hl = []
d1 = 0
ans = 0
mon = 0

N = int(input())
H = input().split()

for i in H:
  hl.append(int(i))

while d1 != 1:
  for i in range(len(hl)):
    dame = 0
    while dame != 2:
      if(i <= len(hl)):
        if(dame == 0):
          if(hl[i] != 0):
            hl[i] -= 1
            i += 1
            dame += 1
        else:
          if(hl[i] != 0):
            h[i] -= 1
            i += 1
          else:
            dame += 1
      else:
        dame = 2
    ans += 1
  for i in range(len(hl)):
    if(i != 0):
      mon = 1
  if(mon == 0):
    di = 1
  else:
    mon = 0

print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

N=input()
N=int(N)
hs=[int(i) for i in input().split()]


def func(x):
   # print(x)
    x_min = min(x)
    #print(x_min)
    x = np.array(x)
    
    #print(x)
    x -= x_min
    x = list(x)
    ans = x_min
    if len(x)==0:
        return 0
    if len(x)==1:
        return ans
    while x[0]==0:
        x=x[1:]
    while x[-1]==0:
        x=x[:-1]
    lsts=[]
    lst=[]
    for i in range(len(x)):
        if x[i]!=0:
            lst.append(x[i])
            if i==len(x)-1:
                lsts.append(lst)
        else:
            lsts.append(lst)
            lst=[]
    #print(lsts)
    for lst in lsts:
        ans += func(lst)

    return ans

print(func(hs))

Submission status: Runtime Error

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

def dfs(L):
    for i,l in enumerate(L):
        if l != 0:
            break
    L = L[i:N]
    s = 0
    res = 0
    for i,l in enumerate(L):
        if l == 0:
            L_ = [l_-1 for l_ in L[s:i]]
            if len(L_) != 0:
                res += 1
                s = i+1
                if sum(L_) != 0:
                    res += dfs(L_)
        elif i == len(L)-1:
            L_ = [l_-1 for l_ in L[s:len(L)]]
            if len(L_) != 0:
                res += 1
                s = i+1
                if sum(L_) != 0:
                    res += dfs(L_)
    return res

if sum(h) == 0:
    print(0)
else:
    print(dfs(h))

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split(' ')))
b = [0]*n
cnt = 0
for i in range(1, max(a)+1):
	flg = False
	for j in range(n):
		if a[j] >= i and b[j] < i:
			b[j] += 1
			cnt += 1
			for k in range(j+1,n):
				if a[k] >= i and b[k] < i:
					b[k] += 1
				else:
					break
			if k == n:
				break
print(cnt)

Submission status: Runtime Error

Solution code:
def mizuyari2(l):
    count = 0
    stopper = 0

    for i in l:
        stopper+=i 
    if(stopper == 0):
        return 0        

    if(l[0]!=0):
        count+=1
    for i in range(len(l)-1):
        if(l[i]==0 and l[i+1]!=0):
            count+=1

    for index,i in enumerate(l):           
        l[index]-=1
        if(l[index]<0):
            l[index]=0

    return count+mizuyari2(l)

N = int(input())
h = []

for i in range(N):
    h.append(int(input()))

print(mizuyari2(h))

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split(' ')))
b = [0]*n
cnt = 0
for i in range(1, max(a)+1):
	flg = False
	for j in range(n):
		if a[j] >= i and b[j] < i:
			b[j] += 1
			cnt += 1
			for k in range(j+1,n):
				if a[k] >= i and b[k] < i:
					b[k] += 1
				else:
					break
			if k == n:
				break
print(cnt)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:

def grand_garden(count, hlist):
    nonzero = False
    for i, h_i in enumerate(hlist):
        if h_i != 0:
            nonzero = True
            hlist[i] = hlist[i] -1
        elif nonzero:
            break


    if nonzero:
        return grand_garden(count + 1, hlist)
    else:
        return count






n = int( input())
hlist = input().split()
for i, h_i in enumerate(hlist):
    hlist[i] = int(hlist[i])

count = grand_garden(0, hlist)

print(count)

Submission status: Runtime Error

Solution code:
n = int(input())

h = list(map(int, input().split()))
#now = [0 for _ in range(n)]

ans = 0

indexes = []

#if len(set(h)) == n:

if h[0] >= h[1]:
    indexes.append(0)
        
for i in range(1, n-1):
    if h[i-1] <= h[i] >= h[i+1]:
        indexes.append(i)
                
if h[n-2] <= h[n-1]:
    indexes.append(n-1)
                    
#print(indexes)

for i in range(len(indexes)):
    if i != 0:
        if indexes[i] == indexes[i-1] + 1:
            ans -= h[indexes[i]]

    if indexes[i] != n-1 and indexes[i] != 0:
        ans += h[indexes[i]]
        
    elif indexes[i] == n-1:
        ans += h[n-1] - h[n-2]

    else:
        ans += h[0] - h[1]


print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())

h = list(map(int, input().split()))
now = [0 for _ in range(n)]

ans = 0

indexes = []

#if len(set(h)) == n:

if h[0] > h[1]:
    indexes.append(0)
        
for i in range(1, n-1):
    if h[i-1] <= h[i] >= h[i+1]:
        indexes.append(i)
                
if h[n-2] < h[n-1]:
    indexes.append(n-1)
                    
#print(indexes)

for i in range(len(indexes)):
    if i != 0:
        if indexes[i] == indexes[i-1] + 1:
            ans -= h[indexes[i]]

    if indexes[i] != n-1 and indexes[i] != 0:
        ans += h[indexes[i]]
        
    elif indexes[i] == n-1:
        ans += h[n-1] - h[n-2]

    else:
        ans += h[0] - h[1]


print(ans)

Submission status: Runtime Error

Solution code:
#import numpy as np

N = int(input())
h_list = list(map(int, input().split(" ")))
#h_list = np.asarray(h_list)

def calc(group):
    out = 0
    while True:
        #print("group", group)
        min_val = min(group)
        #group -= min_val # 1
        #zero_list = np.where(group == 0)[0] #1
        zero_list = [elm_i for elm_i, elm in enumerate(group) if elm == 0]
        group = [elm - min_val for elm in group]
        out += min_val

        #cond1 = group.shape[0] - 1 in zero_list # 1
        cond1 = len(group) - 1 in zero_list
        cond2 = 0 in zero_list

        #if group.shape[0] == 1:
        if len(group) == 1:
            #assert group[0] == 0
            break
        else:
            if cond1: 
                zero_list = zero_list[:-1]
                group = group[:-1]
            if cond2:
                zero_list = zero_list[1:]
                group = group[1:]

            if not cond1 and not cond2:
                break

            #if group.shape[0] == 0:
            if len(group) == 0:
                break


    #if group.shape[0] <= 1:
    if len(group) <= 1:
        pass
    else:
        start_i = -1
        for end_i in zero_list:
            child_group = group[start_i+1: end_i]
            #print("child_group", child_group)
            out += calc(child_group)
            start_i = end_i

        child_group = group[start_i+1:]
        #if child_group.shape[0] >= 1:
        if len(child_group) >= 1:
            #print("child_group", child_group)
            out += calc(child_group)

    return out

out = calc(h_list)
print(out)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 20 20:52:23 2019

@author: Owner
"""

import collections
import scipy.misc
import sys
import numpy as np
import math
from operator import itemgetter
import itertools
import copy

#素因数を並べる
def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
      table.append(int(i))
    i += 1
  if n > 1:
    table.append(int(n))
  return table   
# 桁数を吐く
def digit(i):
    if i > 0:
        return digit(i//10) + [i%10]
    else:
        return []
    
def getNearestValueIndex(list, num):
    """
    概要: リストからある値に最も近い値のインデックスを取得する関数
    @param list: データ配列
    @param num: 対象値
    @return 対象値に最も近い値
    """

    # リスト要素と対象値の差分を計算し最小値のインデックスを取得
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx

"""
N, X = map(int, input().split())

x = [0]*N
x[:] = map(int, input().split())

P = [0]*M
Y = [0]*M
for m in range(M):
    P[m], Y[m] = map(int, input().split())
    
all(nstr.count(c) for c in '753')
"""
N = int(input())

h = [0]*N
h[:] = map(int, input().split())

h = np.array(h)

cnt = 0
while np.sum(h ==0) != N:
    if np.sum(h ==0) == 0:        
        h = h-1
        cnt +=1
    else:
        for n in range(N-1):
            if h[n] == 0 and h[n+1] !=0:
                l = n+1
            elif h[0] != 0:
                l = 0
            else:
                pass
            if h[n] != 0 and h[n+1] == 0:
                r = n
                break
        for x in range(l, r+1):
            h[x] = h[x] -1
        cnt +=1
print(cnt)

Submission status: Runtime Error

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

mizuyari = 0
for i in range(int(sorted(h)[n-1])):
    maenohana = False
    mizuyari += thismizuyari
    thismizuyari = 0
    for j in range(n):
        if not maenohana and i+1<=int(h[j]):
            thismizuyari += 1
        maenohana = i+1<=int(h[j])
print(mizuyari)

Submission status: Runtime Error

Solution code:
n=int(input())
h=list(map(int,input().split()))
cnt=0
while set(h)=={0}
  if h not in 0:
    for i in range(len(h)):
      h[i]=h[i]-min(h)
      cnt+=min(h)
  else:
    for i in range(len(h)):
      l=[]
      if h[i]==0:
      else:
        l.append(h[i])
        continue
      cnt+=min(l)
print(cnt)

Submission status: Runtime Error

Solution code:
import numpy as np

N=input()
N=int(N)
hs=[int(i) for i in input().split()]


def func(x):
    x_min = min(x)
    #print(x_min)
    x = np.array(x)
    
    #print(x)
    x -= x_min
    x = list(x)
    ans = x_min

    if len(x)==1:
        return ans
    if x[0]==0:
        x=x[1:]
    if x[len(x)-1]==0:
        x=x[:len(x)-1]
    lsts=[]
    lst=[]
    for i in x:
        if i!=0:
            lst.append(i)
            if i==x[len(x)-1]:
                lsts.append(lst)
        else:
            lsts.append(lst)
            lst=[]
    for lst in lsts:
        ans += func(lst)

    return ans

print(func(hs))

Submission status: Runtime Error

Solution code:
import numpy as np

N = int(input())
h_list = np.asarray(list(map(int, input().split(" "))))

def calc(group):
    out = 0
    while True:
        #print("group", group)
        min_val = min(group)
        group -= min_val
        out += min_val
        zero_list = np.where(group == 0)[0]

        cond1 = group.shape[0] - 1 in zero_list
        cond2 = 0 in zero_list

        if group.shape[0] == 1:
            assert group[0] == 0
            break
        else:
            if cond1: 
                zero_list = zero_list[:-1]
                group = group[:-1]
            if cond2:
                zero_list = zero_list[1:]
                group = group[1:]

            if not cond1 and not cond2:
                break

            if group.shape[0] == 0:
                break


    if group.shape[0] == 1:
        pass
    else:
        start_i = -1
        for end_i in zero_list:
            child_group = group[start_i+1: end_i]
            #print("child_group", child_group)
            out += calc(child_group)
            start_i = end_i

        child_group = group[start_i+1:]
        if child_group.shape[0] >= 1:
            #print("child_group", child_group)
            out += calc(child_group)

    return out

out = calc(h_list)
print(out)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
def inpl(): return list(map(int, input().split()))
N = int(input())
F = [0]*N
H = inpl()
ans = 0
while True:
    for l in range(N):
        if F[l] < H[l]:
            for r in range(l+1, N):
                if F[r] < H[r]:
                    continue
                break
            else:
                r += 1
            if r > N:
                r = N
            for j in range(l, r):
                F[j] += 1
            if l < r:
                ans += 1
            break
    else:
        break
#assert F == H
print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
def inpl(): return list(map(int, input().split()))
N = int(input())
F = [0]*N
H = inpl()
ans = 0
checked = [0]*1000
while True:
    checked[ans] += 1
    for l in range(N):
        if F[l] < H[l]:
            for r in range(l+1, N):
                if F[r] < H[r]:
                    continue
                break
            else:
                r += 1
            if r > N:
                r = N
            for j in range(l, r):
                F[j] += 1
            if l < r:
                ans += 1
            break
    else:
        break
assert F == H
print(ans)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(1000000)

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

def f(A):
    p = 0
    if min(A)==0 and max(A)==0:
        return 0
    if min(A)>0:
        minA = min(A)
        for i in range(len(A)):
            A[i] -= minA
        p += f(A) + minA
    else:
        for i in range(len(A)):
            if A[i]>0:
                A = A[i:]
                break
        for i in range(len(A)):
            if A[len(A)-1-i]>0:
                A = A[:len(A)-i]
                break
        x = [-1]
        for i in range(len(A)):
            if A[i]==0:
                x.append(i)
        x.append(len(A))
        for i in range(len(x)-1):
            p += f(A[x[i]+1:x[i+1]:])
    return p

print(f(H))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)

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

def f(A):
    p = 0
    if min(A)==0 and max(A)==0:
        return 0
    if min(A)>0:
        minA = min(A)
        for i in range(len(A)):
            A[i] -= minA
        p += f(A) + minA
    else:
        for i in range(len(A)):
            if A[i]>0:
                A = A[i:]
                break
        for i in range(len(A)):
            if A[len(A)-1-i]>0:
                A = A[:len(A)-i]
                break
        x = [-1]
        for i in range(len(A)):
            if A[i]==0:
                x.append(i)
        x.append(len(A))
        for i in range(len(x)-1):
            p += f(A[x[i]+1:x[i+1]:])
    return p

print(f(H))

Submission status: Runtime Error

Solution code:
n = int(input())
li = list(map(int,input().split()))
ans = 0
for i in range(n):
  if li[i - 1] == 0:
    del li[i - 1]
    n = len(li)
  else:
    ans += li[i - 1]
    for j in range(n):
      li[j - 1] -= 1
print(str(ans))

Submission status: Runtime Error

Solution code:
n = int(input())
flowers = list(map(int, input().split()))
flowers.append(max(flowers))
cnt = 0
for i in range(sum(flowers)):
    if(all([x<0 for x in flowers])): break
    print(flowers, i, cnt)
    flowers = list(map(lambda x: (x-1), flowers))
    for j in range(n):
        if(flowers[j] > 0 and flowers[j+1] <= 0): cnt+=1
print(i + cnt)

Submission status: Runtime Error

Solution code:
import numpy as np
n = int(input())
h = list(map(int, input().split()))

h = np.array(h)

h_zero = np.hstack([np.zeros(1), h, np.zeros(1)])

d = np.diff(h_zero)

a= (d[:-1] * d[1:]) < 0

h_a = h[a == True]

s = 0
for k in range(len(h_a)):
    
    if k%2 == 0:
        
            
        if k == 0:
            s += h_a[k] - h_a[k+1]
        elif k == len(h_a)-1:
            s += h_a[k] - h_a[k-1] 
        else:
            s += h_a[k] - np.max([h_a[k-1], h_a[k+1]])
    else:
        s += h_a[k]
#     print(k, s)
print(s)

Submission status: Runtime Error

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

h = np.array(h)

h_zero = np.hstack([np.zeros(1), h, np.zeros(1)])

d = np.diff(h_zero)

a= (d[:-1] * d[1:]) < 0

h_a = h[a == True]

s = 0
for k in range(len(h_a)):
    
    if k%2 == 0:
        
            
        if k == 0:
            s += h_a[k] - h_a[k+1]
        elif k == len(h_a)-1:
            s += h_a[k] - h_a[k-1] 
        else:
            s += h_a[k] - np.max([h_a[k-1], h_a[k+1]])
    else:
        s += h_a[k]
#     print(k, s)
print(s)

Submission status: Runtime Error

Solution code:
N = int(input())

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

ans = 0
minh = min(h)
ans += min(h)

h = list(map(lambda x: x-minh, h))

while True:
    rightflag = False

    for i in range(N):
        if h[i] == 0:
            continue
        else:
            left = i
            break

    for j in range(left,N):
        if h[j] != 0:
            continue
        else:
            right = j-1
            rightflag = True
            break

    if not rightflag:
        right = N-1

    # print(left,right)

    tmp = min(h[left:right+1])
    ans += tmp

    for pos in range(left,right+1):
        h[pos] -= tmp

    # print(h)
    if set(h) == {0}:
        break

print(ans)