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

def solve(heights, beg, end) -> int:
    if beg >= end: return 0
    try:
        idx = beg + heights[beg:end].index(0)
        return  solve(heights, beg, idx) + solve(heights, idx+1, end)
    except ValueError:
        pass
    
    minh = min(heights[beg:end])
    for i in range(beg, end):
        heights[i] -= minh
    return minh + solve(heights, beg, end)

cnt = solve(heights, 0, len(heights))
print(cnt)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(max(a)):
    cnt = 1 if a[0] - i >= 1 else 0
    for j in range(1, n):
        if a[j-1] - i < 1 and a[j] - i >= 1:
            cnt += 1
    ans += cnt
print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=[0 for _ in range(n)]

ans=0

while a!=b:
  ch=0
  for i in range(n):
    if b[i]+1<=a[i]:
      b[i]+=1
      ch+=1
    elif b[i]+1>a[i] and ch>0:
      ch=0
      ans+=1
      
    if ch>0 and i==n-1:
      ans+=1
      
print(ans)

Solution (Python):
n = int(input())
lst = list(map(int, input().split()))
hlst = [0]
for h in lst:
  if hlst[-1] != h:
    hlst.append(h)
hlst.append(0)
ans = 0
l = len(hlst)
for i in range(1, l - 1):
  if hlst[i - 1] < hlst[i] and hlst[i] > hlst[i + 1]:
    ans += hlst[i]
  elif hlst[i - 1] > hlst[i] and hlst[i] < hlst[i + 1]:
    ans -= hlst[i]
print(ans)

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

cnt=0

while 1:
    for i in range(len(h)-1,0,-1):
        if h[i]==h[i-1]:
            h.pop(i)

    if len(h)==1:
        break
    
    mp = h.index(max(h))

    if mp==0:
        cnt += h[mp] - h[mp+1]
    elif mp==len(h)-1:
        cnt += h[mp] - h[mp-1]
    else:
        cnt += h[mp] - max(h[mp-1],h[mp+1])

    h.pop(mp)

print(cnt+h[0])

Solution (Python):
import sys
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7

n = I()
hl = LI()




































ans = hl[0]
for i in range(1, n):
    ans += max(0, hl[i] - hl[i - 1])
print(ans)

Solution (Python):
import sys
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7

n = I()
hl = LI()
wl = []
count = 0

len_h = len(hl)

def m1(n):
    if n > 0:
        n = n - 1
    else:
        n = 0
    return n

max_hana = max(hl)
if max_hana == 0:
    print(0)
    sys.exit()

for i in range(max_hana):
    for j in range(len_h):
        if j == 0:
            if hl[j] > 0:
                count += 1
        else:
            if hl[j] > 0 and hl[j-1] == 0:
                count += 1
            elif hl[j] > 0 and hl[j-1] == 0:
                count += 1
    hl = list(map(m1, hl))

print(count)

Solution (Python):
a = int(input())
b = list(map(int,input().split()))
b.append(0)
ans = 0
while not max(b) == 0:
  for i in range(len(b)):
    if b[i] > 0 and b[i+1] <= 0:
      ans += 1
  b = list(map(lambda x: x-1,b))
print(ans)

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

cnt = 0

a = 0
for i in range(n-1):
  if h[i+1] < h[i]:
    cnt += h[i]-a
    a = h[i+1]
cnt += h[-1]-a
print (cnt)

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

ans = 0

for i in range(max(h)):
    if h[0] == 0:
        zeroFlag = True
        for j in range(0, n):
            if h[j] == 0:
                zeroFlag = True
            if h[j] != 0 and zeroFlag == True:
                ans += 1
                zeroFlag = False
            if h[j] > 0:
                h[j] -= 1
    else:
        numFlag = True
        for j in range(0, n):
            if h[j] == 0:
                numFlag = True
            if h[j] != 0 and numFlag == True:
                ans += 1
                numFlag = False
            if h[j] > 0:
                h[j] -= 1

print(ans)

Solution (Python):
n = int(input())
hh = list(map(int, input().split()))
cnt = 0

while hh.count(0) != n:
  a = [i for i, x in enumerate(hh) if x == 0]
  
  if a != []:
    for i in range(len(a)-1):
      if a[i]+1 != a[i+1]:
        for j in range(a[i]+1 , a[i+1]):               
          hh[j] -= 1
        cnt += 1
        
    if a[0] != 0:       
      for j in range(a[0]):
        hh[j] -= 1
      cnt +=1
    
    if a[-1] != n-1:
      for j in range(a[-1]+1, n):      
        hh[j] -= 1
      cnt += 1
        
  else:
    for i in range(n):
      hh[i] -= 1
    cnt += 1
             
