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()))
count=0
while h.count(0)!=len(h):
  stack=[]
  substack=[]
  for i in range(len(h)):
    if h[i]!=0:
      substack.append(i)
    else:
      substack.append(i)
      stack.append(substack)
      substack=[]
  h=[]
  for i in range(len(stack)):
    count+=min(i)
    h+=i
print(count)

Submission status: Runtime Error

Solution code:
n = int(input())
h = list(map(int,input().split()))
b = []
inf = 10**10
start = 0
end = 0
ans = 0

for ii in range(inf):
    while h[0] == 0:
        del h[0]

    for i in range(len(h)):
        if h[i] == 0:
            end = i
            #print(b)
            break
        end = len(h)
        b.append(h[i])

    #print(b)
    sa = min(b)
    b.clear()
    #print(sa)

    for j in range(end):
        h[j] = h[j] - sa
        #print(h)

    ans = ans + sa
        
    if max(h) == 0:
        break

print(ans)

Submission status: Runtime Error

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

a = np.zeros(N)
h_min = min(h)
tmp = (h >= h_min+1)*1

cnt = 0
for idx, i in enumerate(tmp):
    if idx==0:
        a0 = i
        if i==1:
            cnt+=1
        continue
        
    if idx==a0:
        continue
    else:
        cnt+=1
else:
    if i==0:
        cnt-=1
        
print(cnt+h_min)

Submission status: Runtime Error

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

a = np.zeros(N)
h_min = min(h)
tmp = (h >= h_min+1)*1

cnt = 0
for idx, i in enumerate(tmp):
    if idx==0:
        a0 = i
        if i==1:
            cnt+=1
        continue
        
    if idx==a0:
        continue
    else:
        cnt+=1
else:
    if i==0:
        cnt-=1
        
print(cnt+h_min)

Submission status: Runtime Error

Solution code:
from itertools import*
from numpy import*
n,*a=map(int,open(0).read().split())
a=array(a)
count=0
ans=0
while 1:
    count=min(a[a!=0])
    ans+=count*sum(k for k,v in groupby(a>0)if k==True)
    a=a-min(a[a!=0])
    a[a<0]=0
    if sum(a)==0:
        break
print(ans)

Submission status: Runtime Error

Solution code:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
N = input()
h = [int(x) for x in input().split()]
count = 0
while sum(h) != 0:
    htop = [1 if x-(max(h)-1) >= 1 else 0 for x in h]
    h = [x-1 if x-1 >= 0 else 0 for x in h]
    htop = "".join([str(x) for x in htop])
    htop = htop.strip("0")split("0") 
    count += len(htop)

print(count)

Submission status: Runtime Error

Solution code:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
N = input()
h = [int(x) for x in input().split()]
count = 0
while sum(h) != 0:
    htop = [1 if x-(max(h)-1) >= 1 else 0 for x in h]
    h = [x-1 if x-1 >= 0 else 0 for x in h]
    htop = "".join(htop)
    htop = htop.split("0") 
    count += len(htop)

print(count)

Submission status: Runtime Error

Solution code:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
N = input()
h = [int(x) for x in input().split()]
count = 0
while sum(h) == 0:
    htop = [1 if x-(max(h)-1) >= 1 else 0 for x in h]
    h = [x-1 if x-1 >= 0 else 0 for x in h]
    htop = "".join(htop)
    htop = htop.split("0") 
    count += len(htop)

print(count)

Submission status: Runtime Error

Solution code:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
N = input()
h = [int(x) for x in input().split()]
count = 0
while sum(h) == 0:
    htop = [1 if x-(max(h)-1) >= 1 else 0 for x in h]
    h = [x-1 if x-1 >= 0 else 0 for x in h]
    htop = "".join(htop)
    htop = htop.split("0") 
    count += len(htop)-1

print(count)

Submission status: Runtime Error

Solution code:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
N = input()
h = [int(x) for x in input().split()]
count = 0
while sum(h) == 0:
    htop = [1 if x-(max(h)-1) >= 1 else 0 for x in h]
    h = [x-1 if x-1 >= 0 else 0 for x in h]
    htop = "".join(htop)
    htop = htop.split("1") 
    count += len(htop)-1

print(count)

Submission status: Runtime Error

