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

Solution (Python):
n = int(input())
h = list(map(int,input().split()))
ans = 0
res = 0
for i in h:
    if i>res:
        ans += i-res
    res = i
print(ans)

Solution (Python):
N = int(input())
H = input()
H = H.split()
for i, val in enumerate(H):
    H[i] = int(val)
maxNum = max(H)
counter = 0
for i in range(maxNum):
    for j, val in enumerate(H):
        if H[j] != 0:
            H[j] -= 1
            if j < (N-1):
                if H[j + 1] == 0:
                    counter += 1
            if j == (N - 1):
                counter += 1
    
    
print(counter)

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

ans = 0
min1 = 0

for i in h:
    if i > min1:
        ans += i - min1
    min1 = i
    
print(ans)

Solution (Python):
def main():
    N = int(input())
    h_lst = list(map(int, input().split()))
    while h_lst[0] == 0:
        del h_lst[0]
        if h_lst == []:
            break
    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()

Solution (Python):
n = int(input())
h = list(map(int, input().split(' ')))
ans = 0
min1 = 0
for hk in h:
    if hk > min1:
        ans += hk - min1
    min1 = hk
print(ans)

Solution (Python):
def solve(height):
    
    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)
    
def solve_zero(height): 
    print(0)

if __name__ == "__main__":
    N = int(input()) 
    height = list(map(int, input().split())) 
    if sum(height) == 0:
        solve_zero(height)
    else:
        solve(height)

Solution (Python):
n = int(input())
ks = list(map(int, input().split(' ')))

prev = 0
ret = 0
for k in ks:
    if prev < k:
        ret += k - prev
    prev = k

print(ret)

Solution (Python):
def solve(lis):
    
    if is_all_zero(lis):
        return 0

    
    zero_index = search_zero_index(lis)
    if zero_index != -1:
        return solve(lis[:zero_index]) + solve(lis[zero_index+1:])

    
    min_num = min(lis)
    lis = list(map(lambda x: x - min_num, lis))
    return min_num + solve(lis)

def is_all_zero(lis):
    return len(list(filter(lambda x : x == 0, lis))) == len(lis)

def search_zero_index(lis):
    if 0 in lis:
        return lis.index(0)
    else:
        return -1

if __name__ == "__main__":
    N = int(input())
    h_list = list(map(int, input().split()))
    print(solve(h_list))

Solution (Python):

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

counter = min(h)
h = list(map(lambda x, y=min(h): x-y, h))

while sum(h) > 0:
    start = False
    
    for i in range(len(h)):
        if not start and h[i] > 0:
            start = True
            start_index = i
            continue
        elif start and h[i] == 0:
            start = False
            water = min(h[start_index:i])
            counter += water
            h = h[:start_index] + \
                list(map(lambda x, y=water: x - y, h[start_index:i])) + h[i:]
    if start:
        water = min(h[start_index:])
        counter += water
        h = h[:start_index] + \
            list(map(lambda x, y=water: x - y, h[start_index:]))
print(counter)

Solution (Python):
N=int(input())
h=list(map(int,input().split()))
h.insert(0,0)
count=0
def find(h,x):
    count=0
    for i in range(0,N):
        if h[i]<x and h[i+1]>=x:
            count=count+1
    return count
for j in range(1,max(h)+1):
    count=count+find(h,j)
print(count)

Solution (Python):
input()
H = list(map(int, input().split()))
count = 0
while any([h != 0 for h in H]):
    n = 0
    prev = 0
    for h in H:
        if prev == 0 and h != 0:
            n += 1
        prev = h
    count += n
    H = list(map(lambda x: x if x == 0 else x - 1, H))
print(count)

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


count = 0
while 1:
    if sum(h) == 0:
        break
    else:
        
        
        if 0 in h:
            while 1:
                num = h.pop(0)
                if num != 0:
                    h.insert(0, num)
                    if 0 in h:
                        i = h.index(0)
                        h = list(map(lambda x: x - 1, h[:i]))+h[i:]
                    else:
                        h = list(map(lambda x: x-1, h))
                    break
            
            
            count += 1
        else:
            h = list(map(lambda x: x-1, h))
            
            count += 1

