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):













from sys import stdin
from operator import itemgetter
from collections import deque, Counter
import math
import pprint
from functools import reduce
import numpy as np
import random
import bisect
import copy



MOD = int(1e9+7)
INF = float('inf')
alpha = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

def keta(kazu): 
    kazu_str = str(kazu)
    kazu_list = [int(kazu_str[i]) for i in range(0, len(kazu_str))]
    return kazu_list

def gcd(*numbers): 
    return reduce(math.gcd, numbers)

def combination(m,n): 
    
    if n > m:
        return 'すまん'
    return math.factorial(m) // (math.factorial(m-n) * math.factorial(n))

def pow_k(x,n):  
    if n == 0:
        return 1
    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2
    return K * x

def fact(n): 
    arr = {}
    temp = n
    for i in range(2,int(n**0.5)+1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr[i] = cnt
    if temp != 1:
        arr[temp] = 1
    if arr == {}:
        arr[n] = 1
    return arr
    
def main():
    N = int(stdin.readline().rstrip())
    H = list(map(int,[int(x) for x in stdin.readline().rstrip().split()]))
    ans = 0
    now = 0
    ok = 0
    while H.count(0) != N:
        while True:
            if H[ok] == 0:
                ok += 1
                break
            else:
                now = ok
                while True:
                    if now >= N or H[now] == 0:
                        ans += 1
                        break
                    else:
                        H[now] -= 1
                        now += 1

    print(ans)
                
main()

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
ans = 0
while(any(a)):
    f = True
    x = a[0]
    for i,ai in enumerate(a):
        if ai:
            a[i] -= 1
        if ai < 1:
            if x != 0:
                break
        x = ai
    x = a[0]
    ans += 1
    
print(ans)

Solution (Python):
def solve():
  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]
  return ans
print(solve())

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

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


print(cnt)

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

H.append(0)
cnt = 0
for i in range(N + 1):
    cnt += max(H[i] - H[i - 1], 0)


print(cnt)

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

ans = 0
zero = 0

for h in H:
    if h == 0:
        zero += 1

while zero < N:
    flg = False
    cnt = 0

    for i in range(N):
        if H[i] > 0:
            flg = True
            H[i] -= 1

            if H[i] == 0:
                zero += 1
        else:
            if flg:
                cnt += 1
                flg = False

    if flg:
        cnt  += 1

    ans += cnt


print(ans)

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

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

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

ans=0
for i in range(k):
  j=0
  while j<n:
    
    if h[j]==0:
      j=j+1
    else:
      while j<n and h[j]!=0:
        h[j]=max(h[j]-1,0)
        j=j+1
      ans=ans+1
print(ans)

Solution (Python):
from collections import deque

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

cnt=0
l=deque([h])
bot=deque([0])
while len(l)>0:
    tmp=l.popleft()
    b=bot.popleft()
    cnt+=min(tmp)-b
    g=deque([n for n in range(len(tmp)) if tmp[n]==min(tmp)])
    g.appendleft(-1)
    g.append(N)
    
    for i in range(1,len(g)):
        if len(tmp[g[i-1]+1:g[i]])>0:
            l.append(tmp[g[i-1]+1:g[i]])
            bot.append(min(tmp))

print(cnt)

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

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

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

count = 0
while h.count(0) != N:
    beforeWater = False
    for i in range(N):
        if h[i] > 0:
            if not beforeWater:
                count += 1
            h[i] = h[i] - 1
            beforeWater = True
        else:
            beforeWater = False

print(count)

Solution (Python):
N = int(input())
h = list(map(int,input().split()))
cnt = 0
for i in range(max(h)):
    bol = 0
    for j in h:
        if j > i:
            bol = 1
        else:
            cnt += 1 if bol == 1 else 0
            bol = 0
    if bol == 0:
        cnt -= 1
print(max(h)+cnt)

Solution (Python):









import os
import sys


from itertools import groupby


def main():
    n = int(input())
    H = list(map(int, input().split()))
    A = []
    for i in range(max(H)+1):
        A.append([1 if h > i else 0 for h in H])

    cnt = 0
    for a in A:
        ans = groupby(a)
        for k, g in ans:
            if 1 in list(g):
                cnt += 1
    print(cnt)


if __name__ == "__main__":
    main()

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

ans = 0