Solution code:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
N = input()
h = [int(x) for x in input().split()]
count = 0
while sum(h) == 0:
    htop = [x-(max(h)-1)　if x-(max(h) >= 0) else 0 for x in h]
    h = [x-1 if x-1 >= 0 else 0 for x in h]
    htop = "".join(htop)
    htop = htop.split("1") 
    count += len(htop)-1

print(count)

Submission status: Runtime Error

Solution code:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
N = input()
h = [int(x) for x in input().split()]
count = 0
while sum(h) == 0:
    htop = [x-(max(h)-1)　if x-(max(h) >= 0) else 0 for x in h]
    h = [x-1 if x-1 >= 0 else 0 for x in h]
    htop = "".join(h)
    htop = htop.split("1") 
    count += len(htop)-1

print(count)

Submission status: Runtime Error

Solution code:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
N = input()
h = [int(x) for x in input().split()]
count = 0
while sum(h) == 0:
    htop = [x-(max(h)-1)　if x-(max(h) >= 0) else 0 for x in h]
    h = [x-1 if x-1 >= 0 else 0 for x in h]
    count += len(h.split("".join(htop)))-1

print(count)

Submission status: Runtime Error

Solution code:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
N = input()
h = "".join(input().split()) # h は数字の文字列
count = 0
while sum([int(x) for x in h]) != 0:
    h2 = [x for x in h.strip("0").split("0")]
    count += len(h2)
    h3 = "".join(h2)
    h = [int(x)-1 for x in h3]

print(count)

Submission status: Runtime Error

Solution code:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
N = input()
h = "".join(input().split()) # h は数字の文字列
count = 0
while sum([int(x) for x in h]) != 0:
    h2 = [x for x in h.strip("0").split("0")]
    count += len(h2)
    h3 = ""join.h2
    h = [int(x)-1 for x in h3]

print(count)

Submission status: Runtime Error

Solution code:
n=int(input())
h=list(map(int,input()))
hm=max(h)
s=['']*hm
for k in range(hm+1):
  for i in range(n):
    if h[i]>=k:
      s[k]+='#'
    else:
      s[k]+=' '

ans=0
for k in range(hm+1):
  ans+=len(list(s[k].split()))
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
H = [int(input()) for _ in range(N)]
cost = 0
for i in range(N-1):
  if H[i] > H[i+1]:
    cost += (H[i] - H[i+1])
cost += H[N-1]
print(cost)

Submission status: Runtime Error

Solution code:
def solve(lst):
    def rec(left, right):
        if left == right:
            return 0
        res = 0
        if not 0 in lst[left:right]:
            m = min(lst)
            for i in range(left, right):
                lst[i] = lst[i] - m
            res += m
        m = 1000
        for i in range(left, right):
            if lst[i] > 0:
                m = min(m, lst[i])
            else:
                for j in range(left, i):
                    lst[j] = lst[j] - m
                if left != i:
                    res += m
                return res + rec(left, i) + rec(i+1, right)
    return rec(0, len(lst))


N = int(input())
lst = list(map(int, input().split()))
print(solve(lst))

Submission status: Runtime Error

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

loc_max = h[0]
count = 0

for i in range(1, N):
    if h[i] >= loc_max:
        loc_max = h[i]
    if h[i] < h[i-1]:
        count = count + loc_max - h[i]
        loc_max = 0
    if i == N-1:
        count = count + h[i]

print(count)

Submission status: Runtime Error

Solution code:
N = input()
str_h = input()

fls = flowers(str_h)
ls_fls = fls.str_split0()
water = 0

while(len(ls_fls)>0):
    #print(f'ls_fls = {ls_fls}')
    temp_ls_fls = []
    for fls in ls_fls:
        if len(fls)<=2:
            water += max([int(s) for s in fls])
            continue
        else:
            _fls = flowers(' '.join(fls))
            _fls.minimize()
            _ls_fls = _fls.str_split0()
            water += _fls.water
            temp_ls_fls += _ls_fls
            flg=1
    ls_fls = temp_ls_fls
print(water)

Submission status: Runtime Error

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

ans = 0
for i in range(1, n):
  d = h[i] - h[i-1]
  if d > 0:
    ans += d
print(ans)

Submission status: Runtime Error

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