print(count)

Solution (Python):
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:
            
                
                ans+=1
            elif i==len(h)-1:
                
                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))

Solution (Python):
def solve(h):
    if len(h)<2:
        return h[0]
    s=[i for i in range(len(h))if h[i]<1]
    s=[-1]+s+[len(h)]
    a=0
    for l in[h[i+1:j]for i,j in zip(s,s[1:])]:
        if not l:continue
        v=min(l)
        l=[t-v for t in l]
        a+=v+solve(l)
    return a
input()
print(solve(list(map(int,input().split()))))

Solution (Python):
def solve(h):
    if len(h)<2:
        return h[0]
    s=[i for i in range(len(h))if h[i]<1]
    s=[-1]+s+[len(h)]
    a=0
    for l in[h[i+1:j]for i,j in zip(s,s[1:])]:
        if not l:continue
        v=min(l)
        l=[t-v for t in l]
        a+=v+solve(l)
    return a
input()
print(solve(list(map(int,input().split()))))

Solution (Python):
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b



def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)



def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


def main():
    N = int(input())
    H = list(map(int, input().split())) + [0]

    ans = 0
    change = True
    while change:
        change = False

        left = 0
        found = False
        for i in range(len(H)):
            if H[i] != 0:
                if not found:
                    found = True
                    left = i

            if H[i] == 0:
                if found:
                    ans += 1
                    change = True
                    for j in range(left, i):
                        H[j] -= 1

                    left = i + 1
                    found = False

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
h = list(map(int, input().split()))
ans = 0
for i in range(max(h)):
    flag = False
    for num in h:
        if num > i and flag == False:
            flag = True
            ans += 1
        elif num > i and flag == True:
            pass
        else:
            flag = False
print(ans)

Solution (Python):
n_flowers = int(input())
heights = [int(h) for h in input().split()]
diff = [h for h in heights]
n_pour = 0

for i in range(n_flowers):
  while diff[i] > 0:
    for j in range(i, n_flowers):
      if diff[j] <= 0:
        diff[i:j] = [d - 1 for d in diff[i:j]]
        n_pour += 1
        break
      elif j == n_flowers - 1:
        diff[i:n_flowers] = [d - 1 for d in diff[i:n_flowers]]
        n_pour += 1
        break

print(n_pour)

Solution (Python):
n = int(input())
hs = list(map(int, input().split()))
hs.append(0)
ans = 0
for i in range(len(hs)):
    if len(set(hs))==1 and 0 in hs:
        break
    elif hs[i]==0:
        continue
    else:
        while True:
            if hs[i]==0:
                break
            for j in range(i,len(hs)):
                if hs[j] ==0:
                    for k in range(i,j):
                        hs[k]-=1
                    ans+=1
                    break
print(ans)

Solution (Python):
def dfs(_h):
    if len(_h) == 0:
        return
    min_h = min(_h)
    global ans
    ans += min_h
    tmp = []
    for i in range(len(_h)):
        if _h[i] == min_h:
            tmp.append(i)
    tmp.append(N)
    head = 0
    sp = []
    for i in tmp:
        sp.append([x - min_h for x in _h[head:i]])
        head = i+1
    for s in sp:
        dfs(s)

N = int(input())
h = [int(x) for x in input().split()]
ans = 0
dfs(h)
print(ans)

