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):
import copy
N = int(input())
series = list(map(int,input().split()))
final = [0 for _ in range(len(series))]
count = 0
while series != final:
	start = 0
	end = 0
	for i in range(len(series)):
		if series[i] == 0:
			if start != i:
				end = i
				m = min(series[start:end])
				for j in range(start,end):
					series[j] -= m
				count += m
			else:
				start = i + 1
		elif i == len(series)-1:
			end = i
			m = min(series[start:i+1])
			for j in range(start,i+1):
				series[j] -= m
			count += m
print(count)

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


H.append(0)

res = 0
up = True
bottom = 0
for i in range(N):
    if (up):
    	if H[i] > H[i+1]:
            
            res += (H[i] - bottom)
            up = False
    else:
        if H[i] < H[i+1]:
            
            bottom = H[i]
            up = True
print(res)

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

counter = 0
maximum = max(h)

while maximum > 0:
    array = []
    for i in range(N):
        if h[i] == maximum:
            if i-1 not in array:
                counter += 1
            h[i] -= 1
            array.append(i)

    maximum -= 1

print(counter)

Solution (Python):
N = int(input())
h = list(map(int,input().split()))
h.append(0)
maxh = max(h)
cnt = [0 for i in range(maxh)]
for i in range(maxh,-1,-1):
    for j in range(len(h)-1):
        if h[j]>= i and h[j+1]<i:
            cnt[i-1] += 1

print(sum(cnt))

Solution (Python):
N = int(input())
h = list(map(int,input().split()))
sorth = sorted(h)[::-1]
count = 0
time = 0
while(h !=[0]*N):
    for i in range(N):
        if i == N-1 and h[i] > 0:
            count += 1
        elif h[i] > 0 and h[i+1] == 0 : count += 1
        h[i] -= 1 if h[i] > 0 else 0
print(count)

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

ans = 0
i = 0
while i < N:
    while h[i] == 0:
        i += 1
        if i == N: break
    j = i
    if i == N: break
    while h[j] > 0:
        h[j] -= 1
        j += 1
        if j == N: break
    ans += 1

print(ans)

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

    cnt = 0
    hss = [hs[:]]
    while True:
        tmp_sum = 0
        for tmp_hs in hss:
            tmp_sum += sum(tmp_hs)
        if tmp_sum == 0:
            break

        for hs_idx, tmp in enumerate(hss):
            if 0 in tmp:
                idx_0 = tmp.index(0)
                del hss[hs_idx]
                hss.append(tmp[:idx_0])
                hss.append(tmp[idx_0+1:])

        for tmp in hss:
            if not tmp:
                continue
            min_tmp = min(tmp)
            cnt += min_tmp

            for i in range(len(tmp)):
                tmp[i] -= min_tmp


        
    print(cnt)

main()

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

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

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

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

if set(h) == {0}:
    print(ans)

else:
    while True:
        rightflag = False

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

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

        if not rightflag:
            right = N-1

        

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

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

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

    print(ans)

Solution (Python):
N = int(input())
h = 0
h_before = 0
count = 0
MaxLevel = 0
for i in input().split():
	h_before = h
	h = int(i)
	diff = h - h_before
	if diff >= 0:
		count += diff
		MaxLevel = h

print(count)

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

m = h[0]

for i in range(1,n):
  if h[i] > h[i-1]:
    m += h[i] - h[i-1]

print(m)

Solution (Python):
def main():
    N = int(input())
    h = list(map(int, input().split()))
    now_h = []
    for _ in range(N):
        now_h.append(0)
    i = 0
    count = 0
    while True:
        if now_h[i] < h[i]:
            
            end = i
            for j in range(i + 1, N):
                if now_h[j] < h[j]:
                    end = j
                else:
                    break
            for k in range(i, end + 1):
                now_h[k] += 1
            count += 1
        else:
            if i == N - 1:
                break
            i += 1
            continue
    print(count)


if __name__ == "__main__":
    main()