for _ in range(max(h)):
    if h[0]:
        ans += 1
    for i in range(n - 1):
        if not h[i] and h[i + 1]:
            ans += 1
    for j in range(n):
        if h[j]: h[j] -= 1
print(ans)

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

ans = 0
now = 0

for h in H:
    if h <= now:
        now = h
    else:
        ans += h - now
        now = h

print(ans)

Solution (Python):











def solve(n,a):
  h_max = max(a)
  res = 0

  for h in range(h_max):
    flag = 0 

    for i in range(n):
      if a[i] > h and not flag:
        res += 1
        flag = 1
      elif a[i] <= h:
        flag = 0

  return res



def main():
  N = int(input())
  
  a = list(map(int,input().split()))
  print(solve(N,a))
  return

if __name__ == '__main__':
  main()

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()))
print(sum(max(0,h[x]-h[x+1]) for x in range(N-1)) + h[-1])

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

def mizuyari(l):
    if l == 0 or l == []:
        return 0
    nin = min(l)
    for i in range(len(l)):
        l[i] -= nin
    index = l.index(0)
    return nin + mizuyari(l[:index]) + mizuyari(l[index+1:])


print(mizuyari(h))

Solution (Python):
import sys
import math
def I():return int(sys.stdin.readline().replace("\n",""))
def I2():return map(int,sys.stdin.readline().replace("\n","").split())
def S():return str(sys.stdin.readline().replace("\n",""))
def L():return list(sys.stdin.readline().replace("\n",""))
def Intl():return [int(k) for k in sys.stdin.readline().replace("\n","").split()]
def Lx(k):return list(map(lambda x:int(x)*-k,sys.stdin.readline().replace("\n","").split()))

if __name__ == "__main__":
    n = I()
    a = Intl()
    cnt = 0
    def dfs(l):
        global cnt
        if l == [] or l == [0]:return
        elif 0 in l:
            i = l.index(0)
            dfs(l[:i])
            dfs(l[i+1:])
        else:
            l = [k-1 for k in l]
            cnt += 1
            dfs(l)
    dfs(a)
    print(cnt)

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

ans = 0
active = 0

for i in range(n):
    if h[i] >= active:
        ans += h[i] - active
        active = h[i]
    else:
        active = h[i]
print(ans)

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

    tmp = 0
    ans = 0
    goukei = sum(inlis)
    while goukei > 0:
        for i in range(n):
            if inlis[i] > 0:
                
                inlis[i] -= 1
                tmp += 1
                ans += 1
            else:
                if tmp > 0:
                    ans -= tmp - 1
                    tmp = 0
        if tmp > 0:
            ans -= tmp - 1
            tmp = 0
        goukei = sum(inlis)
        
        

    print(ans)

            



if __name__ == "__main__":
    main()

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

l = -1
r = -1
ans = 0
while sum(h) > 0:
    for i in range(n):
        if l == -1 and h[i] > 0:
            l = i
            
        if l != -1 and h[i] == 0:
            r = i-1
        elif  l != -1 and i == n-1:
            r = i
        
        if l != -1 and r != -1:
            ans += 1
            for j in range(l, r+1):
                h[j] -= 1
            else:
                l = -1
                r = -1

print(ans)

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

cnt=0
flg=1

while max(h)>0:
    
    for i in range(N+1):
        if h[i] >=1:
            flg=0
        elif flg==0 and h[i]==0:
            flg=1
            cnt+=1
    for j in range(N):
        h[j]-=1
        if h[j]<0:
            h[j]=0

print(cnt)

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

cnt = 0
stop = max(H)
while stop>0:
    stop += -1
    for i in range(N):
        if H[i]>0 and H[i+1]==0:
            cnt+=1
    for i in range(N):
        if H[i]>0:
            H[i]+=-1
print(cnt)

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

def mizuyari(h):
    if len(h)==0:
        return 0
    elif len(h)==1:
        return h[0]

    m = min(h)
    l = 0
    r = 0
    sm = 0

    while r < len(h):
        if h[r]==m:
            sm += mizuyari(list(map(lambda x: x-m, h[l:r])))
            l = r+1
        r += 1
    sm += mizuyari(list(map(lambda x: x-m, h[l:r])))

    return m + sm

print(mizuyari(h))

Solution (Python):
N = int(input())
H = list(map(int,input().split()))
print(sum(max(0,H[n]-H[n+1]) for n in range(N-1))+H[-1])