Solution (Python):
n = int(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
        elif flag is True:
            break

print(ans)

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

c = 0
l = 0
while True:
    
    while l != n and hn[l] == 0:
        l += 1
    if l == n:
        break

    
    r = l
    min_num = 1e3
    while r != n and hn[r] != 0:
        min_num = min(min_num, hn[r])
        r += 1
    
    
    for i in range(l, r):
        hn[i] -= min_num
    c += min_num

print(c)

Solution (Python):
n = int(input())
f = list(map(int,input().split()))
Garden = [0] * len(f)
Count = 0
cflag = False

while(1):
    for index,item in enumerate(f):
        if item != Garden[index]:
            Garden[index] += 1
            cflag = True
        else:
            if cflag == True:
                Count += 1
            cflag = False
            
    if cflag == True:
        Count += 1
        cflag = False
        
    if f == Garden:
        break

print(Count,end="")

Solution (Python):



def count_mountain(li):
    pre = False
    count = 0
    for i in range(1, max(li) + 1):
        for j in range(len(li)):
            now = li[j] >= i
            if pre == False and now == True:
                count += 1
            pre = now
        pre = False

    return count


def main():
    n = int(input())
    h = list(map(int, input().split()))

    print(count_mountain(h))


if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np
N = int(input())
h = np.array([int(i) for i in input().split()])
counter = 0

'''
ゼロでないhのクラスタを見つけ、そのクラスタの最小値を引いていく

e.g.
[3 1 2 3 1]ならば、まず全てゼロでは無いので、最小値1を全体に対して引く。
すると、[2 0 1 2 0]となる。
このとき、[{2} 0 {1 2} 0]といった具合に、ゼロでない部分をまとめていく。
まとめた部分に対しても、同様の処理を行なっていく。
'''


def clustering(h):
    cluster = []
    cluster_pos = []
    small_cluster = []
    small_cluster_pos = []
    for index, value in enumerate(h):
        if(value != 0):
            small_cluster.append(value)
            small_cluster_pos.append(index)
        else:
            cluster.append(small_cluster)
            cluster_pos.append(small_cluster_pos)
            small_cluster = []
            small_cluster_pos = []
    if(value != 0):
        cluster.append(small_cluster)
        cluster_pos.append(small_cluster_pos)

    return cluster, cluster_pos

while(np.sum(h)>0):
    cluster, cluster_pos = clustering(h)
    for i in range(len(cluster)):
        if(len(cluster[i])==0):
            continue

        minimum = np.min(cluster[i])
        counter += minimum
        h[cluster_pos[i]]-= minimum

print(counter)

Solution (Python):
N = int(input())
H = [int(_) for _ in input().split()]

count = 0
while True:
    for n in range(N):
        if H[n] == 0:
            continue
        elif n == N-1:
            count += 1
        elif H[n+1] == 0:
            count += 1
        H[n] -= 1
    if sum(H) == 0:
        break
print(count)

Solution (Python):
import sys
input = sys.stdin.readline

def lsearch(h): 
    for i in range(len(h)):
        if h[i] != 0:
            return i
    return len(h)-1

def rsearch(h, l): 
    for i in range(l, len(h)):
        if h[i] == 0:
            return i
    return len(h)

n=int(input())
h=list(map(int, input().split()))
cnt=0
while any(h):
    l=lsearch(h)
    r=rsearch(h, l)
    for i in range(l, r):
        h[i] -= 1
    cnt+=1
print(cnt)

Solution (Python):
N=int(input())
HN=list(map(int,input().split()))

rtn=0
btm=0
top=0
for Hi in HN:
  if top<Hi:
    top=Hi
  elif top==btm and btm>Hi:
    top=Hi
    btm=Hi    
  elif top>Hi:
    rtn+=top-btm
    top=Hi
    btm=Hi
rtn+=top-btm
print(rtn)

Solution (Python):

from sys import stdin



s_in = lambda: stdin.readline()[:-1]  
d_in = lambda: int(stdin.readline())  
ds_in = lambda: list(map(int, stdin.readline().split()))  


def search(target):
    if len(target) == 0:
        return 0
    elif len(target) == 1:
        return target[0]
    else:
        count = 0
        if 0 not in target:
            for i in range(len(target)):
                target[i] -= 1
            count += 1
        cand = []
        while 0 in target:
            idx = target.index(0)
            cand.append(target[:idx])
            target = target[idx+1:]
        cand.append(target)
        for s in cand:
            count += search(s)
        return count


N = d_in()
h_list = ds_in()

print(search(h_list))

Solution (Python):
n=int(input())
H=list(map(int,input().split()))

co=0
su=sum(H)
hana=0
while su>0 :
    
    if H[hana]>0 :
        H[hana]-=1
        su-=1
        if hana==n-1 or H[hana+1]==0 :
            co+=1
    hana+=1
    if hana==n :
        hana=0

print(co)

Solution (Python):

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

def f(hn):
    cnt = min(hn)
    hn2 = [h - cnt for h in hn]
    sub_hn = []
    tmp = []
    for h in hn2:
        if h == 0:
            if len(tmp) > 0:
                sub_hn.append(tmp)
                tmp = []
        else:
            tmp.append(h)

    if len(tmp) > 0:
        sub_hn.append(tmp)

    return cnt + sum(map(f, sub_hn))

print(f(hn))

Solution (Python):
N = int(input())
hList = [int(x) for x in input().split()]

def calc(x:list, count:int, before:int):
    if len(x) == 0:
        return 0
    sortedx = sorted(x)
    count = count + (sortedx[0] - before)
    afterList = []

    for _ in range(sortedx.count(sortedx[0])):
        idx = x.index(sortedx[0])
        if len(x[0:idx]) > 0:
            afterList.append(x[0:idx])
        del x[0:idx+1]
    if len(x) > 0:
        afterList.append(x)

    for x in afterList:
        count = calc(x, count, sortedx[0])

    return count

print(calc(hList, 0, 0))

Solution (Python):
import numpy as np
N=int(input())
h=np.array(list(map(int,input().split())))
ans=0
now=0
while(now<N):
    init=now
    while(1):
        if h[now]==0:
            if now==init:
                now+=1
                break
            else:
                h[init:now]-=1
                ans+=1
                now=init
                break
        elif now==N-1:
            h-=1
            ans+=1
            now=init
            break            
        now+=1
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

n = int(input())
H = list(map(int, input().split()))
hnow = 0
cou = 0

for i in range(n):
    h = H[i]
    if h > hnow:
        cou += (h - hnow)
        hnow = h
    elif h < hnow:
        hnow = h

print(cou)

Solution (Python):
n = int(input())
h = list(map(int, input().split()))
h.append(0)
c = 0
while sum(h):
    for i in range(len(h)):
        if (0 < h[i] and h[i + 1] == 0):
            c += 1
    for i in range(len(h)):
        if h[i] != 0:
            h[i] -= 1

print(c)

Solution (Python):
n = int(input())
h = list(map(int,input().split())) + [0]
s = h[0]
for i in range(n):
    s += abs(h[i+1]-h[i])
print(s//2)

Solution (Python):
N = int(input())

count = 0
p = 0

for h in map(int, input().split()):
    count += max(0, h-p)
    p = h

print(count)

Solution (Python):
def count(h_list):
    if len(h_list) == 0:
        return 0
    if len(h_list) == 1:
        return h_list[0]
    if 0 not in h_list:
        return 1 + count([x-1 for x in h_list])
    elif all(x == 1 for x in h_list):
        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 non_zero
                non_zero = []
        else:
            non_zero.append(ar)
    yield non_zero


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

Solution (Python):
N = int(input())
h = list(map(int,input().split()))
count = 0

while max(h) > 0:
    hmax = max(h)
    hmaxi = h.index(hmax)
    h[hmaxi] -= 1
    count += 1
    for i in range(N-hmaxi-1):
        if hmaxi == N-1:
            break
        elif h[hmaxi+i+1] == hmax:
            h[hmaxi+i+1] -= 1
        else:
            break

print(count)

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

h.append(0)
ans = 0
while(sum(h) > 0):
    interval_start = 0
    interval_end = 0
    interval_min = 101
    in_interval = False
    for i in range(N + 1):
        if not in_interval:
            if h[i] == 0:
                continue
            else:
                in_interval = True
                interval_start = i
                interval_min = h[i]
        else:
            if h[i] == 0:
                in_interval = False
                interval_end = i
                ans += interval_min
                for j in range(interval_start, interval_end):
                    h[j] -= interval_min
            else:
                interval_min = min(interval_min, h[i])           
print(ans)

Solution (Python):
a = int(input())
b = input().split()
c = []
for i in b:
    c.append(int(i))

max = 0
ans = 0
for i in c:
    if max < i:
        ans += i - max
    max = i

print(ans)

Solution (Python):
import math
import sys
from operator import itemgetter








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


ans = 0

while (max(h) > 0):
	k = 0
	j = 0
	for i in range(len(h)):
		if (h[i] > 0):
			k = i
			break
	j = 0
	for i in range(k, len(h)):
		if (h[i] != 0):
			j += 1
		else:
			break

	for i in range(k, k+j):
		h[i] -= 1
	ans += 1
print(ans)

Solution (Python):


def solve(h):
    if len(h) == 0:
        return 0
    if len(h) == 1:
        return h[0]
    if max(*h) == 0:
        return 0
    min_v = min(*h)
    if min_v != 0:
        return min_v + solve([a - min_v for a in h])
    zero_ind = h.index(0)

    aaa, bbb = h[:zero_ind], h[zero_ind + 1:]
    return solve(aaa) + solve(bbb)


def main():
    input()
    h = [int(a) for a in input().split()]

    res = solve(h)
    print(res)


if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
H=[int(i) for i in input().split()]
W=[0]*N
num=0
i=0
while i<N:
    if W[i]==H[i]:
        i+=1
        continue
    for j in range(i,N):
        if W[j]==H[j]:
            break
        W[j]+=1
    num+=1
print(num)

Solution (Python):
N = int(input())
H = [int(i) for i in input().split()]




mizu_num=0
renzoku= False
while(True):
    
    for i in range(len(H)):
        if H[i]>0:
            H[i]-=1
            renzoku=True
        else:
            if renzoku==True and i!=0:
                mizu_num+=1
            renzoku=False
    
    if renzoku==True:
        mizu_num+=1

    

    if not any(H):
        break

print(mizu_num)

Solution (Python):
N = int(input())
H = list(map(int,input().split()))
ans = 0
for i in range(N-1):
  if(H[i] > H[i+1]):
    ans += H[i] - H[i+1]
print(ans + H[-1])

Solution (Python):




def f(flowers:list):
  li = []
  n_li = []
  for flower in flowers:
    if flower == 0:
      if len(n_li) != 0:
        li += [n_li]
        n_li = []
    else:
      n_li += [flower-1]
  li += [n_li] if len(n_li) != 0 else []
  count = len(li)
  if li == []:
    return 0

  _sum = 0
  for l in li:
    _sum += f(l)
  return count + _sum



if __name__=='__main__':
  N = int(input())
  flowers = [int(x) for x in input().split()]
  print(f(flowers))

Solution (Python):
n = int(input())
lst = input().split(" ")
a = [int(item) for item in lst]
ans = 0
while sum(a) > 0:
    left = 0
    right = -1
    for i in range(len(a)):
        if not a[i] == 0:
            left = i
            break
    for i in range(left, len(a)):
        if a[i] == 0:
            right = i
            break
    if -1 == right:
        right = len(a)
    right = right - 1
    min_val = a[left]
    for i in range(left, right + 1):
        min_val = min(min_val, a[i])
    ans += min_val
    for i in range(left, right + 1):
        a[i] -= min_val
print(ans)

Solution (Python):
import time
s=0
def min(arr):
    m=arr[0]
    for i in range(1,len(arr)):
        if m>arr[i]:
            m=arr[i]
    return m

def mizu(arr):
    global s
    if len(arr)==1:
        
        s+=arr[0]
    elif len(arr)>1:
        m=min(arr)
        b=[]

        for i in range(len(arr)):
            arr[i]-=m
        s+=m

        for i in range(len(arr)):
            if arr[i]!=0:
                b.append(arr[i])
            else:
                mizu(b)
                b=[]
        if arr[-1]!=0:
            mizu(b)

n=int(input())
k=list(map(int,input().split()))
mizu(k)
print(s)

Solution (Python):
class Solver(object):
    def __init__(self):
        self._input()
        self._solve()

    def _input(self):
        self.n = int(input())
        self.h_s = list(map(int, input().split(" ")))

    def _solve(self):
        counter = 0
        while True:
            watered = False
            watering = False
            for i in range(self.n):
                if self.h_s[i] > 0:
                    if not watering:
                        watered = True
                        watering = True
                        counter += 1
                    self.h_s[i] -= 1
                else:
                    if watering:
                        watering = False
            if watered == False:
                break
        print(counter)


if __name__ == "__main__":
    Solver()

Solution (Python):
N = int(input())
H = [int(n) for n in input().split()]

def count_divided(H):
    indices = [i for i, h in enumerate(H) if h > 0]
    counter = 0
    for i in range(len(indices) - 1):
        if H[indices[i] + 1] <= 0:
           counter += 1
    if len(indices) != 0:
        if indices[-1] == len(H) - 1:
            counter += 1
        if H[-1] <= 0:
            counter += 1
    return counter

def all_negative(H):
    flag = True
    for h in H:
        if h >= 0:
            flag = False
    return flag

counter = 0

while(not all_negative(H)):
    counter += count_divided(H)
    H = [h - 1 for h in H]

print(counter)

Solution (Python):
N = int(input())
h = list(map(int ,input().split(" ")))
flag = 0
h.append(0)
ct = 0
while sum(h):
    for i in range(len(h)):
        if h[i]:
            h[i] = h[i] - 1
            flag = 1
        else:
            if flag:
                ct += 1
                flag = 0
print(ct)

Solution (Python):
N = int(input())
h = []
h = list(map(int, input().split()))

def l_r(lis):
    l = 0
    r = 0
    for i in range(len(lis)):
        if lis[i] > 0:
            l = i
            break
    if l == len(lis)-1:
        r = l
    else:
        for i in range(l+1,len(lis)):
            if lis[i] <= 0:
                r = i
                break
            else:
                r = len(lis)-1
    return l,r

if h == []:
    print(0)
else:
    counta = 0
    while True:
        if all([x<=0 for x in h]):
            break
        else:
            l,r = l_r(h)
            for i in range(l,r+1):
                h[i] = h[i] - 1
            counta += 1
    print(counta)

Solution (Python):
N = int(input())
h = []
h = list(map(int, input().split()))

def l_r(lis):
    l = 0
    r = 0
    for i in range(len(lis)):
        if lis[i] > 0:
            l = i
            break
    if l == len(lis)-1:
        r = l
    else:
        for i in range(l+1,len(lis)):
            if lis[i] <= 0:
                r = i
                break
            else:
                r = len(lis)-1
    return l,r

if h == []:
    print(0)
else:
    counta = 0
    while True:
        if all([x<=0 for x in h]):
            break
        else:
            l,r = l_r(h)
            for i in range(l,r+1):
                h[i] = h[i] - 1
            counta += 1
    print(counta)

Solution (Python):
n = int(input())
arr = [int(i) for i in input().split()]
g_count = 0

def sub(arr, count):
    while 1:
        if arr == []:
            return count
        elif 0 in arr:
            index = arr.index(0)
            arr1 = arr[0:index]
            arr2 = arr[index + 1:len(arr)]
            count = count + sub(arr1, 0) + sub(arr2, 0)
            return count
        else:
            arr = [i - 1 for i in arr]
            count += 1 

print(sub(arr, 0))

Solution (Python):


N = int(input())
H = input().split()
h = []
r, l = 0, 0
times = 0

for i in range(N):
    h.append(int(H[i]))

h.append(0)

def decidePosition(h):
    m = len(h)
    n = len(h)
    flag = 0
    for i in range(len(h)):
        if h[i] != 0 and flag == 0:
            m = i
            flag = 1
        elif h[i] == 0 and flag == 1:
            n = i
            break
    return m, n

while r != len(h):
    r, l = decidePosition(h)
    for i in range(l-r):
        i += r
        h[i] = h[i] - 1
    times += 1

print(times-1)

Solution (Python):









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



ans = h[0]
for i in range(1,N):
  ans += max(0, h[i]-h[i-1])
  

print ans

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

cnt = h[0]
for i in range(len(h) - 1):
    sa = h[i+1] - h[i]
    if sa > 0:
        cnt += sa

print(cnt)

Solution (Python):
n = int(input())
h=[0]+list(map(int,input().split()))


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

Solution (Python):

import sys
import numpy as np










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)
    
    
    
    
    count = 0
    while True:
        
        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()

Solution (Python):
import sys
import numpy as np










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)
    
    
    
    
    count = 0
    while True:
        
        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()

Solution (Python):
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)
    
    
    
    
    count = 0
    while True:
        
        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()

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

ans = 0
for i in range(N-1):
  if(H[i] > H[i+1]):
    ans += H[i] - H[i+1]
print(ans + H[-1])