def mizuyari(h_list, l, r):
    import pdb;pdb.set_trace()
    num_mizuyari = min(h_list[l:r+1])
    h_list = [h - num_mizuyari if l <= x and x <= r else h for x, h in enumerate(h_list)]
    zero_posi = h_list[l:].index(0) + l
    if l == zero_posi:
        first_non_zero_posi = [x for x, h in enumerate(h_list) if h != 0]
        if not first_non_zero_posi:
            print(sum_mizuyari + num_mizuyari)
            exit()
        l = first_non_zero_posi[0]
        if list(range(l, len(h_list))) == first_non_zero_posi:
            return h_list, num_mizuyari, l, len(h_list)
        zero_posi = h_list[l:].index(0) + l
    new_r = zero_posi - 1
    return h_list, num_mizuyari, l, new_r
l = 0
r = len(h_list) - 1
sum_mizuyari = 0
while True:
    h_list, num_mizuyari, l, r = mizuyari(h_list, l, r)
    sum_mizuyari += num_mizuyari

Submission status: Runtime Error

Solution code:
s = int(input())
a = list(map(int, input().split()))

incFlg = True   #登る場合はTure,　降る場合はFalse
before_height = 0
sum = 0

for i in range(len(a)-1):
    #降る場合
    if a[i] > a[i+1]:
        #その前が登り
        if incFlg:
            sum += a[i] - before_height
            incFlg = False
        #その前が下り
        else:
            # before_height = a[i]
    #登る場合
    else:
        if not incFlg:
            before_height = a[i]
        incFlg = True

if incFlg:
    sum += a[len(a)-1] - before_height

print(sum)

Submission status: Runtime Error

Solution code:
"main.py" 5L, 116C written
def calc_watering_num(h, offset, left, right):
        if left > right:
                return 0
        min_h = None
        min_h_indices = []
        for i in range(left, right + 1):
                if min_h is None or min_h > h[i] - offset:
                        min_h = h[i] - offset
                        min_h_indices = [i]
                elif min_h == h[i] - offset:
                        min_h_indices.append(i)
        if len(min_h_indices) == right - left + 1:
                return min_h
        num = min_h
        prev = left
        for i in min_h_indices:
                num += calc_watering_num(h, offset + min_h, prev, i - 1)
                prev = i + 1
        num += calc_watering_num(h, offset + min_h, prev, right)
        return num


input()
h = [int(v) for v in input().split()]
print(calc_watering_num(h, 0, 0, len(h) - 1))

Submission status: Runtime Error

Solution code:
def solve():
	N = int(input())
	h = [int(i) for i in input().split()]
    ans = 0
	for i in range(1, N):
		ans += max(h[i] - (0 if i - 1 < 0 else h[i - 1]), 0)
	return ans

print(solve())

Submission status: Runtime Error

Solution code:
def solve():
	N = int(input())
	h = [int(i) for i in input().split()]
    return N * max(h) - sum(h)

print(solve())

Submission status: Runtime Error

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

def nextgen(comb):
    lis = comb[0]
    j = comb[1]
    for i in range(len(lis)-1):
        if (i != len(lis) and (lis[i] == 0 and lis[i+1] != 0)) or (i == 0 and lis[i] != 0):
            j += 1
        if lis[i] != 0:
            lis[i] -= 1
    if lis[len(lis)-1] != 0:
        lis[len(lis)-1] -= 1
    return [lis, j]

comb = [heights, 0]
while (comb[0].count(0) != len(comb[0])):
    comb = test(comb)

print(comb[1])

Submission status: Runtime Error

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

def f(li):
  global ans
  
  #終了条件
  if li==[]:
    del li
    return
  
  #再帰
  min_num=min(li)
  if min(li)>0:
    count+=min(li)
    li=list(map(lambda x: x-min(li), li))
  li=[li[:li.index(0)],li[li.index(0)+1:]]
  f(li[0])
  f(li[1])

f(h)
print(count)

Submission status: Runtime Error