print(cnt)

Solution (Python):


import collections

def main():
    n = int(input())
    h = input_list()
    count = 0
    while True:
        if list(set(h)) == [0]:
            break
        count += 1
        f = False
        for i, v in enumerate(h):
            if v == 0:
                if f:
                    break
            if v > 0:
                h[i] -= 1
                f = True
    print(count)


def input_list():
    return list(map(int, input().split()))


def input_list_str():
    return list(map(str, input().split()))


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
h = list(map(int,input().split()))
g = [0]*n
ans = 0
l = 0
while not h == g:
  if not g[l] == h[l]:
    r = l+1
    while r <= n-1 and not g[r] == h[r]:
      r += 1
    
    for i in range(l,r):
      g[i] += 1
    ans += 1
  if g[l] == h[l]:
    l += 1
print(ans)

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

    count = 0
    h.append(0)
    for j in range(max(h)):
        for i in range(n):
            if h[i] != 0 and h[i+1] == 0:
                count += 1
            if h[i] != 0:
                h[i] -= 1
    
    print(count)



if __name__ == '__main__':
    main()

Solution (Python):



import collections
import bisect
import math
import fractions
import collections
import itertools
from functools import reduce

def main():
    n = int(input())
    h = list(input_list())
    count = 0
    while True:
        if list(set(h)) == [0]:
            break
        flag = False
        count += 1
        for i, v in enumerate(h):
            if v != 0:
                flag = True
                h[i] -= 1
            else:
                if flag:
                    break
    print(count)

def minus(numbers):
    for i, n in enumerate(numbers):
        if n == 0:
            break
        numbers[i] -= 1
    return numbers


def input_list():
    return list(map(int, input().split()))


def input_list_str():
    return list(map(str, input().split()))


if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
h=[0]+list(map(int,input().split()))+[0]
ma=0
mi=float('inf')
ans=0
for i in range(n+1):
  if h[i]<=h[i+1]:
    ma=max(ma,h[i+1])
    mi=min(mi,h[i])
  else:
    ans+=ma-mi
    ma=h[i+1]
    mi=h[i+1]
print(ans)

Solution (Python):

"""
Created on Tue Sep 15 00:43:39 2020

@author: liang
"""

N = int(input())
H = [int(x) for x in input().split()]

ans = 0
for i in range(max(H)):
    count = 0
    flag = False
    for j in range(N):
        if i < H[j]:
            if flag == False:
                count += 1
            flag = True
        else:
            flag = False
    ans += count
print(ans)

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

def solve(l):
    if len(l) == 1:
        return l[0]

    min_l = min(l)
    ans = min_l
    new_l = []
    for i in l:
        i -= min_l
        if i == 0:
            if len(new_l) > 0:
                    ans += solve(new_l)
                    new_l = []
        else:
            new_l.append(i)
    if len(new_l) > 0:
        ans += solve(new_l)

    return ans

print(solve(a))

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

res=0
temp = 0
for i in range(n):
    if al[i] >=temp:
        res += al[i]-temp
        temp = al[i]
    else:
        temp = al[i]

print(res)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time, copy

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  

n = ni()
h = na()
ans = h[0]
for i in range(1,n):
  ans += max(0, h[i]-h[i-1])
print(ans)

Solution (Python):
def dfs(left,right):
    if right-left==1:
        return h[left]
    elif right-left<=0:
        return 0
    else:
        ret=min(h[left:right])
        k=0
        for i in range(left,right):
            h[i]-=ret
            if h[i]==0:
                k=i
        ret+=dfs(left,k)
        ret+=dfs(k+1,right)
        return ret

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

print(dfs(0,N))

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

def grouping(v):
    
    groups = []
    tmp = 0
    zeros = 0
    for n in v:
        if n == 1:
            if zeros != 0:
                groups.append(zeros)
                zeros = 0
            tmp += 1
        elif n == 0:
            if tmp != 0:
                groups.append(tmp)
                tmp = 0
            zeros -= 1
    
    
    if tmp != 0:
        groups.append(tmp)
    if zeros != 0:
        groups.append(zeros)
    
    groups = [1 if x > 0 else 0 for x in groups]
    return groups