Solution (Python):
n = int(input())
h = list(map(int, input().split()))
def water(l, r, sub):
    if l == r:
        return 0
    i = l + h[l:r].index(min(h[l:r]))
    a = water(l, i, h[i]) 
    b = water(i + 1, r, h[i])
    return h[i] - sub + a + b
print(water(0, n, 0))

Solution (Python):
import numpy as np

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

def count(b):
    b = np.clip(b, 0, 1)
    return int(b[0] > 0) + np.sum(np.diff(b) > 0)

heights = np.array(H)

cnts = []
for water in range(100):
    arr = np.clip(heights - water, 0, 101)
    cnts.append(count(arr))

print(np.sum(cnts))

Solution (Python):





















































def calculation(lines):
    
    N = int(lines[0])
    values = list(map(int, lines[1].split()))
    
    
    

    result = get_cnt(values)

    return [result]


def get_cnt(values):
    
    
    ret = 0
    mi = min(values)
    
    ret += mi
    values = [value - mi for value in values]
    
    lst = None
    for value in values:
        
        if value != 0:
            if lst is None:
                lst = [value]
            else:
                lst.append(value)
        else:
            if lst is None:
                pass
            else:
                ret += get_cnt(lst)
                lst = None
    if lst is not None:
        ret += get_cnt(lst)
    return ret


def func_f(n):
    if n % 2 == 0:
        return int(n/2)
    else:
        return 3*n + 1



def get_input_lines(lines_count):
    lines = list()
    for _ in range(lines_count):
        lines.append(input())
    return lines



def get_testdata(pattern):
    if pattern == 1:
        lines_input = ['4', '1 2 2 1']
        lines_export = [2]
    if pattern == 2:
        lines_input = ['5', '3 1 2 3 1']
        lines_export = [5]
    if pattern == 3:
        lines_input = ['8', '4 23 75 0 23 96 50 100']
        lines_export = [221]
    return lines_input, lines_export



def get_mode():
    import sys
    args = sys.argv
    if len(args) == 1:
        mode = 0
    else:
        mode = int(args[1])
    return mode



def main():
    import time
    started = time.time()
    mode = get_mode()
    if mode == 0:
        lines_input = get_input_lines(2)
    else:
        lines_input, lines_export = get_testdata(mode)

    lines_result = calculation(lines_input)

    for line_result in lines_result:
        print(line_result)

    
    
    
    
    
    
    
    
    
    
    



if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
h = list(map(int,input().split()))
sw = 0
while max(h) != 0:
    for i in range(N):
        if h[i] == 0:
            continue
        j = 0
        for jj in range(i,N+1):
            if jj == N:
                j = jj
                break
            if h[jj] == 0:
                j = jj
                break
        
        sw += 1
        for jj in range(i,j):
            h[jj] -= 1
        break
print(sw)

Solution (Python):

import numpy as np
import sys
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect

sys.setrecursionlimit(10**6)


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


def z():
    return int(sys.stdin.readline())


def S():
    return sys.stdin.readline()


def C(line):
    return [sys.stdin.readline() for _ in range(line)]


N = z()
H = zz()
H = np.array(H)
ans = 0
while (sum(H) != 0):
    s = 0
    while (s < N):
        if(H[s] != 0):
            break
        s += 1
    e = s
    while (e < N):
        if (H[e] == 0):
            break
        e += 1
    
    H[s:e] -= 1
    ans += 1
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, *h = map(int, open(0).read().split())
ans = 0
b = ["1"] * n
while sum(h) > 0:
    for i in range(n):
        if h[i] > 0:
            h[i] -= 1
        else:
            b[i] = "0"
    ans += sum(i != "" for i in "".join(b).split("0"))
print(ans)

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

q = queue.Queue()
ans = 0
def dec(ary):
  global q, ans
  m = min(ary)
  ans += m
  tmp = []
  for i in range(len(ary)):
    t = ary[i] - m
    if t > 0:
      tmp.append(t)
    else:
      if len(tmp) > 0:
        q.put(tmp)
      tmp = []
  if len(tmp) > 0:
    q.put(tmp)
tmp = []
for i in range(n):
  if h[i] > 0:
    tmp.append(h[i])
  else:
    if len(tmp) > 0:
      
      q.put(tmp)
    tmp = []

if len(tmp) > 0:
  q.put(tmp)

while q.empty() == False:
  dec(q.get())
print(ans)

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

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