Solution code:
def main():
    N = int(input())
    h_lst = list(map(int, input().split()))
    while h_lst[0] == 0:
        h_lst = h_lst[1:]
    ans = 0
    while len(h_lst) > 0:
        r = len(h_lst) - 1
        for i in range(len(h_lst) - 1):
            if h_lst[i + 1] == 0:
                r = i
                break

        block_min = min(h_lst[:r + 1])
        for i in range(r + 1):
            h_lst[i] -= block_min
        ans += block_min

        while h_lst[0] == 0:
            del h_lst[0]
            if h_lst == []:
                break

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
N = int(input()) 
height = list(map(int, input().split())) #入力：花の高さ
water_count = 0 #水やり回数
while True:
    index = [i for i, v in enumerate(height) if v >0] #水をやる必要のある花のリスト インデックスが連続した範囲でしか水やりできない
    
    if len(index) == len(height): #全部高さが一以上のとき
        height -= 1 
        water_count += 1
    
    else:
        # 花の水やり範囲を決定
        min_index = index[0] #インデックスの最小値
        max_index = index[0] #インデックスの最大値
    
        while True:
            if (max_index+1) in index:
                max_index += 1
            else:
                break
    
        for flower in range(min_index, max_index+1):
            height[flower] -= 1 
        water_count += 1 #水やりカウンタ増やす
        
    if sum(height) == 0: #水やりが終わったら終了
        break
print(water_count)

Submission status: Runtime Error

Solution code:
N = int(input()) 
height = list(map(int, input().split())) #入力：花の高さ
water_count = 0 #水やり回数
while True:
    index = [i for i, v in enumerate(height) if v >0] #水をやる必要のある花のリスト インデックスが連続した範囲でしか水やりできない
    
    # 花の水やり範囲を決定
    min_index = index[0] #インデックスの最小値
    max_index = index[0] #インデックスの最大値
    
    while True:
        if (max_index+1) in index:
            max_index += 1
        else:
            break
    
    for flower in range(min_index, max_index+1):
        height[flower] -= 1 #水やりカウンタ増やす
    water_count += 1
        
    if sum(height) == 0: #水やりが終わったら終了
        break
print(water_count)

Submission status: Runtime Error

Solution code:
N = int(input()) 
height = list(map(int, input().split())) #入力：花の高さ
water_count = 0 #水やり回数
while True:
    index = [i for i, v in enumerate(height) if v >0] #水をやる必要のある花のリスト インデックスが連続した範囲でしか水やりできない
    
    # 花の水やり範囲を決定
    min_index = index[0] #インデックスの最小値
    max_index = index[0] #インデックスの最大値
    
    while True:
        if max_index+1 in index:
            max_index += 1
        else:
            break
            
    # print("index: {}".format(index)) 
    
    water_amount = min(height[min_index:max_index+1]) #あげる水の量
    for flower in range(min_index, max_index+1):
        height[flower] -= water_amount #水やりカウンタ増やす
    
    # print("height:{} ".format(height))
    water_count +=water_amount
        
    if sum(height) ==0: #水やりが終わったら終了
        break
print(water_count)

Submission status: Runtime Error

Solution code:
N = int(input())
height = list(map(int, input().split())) #入力：花の高さ
water_count = 0 #水やり回数
while True:
    index = [i for i, v in enumerate(height) if v >0] #水をやる必要のある花のリスト インデックスが連続した範囲でしか水やりできない
    
    # 花の水やり範囲を決定
    min_index = index[0] #インデックスの最小値
    max_index = index[0] #インデックスの最大値
    
    while True:
        if max_index+1 in index:
            max_index += 1
        else:
            break
            
    # print("index: {}".format(index)) 
    
    water_amount = min(height[min_index:max_index+1]) #あげる水の量
    for flower in range(min_index, max_index+1):
        height[flower] -= water_amount #水やりカウンタ増やす
    
    # print("height:{} ".format(height))
    water_count +=water_amount
        
    if np.sum(height) ==0: #水やりが終わったら終了
        break
print(water_count)

Submission status: Runtime Error

Solution code:
def calc_range_num(h,height,max_height,ans):#各階層にいくつ区間があるか
    for i in range(len(h)):
        if h[i]>height:
            if i+1<len(h) and h[i]>h[i+1] and h[i+1] <=height:
            #基本的には、高さが下がったところでインクリメント
                #print(i,h[i],ans+1,height)
                ans+=1
            elif i==len(h)-1 and h[i]>height:
                #print(i,h[i],ans+1,height)
                ans+=1
    height+=1
    if height==max_height:
        return ans
    else:
        ans = calc_range_num(h,height,max_height,ans)
    return ans