ans = 0
for i in range(max(H)):
    H = list(map(lambda x:x-1, H))
    vector = [1 if x >= 0 else 0 for x in H]
    ans += sum(grouping(vector))
print(ans)

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

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

Solution (Python):
import numpy as np
N= int(input())
h = list(map(int,input().split()))
s=h[0]
for i in range(N-1):
    if h[i]<=h[i+1]:
        s=s+h[i+1]-h[i]
        
print(s)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)

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


def need(L):
    if L == [] or max(L) == 0:
        return 0
    if min(L) > 0:
        l = [L[i]-1 for i in range(len(L))]
        return need(l)+1
    else:
        i = L.index(0)
        l = L[:i]
        m = L[i+1:]
        return need(l)+need(m)


print(need(H))

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

Solution (Python):

import sys
import math
import decimal
import itertools
from itertools import product
from functools import reduce

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

    ans = 0
    while h != [0] * N:
        if h[0] != 0:
            ans += 1
        for i in range(1, N):
            if h[i - 1] == 0 and h[i] != 0:
                ans += 1
        for i in range(N):
            if h[i] > 0:
                h[i] -= 1
    print(ans)


def input():
    return sys.stdin.readline()[:-1]
def gcd(*numbers):
    return reduce(math.gcd, numbers)
def lcm_base(x, y):
    return (x * y) // math.gcd(x, y)
def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)
def sort_zip(a:list, b:list):
    z = zip(a, b)
    z = sorted(z)
    a, b = zip(*z)
    a = list(a)
    b = list(b)
    return a, b

if __name__ == '__main__':
    main()

Solution (Python):
def grand_garden(n, h):

    count = 0
    j = 100
    for i in range(n):
        if h[i] != 0:
            j = i
            break
    if j == 100:
        return count

    while h[j] != 0:
        l = j  
        r = l  
        for k in range(l, n):  
            if k == n-1 and h[k] != 0:
                r = n
            elif h[k] == 0:
                r = k
                break

        min_val = min(h[l:r])  
        for x in range(l, r):  
            h[x] -= min_val
        count += min_val  
        if h[j] == 0:  
            for t in range(j, n):
                if h[t] != 0:
                    j = t
                    break

        if j >= n:
            break
    return count


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

    answer = grand_garden(n, h)
    print(answer)


'''
数列に0が含まれない最大の幅を取得(0ではない最小の左から最大の右でもよいのか)
その幅に含まれる要素高さを1ずつへらす、
count +=1

0 <= l,r <= n-1
while True:
    if h[i] == 0:
        l += 1
        i += 1
        continue
        if l >= n:
            break

   for i in range(n):
        if h[i] != 0:
            l = i
            r = l
            for j in range(l, n):
                if h[j] != 0:
                    r += 1
            for k in range(l, r):
                h[k] -= min(h[l:r])


            count += 1


'''


if __name__ == "__main__":
    main()

Solution (Python):








def solve(N, hi):
    ans = 0
    for i in range(max(hi)):
        flg = False
        for n in range(N):
            if hi[n] != 0:
                if not flg:
                    flg = True
                    ans += 1
                hi[n] -= 1
            else:
                flg = False
    print(ans)


if __name__ == '__main__':
    
    N = int(input())
    
    hi = [int(i) for i in input().split()]
    
    
    solve(N, hi)

Solution (Python):
N = int(input())
h = list(map(int, input().split()))
max_ = 100*50
h = [0] + h + [0]
cnt = 0
if max(h) == 0:
    print(0)
    exit()
for _ in range(max_):
    for i in range(1, N+1):
        if h[i] == 0 and h[i-1] != 0:
            cnt += 1
            break
        if h[i] > 0:
            h[i] -= 1
            if h[i] == 0 and h[i+1] == 0:
                cnt += 1
                break
        if i == N:
            cnt += 1
            break
    if sum(h) == 0:
        break
print(cnt)

Solution (Python):
n=int(input())
*h,=map(int, input().split())
import numpy as np
h=np.array(h)

def rec(hh):
    if len(hh)==0:
        return 0
    if len(hh)==1:
        return hh[0]
    i=np.argmin(hh)
    res=hh[i]
    l=rec(hh[:i]-hh[i])
    r=rec(hh[i+1:]-hh[i])
    return res+l+r
print(rec(h))

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