Solution (Python):
def c_grand_garden(N, H):
    ans = min(H)
    for k in range(N):
        H[k] -= ans
    H.append(0)
    for k in range(N):
        if H[k] > H[k + 1]:
            ans += H[k] - H[k + 1]
    return ans

N = int(input())
H = [int(i) for i in input().split()]
print(c_grand_garden(N, H))

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

cnt = 0

for j in range(N):
  while h[j] > 0:
    for i in range(j, N):
      if h[i] == 0:
        break
      else:
        h[i] -= 1
    cnt += 1

print(cnt)

Solution (Python):
def water(a, l, r):
    for i in range(l, r+1):
        a[i] -= 1


N = int(input())
a = list(map(int, input().split()))
cnt = 0
while True:
    l = N
    r = N-1
    for i in range(N):
        if a[i] != 0:
            l = i
            break

    if l == N:
        break
    else:
        for i in range(l, N):
            if a[i] == 0:
                r = i-1
                break
        
    water(a, l, r)
    cnt += 1

print(cnt)

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

def f(L):
    if len(L) == 0:
        return 0

    if len(L) <= 2:
        return max(L)

    l = min(L)
    i = np.where(L == l)[0][0]
    return f(L[:i] - l) + f(L[i + 1:] - l) + l

print(f(H))

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

cnt = 0


hmax = max(h)
while hmax > 0:
    for i in range(N):
        if h[i] == hmax:
            h[i] = h[i] - 1
            cnt = cnt + 1
            if i != N-1:
                if h[i+1] == hmax:
                    cnt = cnt - 1
    hmax = max(h)

print(cnt)

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

Solution (Python):

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



def step(h):
    if sum(h) == 0:
        return 0

    if 0 in h:
        split = h.index(0)
        return step(h[:split]) + step(h[split + 1:])

    else:
        new_h = [hk - 1 for hk in h]
        return 1 + step(new_h)



print(step(h))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
p=a[:]
d=max(p)
c=0
for i in range(d):
    k=0
    for i in range(n):
        if a[i]>0:
            if k==0:
                c+=1
                k=1
            a[i]-=1
        else:
            k=0
print(c)

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

Solution (Python):

import sys

import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

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

count = 0
while(1):
    if sum(a) == 0:
        break
    b = False
    for i in range(n):
        if a[i] == 0:
            if b == True:
                break
            continue
        else:
            a[i] -= 1
            b = True
    count += 1
print(count)

Solution (Python):
N = int(input())
hs = map(int, input().split())
b = 0
m = 0
for h in hs:
  if b > h:
    m += b - h
  b = h
m += b
print(m)

Solution (Python):
import math
import copy




def nCast(number):
    if type(number)==str:
        return int(number)
    for idx in range(0,len(number)):
        if type(number[idx])==str:

            number[idx]=int(number[idx])
        else:
            nCast(number[idx])
    return number
    
def inputArr(w):
    l=list()
    for idx in range(0,w):
        l.append(input())
    return l
def inputArr1(w):
    l=list()
    for idx in range(0,w):
        l.append(input())
    return l

a=nCast(input())
b=nCast(input().split())


def help(max):
    count=0
    tmp=0
    for idx in range(0,len(b)):
        if b[idx]==max:
            b[idx]=b[idx]-1
            if tmp==0:
                tmp=1
        else:
            if tmp==1:
                tmp=0
                count+=1

    return tmp+count

def func():
    result=0
    max=100
    while max!=0:
        result+=help(max)
        max-=1
    return result
print(func())

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

def uniq(h):
	res = []
	pre = -1
	for x in h:
		if pre != x:
			res.append(x)
		pre = x
	return res

ans = 0
while(True):
	h = uniq(h)
	if len(h)== 1:
		ans += h[0]
		break
	m = max(h)
	ans+=h.count(m)
	h = [x if x != m else m-1 for x in h]
print(ans)

Solution (Python):
import numpy as np
import sys
input = sys.stdin.readline
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N = I()
l = IL()

dp = [0]*N
dp[0] = l[0]