N = int(input())
h = list(map(int, input().split()))
max_height=max(h)
ans=0
print(calc_range_num(h,0,max_height,ans))

Submission status: Runtime Error

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

ans = 0

while sum(hn) > 0:
    ans+= 1
    flag = False
    for i in range(n):
        if hn[i] > 0:
            hn[i] -= 1
            flag = True
        else:
            break

print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np


def mizu(l, r, flowers):
    flowers[l:r+1] -= 1
    return flowers


def check_height(flowers):
    count = 0
    temp = 0
    ret = np.zeros(flowers.shape, dtype=int)
    for i in range(len(flowers)):
        if flowers[i] > 0:
            if temp == 0:
                count += 1
            ret[i] = count
        else:
            ret[i] = 0
        temp = ret[i]
    return ret

N=int(input())
flowers = np.array(list(map(int,input().split())))
count = 0
index = 1
while True:
    temp = check_height(flowers)
    l = np.min(np.where(temp == index))
    r = np.max(np.where(temp == index))

    if flowers[l] == 0 and flowers[r] == 0:
        index += 1
    flowers = mizu(l, r, flowers)
    count += 1
    if np.all(flowers == 0):
        break

print(count)

Submission status: Runtime Error

Solution code:

def count(h_list):
    if len(h_list) == 0:
        return 0
    if len(h_list) == 1:
        return h_list[0]
    if (h_list != 0).all():
        return 1 + count(h_list - 1)
    elif (h_list == 1).all():
        return 1
    return sum([count(x) for x in divide_by_zero(h_list)])


def divide_by_zero(arr):
    non_zero = []
    for ar in arr:
        if ar == 0:
            if non_zero:
                yield np.array(non_zero)
                non_zero = []
        else:
            non_zero.append(ar)
    yield np.array(non_zero)

n = int(input())
h_list = np.array([int(x) for x in input().split(" ")])
print(count(h_list))

Submission status: Runtime Error

Solution code:
#  import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
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)
    # 0以上の塊をみつける
    # その塊に水をやる
    # 以上を繰り返す
    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:
            # 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()

Submission status: Runtime Error

Solution code:
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)
    # 0以上の塊をみつける
    # その塊に水をやる
    # 以上を繰り返す
    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:
            # 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()

Submission status: Runtime Error

Solution code:
n = int(input())
h = [int(i) for i in input().split()]
c,h = 0,h+[0]
while sum(h):
	for i in range(n):
		if h[i]!=0:
			l = i
			break
	for i in range(l,n+1):
		if h[i]==0:
			m = min(h[l:i])
			for j in range(l,i): h[j]-=m
			c+=m
			for j in range(l,n+1):
				if h[j]!=0:
					l = j
					break
print(c)

Submission status: Runtime Error

Solution code:
# coding:utf-8
# C - Grand Garden

N = int(input())
h = list(map(int,input().split()))
h += [0]
ans = 0

for n in range(100):
    for i in range(N+1):
        if h[i] != 0:
            h[i] -= 1
            add = True
        else:
            if add:
                ans += 1
            add = False

print(ans)

Submission status: Runtime Error

Solution code:
# coding:utf-8
# C - Grand Garden

N = int(input())
h = list(map(int,input().split()))
h += [0]
ans = 0

for _ in range(100):
    for i in range(N+1):
        if h[i] != 0:
            h[i] -= 1
            add = True
        else:
            if add:
                ans += 1
            add = False

print(ans)

Submission status: Runtime Error

Solution code:
# coding:utf-8
# C - Grand Garden

N = int(input())
h = list(map(int,input().split()))
h += [0]
ans = 0

for _ in range(100):
    for i in range(N+1):
        if h[i] != 0:
            h[i] -= 1
            add = True
        else:
            if add:
                mini_ans += 1
            add = False

print(ans)

Submission status: Runtime Error

Solution code:
# coding:utf-8
# C - Grand Garden

N = int(input())
h = list(map(int,input().split()))
h += [0]
ans = 0

for _ in range(1000):
    mini_ans = 0
    for i in range(N+1):
        if h[i] != 0:
            h[i] -= 1
            add = True
        else:
            if add:
                mini_ans += 1
            add = False
    
    if mini_ans == 0:
        break
    else:
        ans += mini_ans

print(ans)

Submission status: Runtime Error

Solution code:
# coding:utf-8
# C - Grand Garden