def dfs(a):
  if not a:
    return 0
  cnt = 0
  while True:
    if 0 in a:
      z = [i for i,x in enumerate(a) if x==0]
      cnt += dfs(a[:z[0]])
      for i in range(1,len(z)):
        cnt += dfs(a[z[i-1]+1:z[i]])
      cnt += dfs(a[z[-1]+1:])
      return cnt
    else:
      a = [x-1 for x in a]
      cnt += 1

print(dfs(h))

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

cnt = 0
while True:
    zero_ind = []
    for i in range(n+2):
        if h[i] == 0:
            zero_ind.append(i)

    start, end = [], []
    for i in range(len(zero_ind)-1):
        if zero_ind[i+1] - zero_ind[i] > 1:
            start.append(zero_ind[i]+1)
            end.append(zero_ind[i+1])

    for s, e in zip(start, end):
        for i in range(s, e):
            h[i] -= 1
        cnt += 1

    if h.count(0) == n+2:
        break
print(cnt)

Solution (Python):
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007



def main():
	n = int(input())
	
	
	al=lin()
	ch=0
	ret=0
	for h in al:
		ret += max(h-ch, 0)
		ch = h
	return ret
	

isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

Solution (Python):
n = int(input())
h = list(map(int,input().split()))
h.append(101)
ans = 0
start = 0
i = 0
while sum(h[:n])!=0:
    if h[i]!=0:
        h[i] -= 1
    if h[i]==0:
        start = i+1    
    if h[i]>=h[i+1] or i==n-1:
        ans += 1
        i = start
        continue
    i += 1
print(ans)

Solution (Python):

url = "https://atcoder.jp//contests/abc116/tasks/abc116_c"

def main():
    input()
    h = list(map(int, input().split()))
    h.append(0)
    count = 0
    def start_end(h):
        s_e = [-1,-1]
        for i in range(len(h)):
            if h[i] != 0 and s_e[0] == -1:
                s_e[0] = i
            elif h[i] == 0 and s_e[0] != -1:
                s_e[1] = i
                break
        return s_e
    while True:
        s_e = start_end(h)
        if s_e[0] == -1 and s_e[1] == -1:
            print(count)
            exit()
        min_h = min(h[s_e[0]:s_e[1]])
        for i in range(s_e[0], s_e[1]):
            h[i] -= min_h
        count += min_h




if __name__ == '__main__':
    main()

Solution (Python):
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right 
from heapq import heapify, heappop, heappush
from math import *

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)

N,H = ilint()
H = [0]+H[:]
H = [-(H[i+1]-H[i]) for i in range(N)]+[INF]
ans = 0
for l in range(N):
    while H[l]<0:
        r = 0
        while r<N and H[r]<=0:
            r += 1
        tmp = min(-H[l],H[r])
        ans += tmp
        H[l] += tmp
        H[r] -= tmp
print(ans)

Solution (Python):
import math
N = int(input())
hs = list(map(int,input().split()))
c = 0
while True:
    pmi = 101
    start = 0
    flag = True
    for i in range(N):
        if hs[i] == 0:
            if pmi == 101:
                start += 1
                continue
            for j in range(start,i):
                hs[j] -= pmi
            
            c += pmi
            start = i+1
            pmi = 101
        else:
            flag = False
            pmi = min(pmi, hs[i])
    if flag:
        break
    else:
        if pmi != 101:
            for i in range(start,N):
                hs[i] -= pmi
            c += pmi
print(c)

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

count = 0

while h != [0]*(n+1):
    for i in range(len(h)):
        if h[i] != 0:
            break

    for j in range(i, len(h)+1):
        if h[j] == 0:
            break

    for x in range(i, j):
        h[x] -= 1

    count += 1

print(count)

Solution (Python):
n = int(input())
h = list(map(int, input().split()))
cnt=0
for i in range(n):
    while h[i] != 0:
        cnt+=1
        for j in range(i, n):
            if h[j] != 0:
                h[j] = h[j] - 1
            elif h[j] == 0:
                break
print(cnt)

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

h=list(map(int,input().split()))
sw=True
temp=0
ans=0
for i in range(N-1):
    if sw==True:
        if h[i]>h[i+1]:
            ans+=h[i]-temp
            sw=False
    else:
        if h[i]<h[i+1]:
            temp=h[i]
            sw=True
if sw==True:
    ans+=h[N-1]-temp

print(ans)

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