for i in range(1,N):
    if l[i-1] >= l[i]:
        dp[i] = dp[i-1]
    else:
        dp[i] = dp[i-1]+l[i]-l[i-1]

print(dp[-1])

Solution (Python):
import collections
import math
import re
import numpy
def ai():return input()
def a():return int(input())
def ma():return map(int, input().split())
def ms():return map(str, input().split())
def lma():return list(map(int, input().split()))
def lms():return list(map(str, input().split()))
def say(i):return print("Yes" if i == 0 else "No")
def Say(i):return print("YES" if i == 0 else "NO")
k= [0]
l = []
f = 0
s=0
c=0
e =1
a  =a()
x = lma() +[0]
m = min(x)
for i in range(1,a+1):
    if x[i] < x[i-1]:
        s+=x[i-1]-m
        m = x[i]
print(s)

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

s = [0]*(N+1)
for i in range(N):
    s[i]+=h[i]
    s[i+1]-=h[i]
ans = 0
for si in s:
    if si>0: ans += si
print(ans)

Solution (Python):
if __name__ == '__main__':
    n = int(input())
    hs = list(map(int, input().split()))
    height = hs[0]
    count=0
    for h in hs[1:]:
        if height>h:
            count+=height-h
            height=h
        else:
            height=h
    print(int(count+height))

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

ans = 0
c = 0
p = 0
s = 0
while any(h != 0 for h in H):
    for i, h in enumerate(H):
        if h > p:
            c = 1
            p = h
            s = i
        elif h < p:
            break
        else:
            c += 1
    ans += 1
    for i in range(c):
        H[s + i] -= 1
    p = 0
print(ans)

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

def feedcount(heights):
    counter = 0
    hiszero = True
    for h in heights:
        if h != 0 and hiszero:
            hiszero = False
            counter += 1
        elif h == 0:
            hiszero = True
    return counter

def feedwater(heights):
    return list(map(lambda h: max(0,h-1), heights))

total = 0
while(feedcount(heights) != 0):
    total += feedcount(heights)
    heights = feedwater(heights)
print(total)

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

mizu = 0

for i in range(len(h_list)-1):
    if h_list[i] > h_list[i+1]:
        mizu += h_list[i] - h_list[i+1]
        
mizu += h_list[-1]

print(mizu)

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

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
a=0
for i in range(len(l)):
    if a<=l[i]:
        a,l[i]=l[i],l[i]-a
    else:
        a=l[i]
        l[i]=0
ans=0
for i in l:
    ans+=i
print(ans)

Solution (Python):


import sys


INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


n = II()
H = LI() + [0]

ans = 0
state = 1
for i in range(max(H)):
    for h in H:
        if state:
            if h >= i + 1:
                state = 0
        else:
            if h < i + 1:
                ans += 1
                state = 1


print(ans)

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

while any(h):
  l,r=0,0
  while l<n and not h[l]:
    l+=1
  r=l
  while r<n and h[r]:
    r+=1
  v=min(h[l:r])
  for i in range(l,r):
    h[i]-=v
  ans+=v

print(ans)

Solution (Python):
import numpy as np

def counter(array):
    if array.size == 0:
        return 0
    if array.size == 1:
        return array[0]
    min_num = np.min(array)
    idx = np.argmin(array)
    tmp = array - min_num
    return min_num + counter(tmp[:idx]) + counter(tmp[idx+1:])


n = int(input())
h = np.array(list(map(int, input().split())))
print(counter(h))

Solution (Python):

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

res = 0

for i in range(n):
    if hl[i]==0:
        continue

    while hl[i]>0:
        res += 1
        for j in range(i,n):
            if hl[j]==0:
                break
            hl[j] -= 1

print(res)

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

ans = 0
que = [H]

while que:
    h = que.pop()
    h_min = min(h)
    ans += h_min
    h = [hi - h_min for hi in h]

    tmp = []
    for hi in h:
        if hi == 0:
            if tmp:
                que.append(tmp)
                tmp = []
        else:
            tmp.append(hi)
    else:
        if tmp:
            que.append(tmp)