N = int(input())
h =  [int(i) for i in input().split()]
h += [0]
ans = 0

for _ in range(1000000):
    mini_ans = 0
    for i in range(N+1):
        if h[i] != 0:
            h[i] -= 1
            add = True
        else:
            if add:
                mini_ans += 1
            add = False
    
    if mini_ans == 0:
        break
    else:
        ans += mini_ans

print(ans)

Submission status: Runtime Error

Solution code:
# coding:utf-8
# C - Grand Garden

N = int(input())
h =  [int(i) for i in input().split()]
h += [0]
ans = 0

while True:
    mini_ans = 0
    for i in range(N+1):
        if h[i] != 0:
            h[i] -= 1
            add = True
        else:
            if add:
                mini_ans += 1
            add = False
    if mini_ans == 0:
        break
    else:
        ans += mini_ans

print(ans)

Submission status: Runtime Error

Solution code:
def getN():
    return int(input())


def getNM():
    return map(int, input().split())


def getList():
    return list(map(int, input().split()))
import heapq

n, k = getNM()
sushi_all = []

for i in range(n):
    sushi_all.append(getList())

sushi_all.sort(reverse=True, key=lambda x: x[1])
#print(sushi_all)

m = len(list(set([x[0] for x in sushi_all[:k]])))
fm = sum([x[1] for x in sushi_all[:k]])
fx = [fm] * k + [0] * (n-k+1)
#print(fx)

heap_used = []
heap_not = []

flag = [0] * (n+1)
for sushi in [x for x in sushi_all[:k]]:
    if flag[sushi[0]] == 1:
        heap_used.append(sushi)
    else:
        flag[sushi[0]]  = 1

heap_used.sort(key=lambda x: -x[1])
#heapq.heapify(sushi_all, key=lambda x: x[1])
#print(heap_used)
for sushi in sushi_all:
    if flag[sushi[0]] == 0:
        m += 1
        fx[m] = fx[m-1] + sushi[1] - heapq.heappop(heap_used)[1]
        flag[sushi[0]] = 1
        if sum(flag) == k:
            break

gx = [fx[i] + i*i if fx[i] != 0 else 0 for i in range(len(fx))]
print(max(gx))


"""
5 2
1 9
1 7
2 6
2 5
3 1
"""
###

Submission status: Runtime Error

Solution code:
N = int(raw_input())
H = raw_input()
H = H.split()
for i, val in enumerate(H):
    H[i] = int(val)
Top = 0
Bottom = len(H)-1
T_PList = [0, Bottom]
count = max(H)
total = 0
Top_L = []
Bot_L = []
current_top = H[0]
current_bot = H[0]
if N <= 2:
    print(count)
else:
    if H[0] > H[1]:
        Top_L.append(H[0])
        current_bot = H[1]
        current_top = [-1]
    elif H[0] < H[1]:
        current_top = H[1]
        current_bot = -1

    for i in range(2,len(H),1):
        if (current_top == H[i-1]) & (H[i]>H[i-1]):
            current_top = H[i]
        elif (current_top == H[i-1]) & (H[i]<H[i-1]):
            Top_L.append(current_top)
            current_bot = H[i]
            current_top = -1
        elif (current_bot == H[i-1]) & (H[i]<H[i-1]):
            current_bot = H[i+1]
        elif (current_bot == H[i-1]) & (H[i]>H[i-1]):
            Bot_L.append(current_bot)
            current_bot = -1
            current_top = H[i]
        # print(current_top)
        # print(current_bot)
    last = len(H)
    if H[last-1] > H[last-2]:
        Top_L.append(H[last-1])
    print(sum(Top_L)-sum(Bot_L))

Submission status: Runtime Error

Solution code:
N = int(raw_input())
H = raw_input()
H = H.split()
for i, val in enumerate(H):
    H[i] = int(val)
Top = 0
Bottom = len(H)-1
T_PList = [0, Bottom]
count = max(H)
total = 0
Top_L = []
Bot_L = []
current_top = H[0]
current_bot = H[0]
if H[0] > H[1]:
    Top_L.append(H[0])
    current_bot = H[1]
    current_top = [-1]
elif H[0] < H[1]:
    current_top = H[1]
    current_bot = -1