h=list(map(int,input().split()))
sw=True
temp=0
ans=0
for i in range(N-1):
    if sw==True:
        if h[i]>h[i+1]:
            ans+=h[i]-temp
            sw=False
    else:
        if h[i]<h[i+1]:
            temp=h[i]
            sw=True
if sw==True:
    ans+=h[N-1]-temp

print(ans)

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

Solution (Python):
n = int(input())
h = list(map(int,input().split()))
tmp = [0 for i in range(n)]
ans = 0
right = 0
left = n-1

while tmp != h:
    for i in range(n):
        if tmp[i] != h[i]:
            right = i
            left = i
            break
    for i in range(i+1,n):
        if tmp[i] == h[i]:
            left = i-1
            break
        elif i == n-1 and tmp[n-1] != h[n-1]:
            left = n-1
            break
    for i in range(right,left+1):
        tmp[i] += 1
    ans += 1



print(ans)

Solution (Python):
n = int(input())
h = list(map(int,input().split()))
tmp = [0 for i in range(n)]
ans = 0
right = 0
left = n-1

while tmp != h:
    for i in range(n):
        if tmp[i] != h[i]:
            right = i
            left = i
            break
    for i in range(i+1,n):
        if tmp[i] == h[i]:
            left = i-1
            break
        elif i == n-1 and tmp[n-1] != h[n-1]:
            left = n-1
            break
    for i in range(right,left+1):
        tmp[i] += 1
    ans += 1



print(ans)

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

ans = 0
def solve(li_h):
    l = 0
    for i in li_h:
        if(i==0):
            l += 1
        else:
            break
    r = l + 1
    for i in li_h[l+1:]:
        if(i==0):
            break
        r += 1
    
    for i in range(l, r):
        li_h[i] -= 1
    
    return li_h

while(sum(h)!=0):
    h = solve(h)
    ans += 1

print(ans)

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

num = 0
for i in range(n):
    
    if L[i] <= L[i+1]:
        num += L[i+1]-L[i]

print(num)

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

num = 0
min1 = 0
for i in range(n):
    
    if L[i] <= L[i+1]:
        num += L[i+1]-L[i]
    else:
        min1 = L[i+1]

print(num)

Solution (Python):
import math

n = int(input())
hL = list(map(int, input().split(" ")))

ans = 0
for i in range(1, 101):
    r = False
    for h in hL:
        if i <= h:
            if not r:
                ans += 1
            r = True
        else:
            r = False

print(ans)

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



ans=h[0]

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

Solution (Python):


import sys



def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    n = input_int()
    flowers = [0] + input_int_list()
    section_min = 0
    ans = 0
    for i in range(1, n + 1):
        if flowers[i] < flowers[i - 1]:
            ans += flowers[i - 1] - section_min
            section_min = flowers[i]
    if flowers[n] > section_min:
        ans += flowers[n] - section_min
    print(ans)
    return


if __name__ == "__main__":
    main()

Solution (Python):
def INT():
    return int(input())

def LI():
    return list(map(int, input().split()))

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

N = INT()
h = [0] + LI() + [0]
ans = 0

while not all([h[i] == 0 for i in range(len(h))]):
    min_idx = [idx for idx, flower in enumerate(h) if flower == 0]
    
    for i in range(len(min_idx) - 1):
        idx_1, idx_2 = min_idx[i], min_idx[i + 1]
        if idx_1 + 1 == idx_2:
            continue
        
        min_tall = min(h[idx_1 + 1 : idx_2])
        ans += min_tall
            
        for j in range(idx_1 + 1, idx_2):
            h[j] -= min_tall
        
print(ans)

Solution (Python):
def splitcount(l):
    if len(l) == 0:
        return 0
    minl = min(l)
    
    if minl==0:
        
        
        return splitcount(l[:l.index(0)]) + splitcount(l[l.index(0)+1:])
    else:
        return minl + splitcount(list(map(lambda x: x - minl, l)))

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


print(splitcount(h))

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

V = sum(H)
E = 0

for i in range(1,N):
  E += min(H[i-1],H[i])
  
print(V-E)

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

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

cnt = 0
for hh in range(1,max(h)+1):
  flg = False
  for i in range(n):
    if h[i] >= hh:
      flg = True
    else:
      if flg:
        flg = False
        cnt += 1
  if flg:
    flg = False
    cnt += 1      
print(cnt)

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