print(ans)

Solution (Python):

"""
Created on Sun Jan 20 20:51:36 2019

@author: Yamazaki Kenichi
"""

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

ans = h[0]

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

print(ans)

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

if n >= 2:
    answer = 0
    while max(h) > 0:
        c = 1 if h[0] > 0 else 0
        for i in range(n - 1):
            if h[i] == 0 and h[i + 1] > 0:
                c += 1
        answer += c
        h = [max([i - 1, 0]) for i in h]
    print(answer)
else:
    print(h[0])

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

for i in range(1,max_height+1):
    switch = 0
    for j in range(n):
        if switch == 0:
            if h[j] >= i:
                switch = 1
                ans += 1
        if switch == 1:
            if h[j] < i:
                switch = 0

print(ans)

Solution (Python):
def A():
    ab, bc, ca = map(int, input().split())
    print(int(bc * ab / 2))


def B():
    s = int(input())
    f_data = [s]
    i = 1
    a = [s]
    tmp = [0 for i in range(1000000)]
    def f(n):
        if n % 2 == 0:
            return n // 2
        else:
            return 3 * n + 1

    while True:
        key = f(f_data[-1])
        f_data.append(key)
        if tmp[key] == 1:
            ans = i + 1
            break
        tmp[key] = 1
        i += 1
    print(ans)


def C():
    N = int(input())
    cnt = 0
    h = list(map(int, input().split()))
    while sum(h) != 0:
        cnt += 1
        for i in range(N):
            if h[i] > 0:
                
                for j in range(i, N):
                    
                    if h[j] == 0:
                        break
                    else:
                        h[j] -= 1
                
                break
    print(cnt)
C()

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

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

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

count = 0
tank = 0
while True:
    for i in range(len(h)):
        if h[i] == 0 and tank != 0:
            count += 1
            tank = 0

        elif h[i] != 0:
            h[i] -= 1
            tank = 1
    if sum(h) == 0:
        break

print(count)

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

cnt = 0
while True:
  f = 0
  if list(set(h)) == [0]:
    break
  for i in range(N):
    if h[i] == 0:
      if f == 1:
        f = 0
        cnt += 1
    else:
      h[i] -= 1
      f = 1
  if f == 1:
    cnt += 1
  
    
print(cnt)

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

ans=0

while h!=[]:
	if h[0]==0:
		h.pop(0)
		continue
	i=0
	while True:
		if i<len(h) and h[i]!=0:
			h[i]-=1
			i+=1
		else:
			i=0
			ans+=1
			break

print(ans)

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

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

c = 0
while 1:
    for i in range(N):
        if h[i] != 0:
            break
    else:
        break
    con0 = 1
    for i in range(N):
        if con0 == 1 and h[i] != 0:
            c += 1
            con0 = 0
        elif con0 == 0 and h[i] == 0:
            con0 = 1
        if h[i] != 0:
            h[i] -= 1

print(c)

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

print(sum(A) + H[-1])

Solution (Python):
import numpy as np

N = int(input())
h_list = list(map(int, input().split()))
ar = np.zeros(N+2)
ar[1:N+1] = np.array(h_list)


cnt = 0
search_flag = False
ss = 0
while True:
    for i in range(N + 2):
        if search_flag:
            if ar[i] == 0:
                search_flag = False
                end_i = i
                cnt += 1
                ar[start_i:end_i] -= 1
        if not search_flag:
            if ar[i] > 0:
                search_flag = True
                start_i = i
    if sum(ar) == 0:
        break
    ss += 1
print(cnt)

Solution (Python):
n=int(input())
ans=0
hlist=[[int(x) for x in input().split()]]
for h in hlist:
    count=min(h)
    ans+=count
    tmp=[]
    for i in h:
        i=i-count
        if i == 0:
            if tmp==[]:
                continue
            hlist.append(tmp)
            tmp=[]
            continue
        tmp.append(i)
    if len(tmp)>0:
        hlist.append(tmp)
print(ans)

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

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

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

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