Solution (Python):
N = int(input())
H = list(map(int, input().split()))
is_init = True
is_end = False
ans = 0
while not is_end:
    is_init = True
    for i in range(N):
        h = H[i]
        if h <= 0:
            if i == N - 1 and is_init:
                is_end = True
            H[i] -= 1
        else:
            is_init = False
            if i - 1 >= 0:
                pre = H[i - 1]
                if pre <= -1:
                    ans += 1
            else:
                if h != 0:
                    ans += 1
            H[i] -= 1
print(ans)

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

for j, i in enumerate(h):
    if val0 > i:
        ans += val0-val1
        val1 = i
    elif j == len(h)-1:
        val0 = i
        ans += val0-val1
    val0 = i
print(ans)

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

print(ans)

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

ans=h[0]

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

print(ans)

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

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

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

hc = [0 for i in range(N)]
mx = max(hl)
count = 0
for k in range(mx):
    i = 0
    while i < N:
        if hl[i] > hc[i]:
            count += 1
            while i < N and hl[i] > hc[i]:
                hc[i] = hc[i] + 1
                i += 1
        else:
            i += 1
print(count)

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] > 0:
    ans += h[i] - h[i-1]
print(ans)

Solution (Python):

 

 
import sys
from fractions import gcd
import math
from math import ceil,floor
import collections
from collections import Counter
import itertools
import copy
 

 










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

ans = 0
while sum(H) > 0:
    tmp = 0
    for i in range(N):
        if H[i] > 0:
            H[i] -= 1
            tmp += 1
        elif H[i] == 0 and tmp > 0:
            ans += 1
            tmp = 0
        if i == N-1 and tmp > 0:
            ans += 1

print(ans)

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

def main():
    n = int(input())
    h = list(map(int, input().split()))
    ans = h[0]
    for i in range(1, n):
        if h[i] - h[i-1] > 0:
            ans += h[i] - h[i-1]
    print(ans)
        
if __name__ == '__main__':
    main()

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

def main():
    n = int(input())
    h = list(map(int, input().split()))
    arr = [h[0]] + [0] * (n - 1)
    ans = 0
    for i in range(1, n):
        arr[i] = h[i] - h[i-1]
    for i in range(n):
        if arr[i] > 0:
            ans += arr[i]
    print(ans)
        
if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N = int(input())
    *H, = map(int, input().split())

    water = 0
    ph = 0
    for h in H:
        if (d := h - ph) > 0:
            water += d
        ph = h
    print(water)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n, *h = map(int, read().split())
    if n == 1:
        print(h[0])
        sys.exit()
    r1 = h[0]
    for i1 in range(n - 1):
        if h[i1] < h[i1 + 1]:
            r1 += (h[i1 + 1] - h[i1])
    print(r1)
if __name__ == '__main__':
    main()

Solution (Python):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n, *h = map(int, read().split())
    if n == 1:
        print(h[0])
        sys.exit()
    r1 = h[0]
    r2 = h[-1]
    for i1 in range(n - 1):
        if h[i1] < h[i1 + 1]:
            r1 += (h[i1 + 1] - h[i1])
        elif h[i1] > h[i1 + 1]:
            r2 += (h[i1] - h[i1 + 1])
    r = max(r1, r2)
    print(r)
if __name__ == '__main__':
    main()

Solution (Python):
N, *H = map(int, open(0).read().split())

ans = 0

def split_list(l, s):
    ans = []
    while l.count(s):
        pos = l.index(s)
        ans.append(l[:pos])
        l = l[pos+1:]
    ans.append(l)
    return ans

def f(L):
    if len(L) == 0:
        return
    while L.count(0) == 0:
        global ans
        ans += 1
        L = [l - 1 for l in L]
    for m in split_list(L, 0):
        f(m)

f(H)
print(ans)

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
Hs = list(mapint())+[0]

ans = 0
while 1:
    cnt = 0
    start = False
    for i in range(N+1):
        if Hs[i]!=0:
            start = True
            Hs[i] -= 1
        else:
            if start:
                cnt += 1
            start = False
    ans += cnt
    if sum(Hs)==0:
        break
print(ans)

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

ans = 0
while max(H) > 0:
  grp = [[]]
  for i,h in enumerate(H):
    if h == 0:
      if len(grp[-1]) > 0: grp.append([])
    else: grp[-1].append((i,h))
  for g in grp:
    if len(g) == 0: continue
    Min = min([h for _,h in g])
    ans += Min
    for i,_ in g: H[i] -= Min