flag = False
ans = 0
while not flag:
  for i in range(n):
    if flag:
      if h[i] != 0: h[i] -= 1
      else: break
    elif not flag and h[i] != 0: 
      flag = True
      h[i] -= 1
      ans += 1
  if not flag: break
  flag = False
print(ans)

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

t = 0

def Water(n):
    global h
    while h[n] != 0:
        h[n] -= 1
        n += 1
        

for i in range(N):
    while h[i] != 0:
        Water(i)
        t += 1

print(t)

Solution (Python):

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

res = 0
for _ in range(100):
    if all(v == 0 for v in X):
        break

    flag = False
    for i in range(N):
        if X[i] > 0:
            X[i] -= 1
            flag = True
        elif flag:
            flag = False
            res += 1

    if flag:
        res += 1

print(res)

Solution (Python):
from math import floor, ceil, sqrt, factorial, log, gcd
from itertools import accumulate, permutations, combinations, product, combinations_with_replacement
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict
from heapq import heappop, heappush, heappushpop
import copy
import numpy as np
import sys
INF = float('inf')
mod = 10**9+7


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





def LI(): return list(map(int, sys.stdin.buffer.readline().split()))




def I(): return int(sys.stdin.buffer.readline())





def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()




def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')







def IR(n): return [I() for i in range(n)]







def LIR(n): return [LI() for i in range(n)]







def SR(n): return [S() for i in range(n)]







def LSR(n): return [LS() for i in range(n)]







def SRL(n): return [list(S()) for i in range(n)]


def main():
    n = I()
    h = list(LI())
    ans = h[0]
    for i in range(1, n):
        if h[i] > h[i-1]:
            ans += h[i] - h[i-1]
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
def divide_and_conuer(ls):
    mi=ls.index(min(ls))
    res=ls[mi]
    if(ls[mi+1:len(ls)]!=list()):
        res+=divide_and_conuer(ls[mi+1:len(ls)])-ls[mi]
    if(ls[0:mi]!=list()):
        res+=divide_and_conuer(ls[0:mi])-ls[mi]
    return res

def main():
    n = int(input())
    a = list(map(int, input().split()))
    print(divide_and_conuer(a))

if __name__ =="__main__":
    main()

Solution (Python):
def solve(L):
    if not L:
        return 0
    if 0 not in L:
        return min(L)+solve([l-min(L) for l in L])
    tmp=0
    l,p=0,False
    for i in range(len(L)):
        if L[i]==0:
            if p:
                p=False
                tmp+=solve(L[l:i])
                l=i+1
            else:
                l=i+1
        else:
            if not p:
                p=True
    return tmp+solve(L[l:])

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

print(solve(H))

Solution (Python):
import sys
import math
sys.setrecursionlimit(500000)
INF = float('inf')

def solve(h, water):
    left = 0
    while left < len(h) - 1 and h[left] == 0:
        left += 1
    right = left
    while right < len(h) and h[right] != 0:
        right += 1

    if left < right:
        h[left:right] = [i - 1 for i in h[left:right]]
    elif left == right and h[left] > 0:
        h[left] = h[left] - 1
    else:
        return water
        
    water = solve(h, water + 1)
    return water

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

if __name__ == '__main__':
    print(main())

Solution (Python):
n = int(input())
h = [0]+list(map(int,input().split()))
ans,temp = 0,-1
while True:
  for i in range(n+1):
    if temp==0 and h[i]!=0:
      ans += 1
    temp = h[i]
    h[i] = max(h[i]-1,0)
  if all(v==0 for v in h):
    break
print(ans)

Solution (Python):

def f(myList):
    if len(myList) == 0:
        return 0
    else:
        
        tmp = min(myList)
        ans = tmp
        for i in range(len(myList)):
            myList[i] -= tmp
        newList = []
        for i in range(len(myList)):
            pop_a = myList.pop()
            if pop_a != 0:
                newList.append(pop_a)
            else:
                ans += f(newList)
                newList = []
        ans += f(newList)
        return ans 

n = int(input())
a = list(map(int,input().split()))

print(f(a))

Solution (Python):
n=int(input())
h=list(map(int,input().split()))
res=0
while True:
  if max(h)==0:
    break

  else:
    i=0
    while i<n:
      if h[i]==0:
        i+=1

      else:
        res+=1
        while i<n and h[i]>0:
          h[i]-=1
          i+=1


print(res)

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