ans=0
c=0
for i in range(N):
    if A[i]>=c:
        ans+=A[i]-c
        c=A[i]
    elif A[i]<c:
        c=A[i]
print(ans)

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

count = 0
for i in range(1, 101):
  flag = False
  for j in range(n):
    if h[j] >= i:
      if not flag:
        flag = True
        count += 1
    else:
      if flag:
        flag = False
print(count)

Solution (Python):
import numpy as np


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

R = 0

for i in range(1, max(H)+1):
    FL = ''.join(['1' if h else '0' for h in (H >= i).tolist()]).split('0')
    while '' in FL:
        FL.remove('')
    R += len(FL)

print(R)

Solution (Python):
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(input())
def S(): return input()
n=I()
h=IL()
c=0
while True:
    for i in range(n):
        if h[i]!=0:
            s=i
            break
    else:
        print(c)
        break
    
    for i in range(i,n):
        if h[i]==0:
            e=i
            break
    else:
        e=n
    
    for i in range(s,e):
        h[i]-=1
    c+=1

Solution (Python):
import sys
import math
import collections
import itertools
import array
import inspect


sys.setrecursionlimit(1000000)



def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))



def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in input().split()]


def gcd(n, m):
    if n % m == 0:
        return m
    else:
        return gcd(m, n % m)


def gcd_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = gcd(v, L[i])

    return v


def lcm(n, m):
    return (n * m) // gcd(n, m)


def lcm_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = lcm(v, L[i])

    return v



def wfs_d(D, N, K):
    """
    D: 隣接行列(距離付き)
    N: ノード数
    K: 始点ノード
    """

    dfk = [-1] * (N + 1)
    dfk[K] = 0

    cps = [(K, 0)]
    r = [False] * (N + 1)
    r[K] = True
    while len(cps) != 0:
        n_cps = []
        for cp, cd in cps:
            for i, dfcp in enumerate(D[cp]):
                if dfcp != -1 and not r[i]:
                    dfk[i] = cd + dfcp
                    n_cps.append((i, cd + dfcp))
                    r[i] = True

        cps = n_cps[:]

    return dfk



def dfs_d(v, pre, dist):
    """
    v:  現在のノード
    pre: １つ前のノード
    dist: 現在の距離

    以下は別途用意する
    D: 隣接リスト(行列ではない)
    D_dfs_d: dfs_d関数で用いる，始点ノードから見た距離リスト
    """

    global D
    global D_dfs_d

    D_dfs_d[v] = dist

    for next_v, d in D[v]:
        if next_v != pre:
            dfs_d(next_v, v, dist + d)

    return


def sigma(N):
    ans = 0
    for i in range(1, N + 1):
        ans += i
    return ans


def comb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2, r + 1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p - 1, r, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def bisearch(L, target):
    low = 0
    high = len(L) - 1
    
    while low <= high:
        mid = (low + high) // 2
        guess = L[mid]
        if guess == target:
            return True
        elif guess < target:
            low = mid + 1
        elif guess > target:
            high = mid - 1
    if guess != target:
        return False



dp = None

def calcdiv(H):
    i = 0
    d = 0

    while i < len(H):
        if H[i] == 0:
            i += 1
            continue

        while i < len(H) and H[i] != 0:
            i += 1
        
        d += 1
    
    return d


def is_end(H):
    for h in H:
        if h >= 1:
            return False
    
    return True


def main():
    N = int(input())
    H = li_input()

    ans = 0

    while not is_end(H):
        d = calcdiv(H)
        ans += d

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


    
    print(ans)


main()

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(n):
	tmp = a[i]
	ans += tmp
	for j in range(i, n):
		tmp = min(tmp, a[j])
		a[j] -= tmp
print(ans)

Solution (Python):
N = int(input())
h = list(map(int, input().split(" ")))
max_ans = sum(h)
b = [min(h[i+1], h[i]) for i in range(N-1)]
decrease = sum(b)

print(max_ans-decrease)