print(ans)

Solution (Python):
N = int(input())
H = list(map(int,input().split()))
tot = 0
while True:
    if H[0]>0:
        cnt = 1
    else:
        cnt = 0
    for i in range(1,N):
        if H[i]>0 and H[i-1]==0:
            cnt += 1
    tot += cnt
    for i in range(N):
        if H[i]>0:
            H[i] -= 1
    flag = 0
    for i in range(N):
        if H[i]>0:
            flag = 1
            break
    if flag==0:break
print(tot)

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

def rec(H):
    
    if not H:
        return 0
    ans = min(H)
    H = [h-ans for h in H]

    ranges = []
    preh = 0
    left = -1
    for i, h in enumerate(H):
        
        if preh == 0 and h > 0:
            left = i
        elif preh > 0 and h == 0:
            ranges.append((left, i))
            left = -1
        preh = h
    else:
        if left >= 0:
            ranges.append((left, len(H)))
        
    for l, r in ranges:
        ans += rec(H[l:r])
    return ans

print(rec(H))

Solution (Python):
def down(H, n):
  x = min(H)
  n += x
  for i in range(len(H)):
    H[i] -= x
  return H, n

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

while not all(h == 0 for h in H):
  L = []
  H0 =[]
  for h in H:
    if h != 0:
      L.append(h)
    else:
      if L:
        L, n = down(L, n)
      H0 += L
      H0.append(0)
      L = []
  if L:
    L, n = down(L, n)
    H0 += L
  H = H0

print(n)

Solution (Python):
import sys
N = int(input())
H = list(map(int, input().split()))
res = 0
while True:
    i = 0
    cnt = 0
    while i < N:
        while i < N and H[i] <= 0:
            i += 1
        if i >= N:
            break
        while i < N and H[i] > 0:
            H[i] -= 1
            i += 1
        cnt += 1
    if cnt == 0:
        print(res)
        sys.exit()
    else:
        res += cnt

Solution (Python):
N=int(input())
H=[0]+list(map(int,input().split()))
M=max(H)

K=0
for a in range(1,M+1):
    for b in range(N):
        if H[b]<a and H[b+1]>=a:
            K+=1
print(K)

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




ans = 0
for _ in range(max(H)):
    flg = False
    for i in range(n):
        if H[i] > 0:
            H[i] -= 1
            if not flg:
                ans += 1
                flg = True
        elif H[i] == 0:
            flg = False

print(ans)

Solution (Python):
import sys
from operator import itemgetter

sys.setrecursionlimit(10**9)
input = sys.stdin.readline

def ii(): return int(input())
def mi(): return map(int, input().split())
def lmi(): return list(map(int, input().split()))
def iif(n): return [int(input()) for _ in range(n)]
def lmif(n): return [list(map(int, input().split())) for _ in range(n)]
def ss(): return input().split()

def main():
    mod = 1000000007
    
    N = ii()
    H = lmi()
    F = [0] * N 

    cnt = 0
    l, r = 0, 0
    while True:
        while H[l] == F[l]:
            l += 1
            if l == N:
                break
        if l == N:
            break
        for i in range(l, N):
            if H[i] == F[i]:
                r = i
                break
            if i == N-1:
                r = N
        for i in range(l, r):
            F[i] += 1
        cnt += 1
    
    print(cnt)

    return

main()

Solution (Python):
_ = input()
h = list(map(int, input().split()))
ans = 0
while sum(h) > 0:
    left = 0
    while left < len(h) and h[left] == 0:
        left += 1
    h[left] -= 1
    right = left + 1
    while right < len(h) and h[right] != 0:
        h[right] -= 1
        right += 1
    ans += 1
print(ans)

Solution (Python):




n = int(input())
h = list(map(int, input().split()))
ans = 0
s = sum(h)
while s > 0:
    left = 0
    while left < n and h[left] == 0:
        left += 1
    h[left] -= 1
    s -= 1
    right = left + 1
    while right < n and h[right] != 0:
        h[right] -= 1
        s -= 1
        right += 1
    ans += 1
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

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

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

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

Solution (Python):
n=int(input())
L=list(map(int,input().split()))
ans=0
ans+=min(L)
for i in range(n):
    L[i]-=ans