for i in range(2,len(H),1):
    if (current_top == H[i-1]) & (H[i]>H[i-1]):
        current_top = H[i]
    elif (current_top == H[i-1]) & (H[i]<H[i-1]):
        Top_L.append(current_top)
        current_bot = H[i]
        current_top = -1
    elif (current_bot == H[i-1]) & (H[i]<H[i-1]):
        current_bot = H[i+1]
    elif (current_bot == H[i-1]) & (H[i]>H[i-1]):
        Bot_L.append(current_bot)
        current_bot = -1
        current_top = H[i]
    # print(current_top)
    # print(current_bot)
last = len(H)
if H[last-1] > H[last-2]:
    Top_L.append(H[last-1])
print(sum(Top_L)-sum(Bot_L))

Submission status: Runtime Error

Solution code:
n = input()
h2 = list(map(int, input().split()))
j2 = -2
count2 = 0


def maax(count, j, h):
    if max(h) == 0:
        print(count)
        return
    k = max(h)
    
    for i in range(len(h)):
        if h[i] == k:
            if j+1 != i:
                count = count+1
    
            j = i
            h[i] = k-1
            maax(count, j, h)
    


maax(count2, j2, h2)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
import collections

def parse(inp):
    N=int(next(inp))
    H=np.array(next(inp).split(),dtype=int)
    return H
  
def split(H):
    Splited=np.array([],dtype=int)
    SplitPoint=[]
    for i in range(len(H)):
        if(H[i]==0):
            SplitPoint.append(i)
    
    if(SplitPoint==[]):
        Splited=np.array([H])
        return Splited
    if(SplitPoint[0]==len(H)-1):
        Splited=np.array([np.delete([H],len(H)-1)])
        return Splited
    
    Splited=np.append(Splited,np.split(H,SplitPoint))
        
    for i in range(len(Splited)):
        if(Splited[i].size==0):
             continue
        elif(Splited[i][0]==0):
            Splited[i]=np.delete(Splited[i],[0])
    
    return Splited
                
def main(H):
    H=collections.deque(H)
#    print(H)

    answer=0
    
    while H:
        Hnow=H.popleft()
        if(Hnow.size==0):
            continue
#        print("now: ",Hnow)
        Hnowmin=Hnow.min()
        answer+=Hnowmin
        for i in range(len(Hnow)):
            Hnow[i]=Hnow[i]-Hnowmin
#        print("decreased: ",Hnow)
        for i in range(len(split(Hnow))):
            if(split(Hnow)[i].size!=0):
#                print("append: ",split(Hnow)[i])
                H.append(split(Hnow)[i])
#        print("H: ",H)
    return answer

print(main(split(parse(sys.stdin))))

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
import collections

def parse(inp):
    N=int(next(inp))
    H=np.array(next(inp).split(),dtype=int)
    return H
  
def split(H):
    Splited=np.array([],dtype=int)
    SplitPoint=[]
    for i in range(len(H)):
        if(H[i]==0):
            SplitPoint.append(i)
    
    if(SplitPoint==[]):
        Splited=np.array([H])
        return Splited
            
    Splited=np.append(Splited,np.split(H,SplitPoint))
    
            
    
    for i in range(len(Splited)):
        if(Splited[i].size==0):
             continue
        elif(Splited[i][0]==0):
            Splited[i]=np.delete(Splited[i],[0])
    
    return Splited
                
def main(H):
    H=collections.deque(H)
#    print(H)

    answer=0
    
    while H:
        Hnow=H.popleft()
        if(Hnow.size==0):
            continue
#        print("now: ",Hnow)
        Hnowmin=Hnow.min()
        answer+=Hnowmin
        for i in range(len(Hnow)):
            Hnow[i]=Hnow[i]-Hnowmin
#        print("decreased: ",Hnow)
        for i in range(len(split(Hnow))):
            if(split(Hnow)[i].size!=0):
#                print("append: ",split(Hnow)[i])
                H.append(split(Hnow)[i])
#        print("H: ",H)
    return answer

print(main(split(parse(sys.stdin))))

Submission status: Runtime Error

Solution code:
N = int(input())
H = list(map(int, input().split()))
num = 0
while H:
    # リストHの0でない要素を探す
    for i in range(N):
        if H[i] != 0:
            while H[i] != 0:
                H[i] -= 1
                i += 1
    num += 1
print(num)