Solution (Python):
N=int(input())
ans=0
tmp=0
for x in list(map(int,input().split())):
    ans+=max(0,x-tmp)
    tmp=x
print(ans)

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

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

ans = 0
h_min = min(h_list)

ans += h_min

pre = h_min
for i in range(N):
    h = h_list[i]
    if i == 0:
        ans += h - h_min
        pre = h
    else:
        if h >= pre:
            ans += h - pre
            pre = h
        else:
            pre = h
print(ans)

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

def count(h,m):
    c = 0
    state = 0
    for i in range(n):
        if h[i] >= m and state == 0:
            c += 1
            state = 1
        elif h[i] < m and state == 1:
            state = 0
    return c

ans = 0
m = max(h)
while m > 0:
    ans += count(h,m)
    m -= 1
    
print(ans)

Solution (Python):
N = int(input())
H = list(map(int, input().split()))
maxh = max(H)
ans = 0
for h in range(1, maxh+1):
  above = False
  for k in range(N):
    if H[k] >= h:
      if not above:
        ans += 1
    above = H[k] >= h
print(ans)

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

ans = 0
while any(H):
    start = False
    for i in range(N):
        if not start and not H[i]: continue
        if H[i]:
            start = True
            H[i] -= 1
        else:
            break
    ans += 1
print(ans)

Solution (Python):
def inpl(): return [int(i) for i in input().split()]

N = int(input())
H = inpl()
ans = 0
while True:
    ctr = 1
    for i in range(N):
        if H[i]:
            ans += ctr
            ctr = 0
            H[i] -= 1
        else:
            ctr = 1
    if not any(H):
        break
print(ans)

Solution (Python):

def LI(): return list(map(int, input().split()))
def II(): return int(input())
def LS(): return input().split()
def S(): return input()
def LIR(n): return [LI() for i in range(n)]
mod = 1000000007


"""
a,b,c = LI()
s = a+b+c
s /= 2
ans = (s*(s-a)*(s-b)*(s-c))**(1/2)
print(int(ans))
"""


"""
s = int(input())
m = 1
lis = [True for i in range(1000001)]

while lis[s]:
    lis[s] = False
    if s % 2 == 1:
        s = 3*s+1
    else:
        s //= 2
    m += 1
print(m)
"""


n = II()
h = LI()
ans = 0
while sum(h) != 0:
    l = 0
    while h[l] == 0: l += 1
    r = l
    while r < n:
        if h[r] == 0: break
        h[r] -= 1
        r += 1
    ans += 1
print(ans)

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

result = 0
for i in range(1, 101):
    for j in range(N):
        if H[j] >= i:
            if j == 0 or H[j - 1] < i:
                result += 1

print(result)

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

ANS=0
while max(A)>0:
    for i in range(1,N+2):
        if A[i-1]<=0 and A[i]>0:
            ANS+=1

    A=[a-1 for a in A]

print(ANS)

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

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

print(a)

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

cnt = 0
while h != [0] * n:
    prev = -1
    for i in range(n):
        if prev == -1 and h[i] != 0:
            cnt += 1
        if h[i] == 0:
            prev = -1
        else:
            prev = 1
            h[i] -= 1

    

print(cnt)

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

Solution (Python):
N = int(input())
*H, = map(int, input().split())
ans = 0
A = [0]*N
update = 1
h = 0
while update:
    update = 0
    cnt = 0
    for i in range(N):
        if H[i] > h:
            cnt += 1
        else:
            if cnt > 0:
                update = 1
                ans += 1
            cnt = 0
    if cnt > 0:
        update = 1
        ans += 1
    h += 1
print(ans)

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

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

ans = 0
pre = 0
for i in h:
    if pre < i:
        ans += i-pre

    pre = i

print(ans)

Solution (Python):
def inpl(): return list(map(int, input().split()))


N = int(input())
A = inpl()

ans = 0
while sum(A) > 0:
    ans += 1
    flag = False
    for i in range(N):
        if A[i] > 0:
            A[i] -= 1
            flag = True
        elif flag is True:
            break
print(ans)