L=[0]+L+[0]
while True:
    tmp=0
    for i in range(1,n+2):
        if L[i]==0 and len(L[tmp:i])!=0:
            if len(L[tmp:i])==1:
                Min=L[tmp+1]
            else:
                Min=min(L[tmp+1:i])
            ans+=Min
            for j in range(tmp+1,i):
                L[j]-=Min
            tmp=i
    if max(L)==0:
        break
print(ans)

Solution (Python):
n = int(input())
h = list(map(int, input().split()))
i = 0
ans = 0
c = 0
while True:
    if h[i] == 0:
        i += 1
        c += 1
        if c == n:
            break
        if i == n:
            i = 0
            c = 0
    else:
        cnt = 0
        ans += 1
        while h[i + cnt] != 0:
            h[i + cnt] -= 1
            cnt += 1
            if i + cnt == n:
                break
        i += cnt
        if i == n:
            i = 0
            c = 0
print(ans)

Solution (Python):
N = int(input())
h = [int(x) for x in input().split()]
count = 0
con = 0
while True:
    for i in range(N):
        if h[i] > 0:
            h[i] -= 1
            con = 1
        elif h[i] <= 0:
            if con == 1:
                count += 1
            con = 0
    if con == 1:
        count += 1
    con = 0
    if not any(h):
        break

print(count)

Solution (Python):

import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd





def solve():
    n = int(input())
    a = [int(x) for x in input().split()]
    m = max(a)
    ans = 0
    for _ in range(m):
        b = a[:]
        for i in range(n):
            if a[i]:
                if i == 0 or a[i-1] == 0:
                    ans += 1
                b[i] -= 1
        a = b[:]

    print(ans)
        

t = 1

for case in range(1,t+1):
    ans = solve()


"""



"""

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 = [int(x) for x in input().split()]
count = 0
con = 0
while True:
    for i in range(N):
        if h[i] > 0:
            h[i] -= 1
            con = 1
        elif h[i] <= 0:
            if con == 1:
                count += 1
            con = 0
    if con == 1:
        count += 1
    con = 0
    if not any(h):
        break

print(count)

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

newlist = [0 for i in range(N)]
cou = 0
while all([a == 0 for a in ns]) == False:
    y = 0
    for i,num in enumerate(ns):
        if num == 0 and y == 0:
            continue
        elif num != 0:
            ns[i] -= 1  
            y = 1
        
        if num == 0 and y == 1:
            break
        
    cou+=1

print(cou)

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

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

Solution (Python):
n = int(input())
h = [int(i) for i in input().split()]
sikiri = [-1, n]
ans = 0

for i in range(max(h)+1): 
    sik = [j for j, x in enumerate(h) if x == i]
    sikiri[len(sikiri):len(sikiri)] = sik
    sikiri.sort()
    
    for j in range(len(sikiri)-1):
        if sikiri[j+1] - sikiri[j] > 1:
            ans += 1
        else:
            pass
print(ans)

Solution (Python):
N = int(input())
H = list(map(int, input().split()))
Hs = [H]
T, c = 0, 1
while c<100000:
  List_1 = []
  for H in Hs:
    start = 0
    T += min(H)
    H = [h-min(H) for h in H]
    List_2 = [i for i, x in enumerate(H) if x == 0]
    for l in List_2:
      List_1.append(H[start:l])
      start = l+1
    List_1.append(H[start:])
  Hs.clear()
  for List in List_1:
    Hs.append(List)
  Hs = [H for H in Hs if H != []]
  c += 1
print(T)

Solution (Python):
n = int(input())
h = list(map(int, input().split()))
count = 0
l = 0
r = 0
while True:
    for i in range(n):
        if h[i] != 0:
            l = i
            break
        if i == n - 1:
            print(count)
            exit()

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

    if l == n - 1:
        r = l

    min_l_r = min(h[l:r+1])
    for i in range(l, r+1):
        h[i] -= min_l_r
    count += min_l_r

Solution (Python):
N = int(input())
H = [int(X) for X in input().split()]
Count = 0
while True:
    if len(H)==1:
        Count += H[0]
        break
    else:
        MAXI = H.index(max(H))
        if MAXI==0:
            LR = MAXI+1
        elif MAXI==len(H)-1:
            LR=MAXI-1
        else:
            LR = H.index(max(H[MAXI-1],H[MAXI+1]))
        Count += H[MAXI]-H[LR]
        del H[MAXI]
print(Count)