Problem:
 Score : 300 points    Problem Statement In a flower bed, there are N flowers, numbered 1,2,......,N . Initially, the heights of all flowers are 0 .
You are given a sequence h=\{h_1,h_2,h_3,......\} as input. You would like to change the height of Flower k to h_k for all k  (1 \leq  k \leq N) , by repeating the following "watering" operation:   Specify integers l and r . Increase the height of Flower x by 1 for all x such that l \leq x \leq r .   Find the minimum number of watering operations required to satisfy the condition.      Constraints  1 \leq N  \leq 100  0 \leq h_i \leq 100  All values in input are integers.         Input Input is given from Standard Input in the following format:  N  h_1  h_2  h_3  ......  h_N       Output Print the minimum number of watering operations required to satisfy the condition.        Sample Input 1 4
1 2 2 1      Sample Output 1 2  The minimum number of watering operations required is 2 .
One way to achieve it is:   Perform the operation with (l,r)=(1,3) .  Perform the operation with (l,r)=(2,4) .        Sample Input 2 5
3 1 2 3 1      Sample Output 2 5       Sample Input 3 8
4 23 75 0 23 96 50 100      Sample Output 3 221

Solution (Python):
N = int(input())
h = list(map(int,input().split()))
if max(h) == min(h):
    ans = h[0]

else:
    same = []
    for i in range(N-1):
        if h[i] == h[i+1]:
            same.append(i)
    
    if len(same) >= 1:
        for k in range (len(same))[::-1]:
            del h[same[k]]
    
    N = len(h)

    lmin = [] 
    lmax = [] 

    if N >=3:
        for i in range(1,N-1):
            if (h[i] > h[i-1]) and (h[i] > h[i+1]):
                lmax.append(h[i])
        
        for k in range(1,N-1):
            if (h[k] < h[k-1]) and (h[k] < h[k+1]):
                lmin.append(h[k])

        if h[0] > h[1]:
            lmax.append(h[0])
            
        if h[N-1] > h[N -2]:
            lmax.append(h[N-1])
                
        if (len(lmax) == 1) :
            ans = max(h)

        else:
            ans = sum(lmax) - sum(lmin)
    
    if N == 2:
        ans = max(h)

    if N == 1:
        ans = h[0]



print(ans)

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

Solution (Python):



































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


ans = 0
imosarray = [0 for _ in range(n + 1)]
for i in range(n):
    imosarray[i] = h2[i + 1] - h2[i]
    ans += max(0, h2[i + 1] - h2[i])

print(ans)

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

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

Solution (Python):
N = int(input())
h = list(map(int, input().split()))
ans = 0
x = [0]*N
for i in range(N):
    for _ in range(h[i]-x[i]):
        y = False
        for j in range(i,N):
            if x[j] < h[j]:
                x[j] += 1
                y = True
            else:
                break
        if y:
            ans += 1
print(ans)

Solution (Python):
import bisect
import operator
import os
from collections import Counter, deque
from fractions import gcd
from functools import lru_cache
from functools import reduce
import functools
import heapq
import itertools
import math
import numpy as np
import re
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")

N = int(sys.stdin.readline())
H = list(map(int, sys.stdin.readline().split()))

H = [0] + H + [0]
ans = 0
for h in reversed(range(0, max(H) + 1)):
    yes = False
    for i in range(len(H)):
        if H[i] >= h:
            yes = True
        else:
            if yes:
                ans += 1
            yes = False
print(ans)

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

def yama(l):
    r=0
    zf=0
    a=[0]*(len(l)+2)
    for i in range(1,len(l)+1):
        a[i]=l[i-1]
    for i in range(0,len(l)+1):
        if a[i]>0 and zf==1:
            r+=1
            zf=0
        elif a[i]<=0:
            zf=1
    return r
ans=0
while yama(l)>0:
    ans+=yama(l)
    for i in range(n):
        l[i]-=1
   
print(ans)

Solution (Python):
import sys
import copy
from collections import deque


input = sys.stdin.readline

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

def water(array):
    global count
    if array != []:
        minimum = min(copy.deepcopy(array))
        count += minimum
        for i in range(len(array)):
            array[i] -= minimum
    indx = [i for i, x in enumerate(array) if x == 0]
    if len(indx) == 0:
        return None
    elif len(indx) == 1:
        water(array[:indx[0]])
        water(array[indx[0]+1:])
    elif len(indx) > 1:
        water(array[:indx[0]])
        water(array[indx[-1]+1:])
        for i in range(len(indx)-1):
            water(array[indx[i]+1:indx[i+1]])

water(h)
print(count)

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

Solution (Python):

N = int(input())

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

ans = 0

for loop in range(100):
        
        start = 102

        for flo in range(N + 1):

            if (h[flo] > loop and start > 101):
                start = flo

            elif (h[flo] <= loop and start < flo):
                ans += 1
                start = 102

print (ans)

Solution (Python):
def resolve():
    N = int(input())
    Hs = list(map(int, input().split(" ")))
    cnt = 0
    while(any(Hs)):
        start = 0
        for i in range(N):
            
            if Hs[i] == 0:
                if i != start:
                    for j in range(start, i):
                        Hs[j] -= 1
                    cnt += 1
                start = i + 1 
            elif i == N-1:
                for j in range(start, N):
                    Hs[j] -= 1
                cnt += 1
        
        
        

    print(cnt)
    

if '__main__' == __name__:
    resolve()

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

t = 0
while max(h) > 0:
    si = N
    ei = N
    for i in range(N):
        if h[i] > 0:
            si = i
            break
    for i in range(si, N):
        if h[i] == 0:
            ei = i
            break
    for i in range(si, ei):
        h[i] -= 1
    t += 1
print(t)

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

for i in range(max(h)):
    zero=0
    for j in range(N+1):
        if h[j]==0:
            if h[j+1]==0:
                pass
            else:
                zero+=1
        else:
            h[j]-=1
    ans+=zero

print(ans)

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

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

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

ans = 0
active = 0

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

Solution (Python):


import sys


s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    N = i2n()
    H = i2nn()
    n = 0
    while True:
        l = -1
        r = -1
        for i, h in enumerate(H):
            if l == -1 and h > 0:
                l = i
            if l > -1 and r == -1 and h <= 0:
                r = i
        if l == -1:
            break
        if r == -1:
            r = N
        for i in range(l, r):
            H[i] -= 1
        n += 1
    print(n)

main()

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

def flower(lst=H):
    if not lst:
        return 0
    ans = min(lst)
    lst = list(map(lambda x:x-ans, lst))
    indexes = [-1]
    indexes.extend([i for i, j in enumerate(lst) if j == 0])
    m = len(indexes)
    indexes.append(len(lst))
    for i in range(m):
        ans += flower(lst[indexes[i]+1:indexes[i+1]])
    return ans

print(flower())

Solution (Python):

import sys
def input(): return sys.stdin.readline().rstrip()
def mi(): return map(int, input().split())


N = int(input())
Hs = list(mi())

ans = 0

while 1:
  
  nowinsection = 0 
  sectioncnt = 0 
  for i, h in enumerate(Hs):
    if h == 0:
      nowinsection = 0
    elif nowinsection == 0: 
      nowinsection = 1
      sectioncnt += 1
      Hs[i] = h - 1 
    else: 
      Hs[i] = h - 1 
  if sectioncnt == 0:
    print(ans)
    exit()
  else:
    ans += sectioncnt

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


def all_zero(h):
    zero = True
    for hh in h:
        if hh != 0:
            zero = False
            break
    return zero

def div_ary(h):
    arys = []
    zeropos = []

    for i,hh in enumerate(h):
        if hh == 0:
            zeropos.append(i)
    
    if len(zeropos) == 0:
        return [h]
    else:
        tmpary = []
        for i, hh in enumerate(h):
            if hh == 0:
                arys.append(tmpary)
                tmpary = []
            else:
                tmpary.append(hh)
        arys.append(tmpary)
    return arys


def mizuyari(h_ary):
    if len(h_ary) == 0:
        return 0
    else:
        
        h_min = min(h_ary)
        for i in range(len(h_ary)):
            h_ary[i] -= h_min
        num = h_min

        
        if all_zero(h_ary):
            return num
        else:
            
            arys = div_ary(h_ary)

            for a in arys:
                num += mizuyari(a)
    return num


h_ary = []
for th in target_h:
    h_ary.append(th)
mizunum = mizuyari(h_ary)
print(mizunum)

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

Hk = set([0])
for h in H:
  Hk.add(h)
Hk = sorted(Hk)

def po(h, s, e):
  posS = s
  cnt = 0
  for posE in range(s, e+1):
    if posE >= e or H[posE] <= Hk[h]:
      if posS < posE:
        cnt += po(h+1, posS, posE)
      posS = posE + 1
  if h > 0:
    cnt += Hk[h] - Hk[h-1]
  return cnt

print(po(0, 0, len(H)))

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

Hk = set([0])
for h in H:
  Hk.add(h)
Hk = sorted(Hk)

def po(h, s, e):
  if h >= len(Hk)-1:
    return Hk[h] - Hk[h-1]
  posS = s
  cnt = 0
  for posE in range(s, e+1):
    if posE >= e or H[posE] <= Hk[h]:
      if posS < posE:
        cnt += po(h+1, posS, posE)
      posS = posE + 1
  if h > 0:
    cnt += Hk[h] - Hk[h-1]
  return cnt

print(po(0, 0, len(H)))

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

res = 0
while True:
    if max(h) == 0:
        break

    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=[0]+list(map(int,input().split()))+[0]
l=[h[i+1]-h[i] for i in range(n+1)]
ans=0
for i in l:
    if i>0:
        ans+=i
print(ans)

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

ans = 0

while any((i > 0 for i in h)):
    counted = False
    for i in range(n):
        if h[i] > 0:
            h[i] -= 1
            if not counted:
                ans += 1
                counted = True
        else:
            counted = False

print(ans)

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

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

ans = 0
bottom = 0

for hh in h:
    if hh > bottom:
        ans += hh - bottom
    
    bottom = hh

print(ans)

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


def calc(l, r):
    mi = float('inf')
    i = -1
    if l + 1 == r:
        return H[l]
    if l == r:
        return 0
    for j in range(l, r):
        if mi > H[j]:
            mi = H[j]
            i = j

    for j in range(l, r):
        H[j] -= mi

    return calc(l, i) + calc(i + 1, r) + mi


print(calc(0, N))

Solution (Python):

time = 0
def count(s):
    if s== []:
        return
    global time 
    isend = True
    div = s.index(min(s))
    minuted = s[div]
    time += minuted
    for i in range(len(s)):
        s[i] -= minuted
        if s[i] != 0:
            isend = False
    else:
        nextlistf = []
        nextlistl = []
        for i in range(div):
            nextlistf.append(s[i])
        for i in range(1, len(s) - div):
            nextlistl.append(s[i + div])
        count(nextlistf)
        count(nextlistl)


n = int(input())
s = list(map(int, input().split()))
count(s)
print(time)

Solution (Python):
import sys
input = sys.stdin.readline
n = int(input())
h = list(map(int, input().split()))
if n == 1:
    print(h[0])
else:
    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 = [int(_) for _ in input().split()]

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

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

H = list(map(int,input().split(" ")))
H_orig = H[:]
heights = [0 for i in range(N)]



cnt = 0

while True:
    
    if min(H) > min(heights):
        for i in range(N):
            heights[i] += 1
            H[i] -= 1
        cnt += 1
    else:
        flag = False
        for i in range(N):
            
            if H[i] > 0:
                heights[i] += 1
                H[i] -= 1
                flag = True
            else:
                if flag:
                    cnt += 1
                    flag = False
                    
            if i == N-1 and flag:
                cnt += 1
        
    if H_orig == heights:
        break
    
print(cnt)

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

counter = 0

while h != [0]*N:
    for l in range(N):
        if h[l] != 0:
            break
    r = N-1
    for r in range(l+1,N):
        if h[r] == 0:
            r -= 1
            break
    for n in range(l,r+1):
        h[n] -= 1
    counter += 1
    continue

print(counter)

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

ans = 0
cou = 0

hList = [0] + hList

for i in range(N):
    if hList[i] < hList[i+1]:
        cou += (hList[i+1] - hList[i])
    else:
        ans += cou
        cou = 0
ans += cou
print(ans)

Solution (Python):
n = int(input())
hs = list(map(int, input().split()))
hs.insert(0, 0)
hs.append(0)
res = lh0 = lh1 = hs[0]
for h in hs:
    if lh0 <= lh1 > h:
        res += lh1
    elif lh0 > lh1 <= h:
        res -= lh1
    lh0, lh1 = lh1, h

print(res)

Solution (Python):
import copy
N = int(input())
h = list(map(int, input().split()))
l = []
l.append(h)
x = 0
while l != []:
	m = []
	for i in range(len(l)):
		a = [j for j, _ in enumerate(l[i]) if _ == min(l[i])]
		start = 0
		new = []
		for s in a:
			if l[i][start:s] != []:
				y = list(map(lambda _: _ - min(l[i]), l[i][start:s]))
				new.append(y)
			start = s + 1
		if l[i][start:] != []:
			z = list(map(lambda _: _ - min(l[i]), l[i][start:]))
			new.append(z)
		x += min(l[i])
		m = m + new
	l = copy.deepcopy(m)
print(x)

Solution (Python):
n=int(input())
h=list(map(int,input().split()))
result=0
H=max(h)
for j in range(0,H):
    tmp=0
    cur=0
    
    for i in range(1,n):
        if h[i]<=0 and h[i-1]>0:
            tmp+=1
            cur=1
            
    if h[n-1]>0 and cur!=0:
        tmp+=1
        
    for i in range(0,n):
        h[i]-=1
    
    result+=max(tmp,1)
print(result)

Solution (Python):



import sys

input = sys.stdin.readline

def main():
    N = int(input())
    H = list(map(int, input().split()))
    C = [0 for _ in range(N)]
    cost = 0
    for i in range(max(H)):
        is_open = True
        for j in range(N):
            if C[j] < H[j]:
                C[j] += 1
                if is_open:
                    cost += 1
                    is_open = False
            else:
                is_open = True
    print(cost)

main()

Solution (Python):
n = int(input())
h = list(map(int,input().split()))
su = sum(h)
count = 0
while sum(h) > 0:
  i = 0
  while i < n:
    if(h[i] == 0):
      i+=1
    else:
      count += 1
      while i < n and h[i] > 0:
        h[i] -= 1
        i += 1

print(count)

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

cnt = 0
while 1:
  if max(h)==0: break

  i = 0
  while i<N:
    if h[i]==0:
      i+=1
    else:
      cnt += 1
      while i<N and h[i]>0:
        h[i] -= 1
        i += 1
print(cnt)

Solution (Python):
from collections import deque

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

cost = 0

d = deque()
d.append((0,N,0))
while len(d):
    l,r,mm = d.pop()
    if l == r:
        continue
    m = min(H[l:r])
    cost += m - mm
    i = l
    while True:
        if H[i] == m:
            d.append((l,i,m))
            l = i + 1
        if i + 1 == r:
            d.append((l,r,m))
            break
        i += 1

print(cost)

Solution (Python):
def main():
    n = int(input())
    h = list(map(int, input().split()))
    h = [0] + h
    count = 0
    for i in range(len(h) - 1):
        tmp = h[i + 1] - h[i]
        count += tmp * int(tmp > 0)
    print(count)

if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
h=[int(i) for i in input().split()]+[0]
ans=0
wat=0
for m,n in zip(h,h[1:]):
    if wat<m:
        wat=m
    if wat>n:
        ans+=wat-n
        wat=n
print(ans)

Solution (Python):

def solve(H):
    total = 0
    while True:
        c = 0
        for i, h in enumerate(H):
            if c == 0:
                if h > 0:
                    H[i] -=1
                    c = 1
            elif 0 < h:
                H[i] -= 1
            elif h == 0:
                break
        if c == 0:
            return total
        total += 1
N = int(input())
H = [int(x) for x in input().split()]
print(solve(H))

Solution (Python):
import copy
N=int(input())
rH=list(map(int,input().split()))
ans=0
for i in range(max(rH)):
    cH=copy.deepcopy(rH)
    for k in range(N):
        if rH[k]!=0 and ((k!=0 and rH[k-1]==0)or(k==0)):
            cH[k]-=1
            ans+=1
        elif rH[k]!=0:
            cH[k]-=1
    rH=copy.deepcopy(cH)
print(ans)

Solution (Python):
N=int(input())
hlist=list(map(int,input().split()))
ans=min(hlist)
if len(hlist)==1:
    print(ans)
else:
    if max(hlist)==min(hlist):
        print(ans)
    else:
        for i in range(min(hlist)+1,max(hlist)+1):
            for j in range(len(hlist)-1):
                if hlist[j]>=i and hlist[j+1]<i:
                    ans+=1
            if hlist[len(hlist)-1]>=i:
                ans+=1
        print(ans)

Solution (Python):
N=int(input())
Hs=[0] + list(map(int,input().split()))
Hs.append(0)
ma = max(Hs)

res = 0
for _ in range(ma):
    cnt = 0
    last1 = -1
    for i in range(N+2):
        if Hs[i] > 0:
            Hs[i] -= 1
            if last1+1 != i:
                cnt += 1
            last1 = i
    
    res += cnt

print(res)

Solution (Python):
n = int(input())
h = list(map(int,input().split()))
 
mizuyari = 0
for i in range(sorted(h)[n-1]):
    maenohana = False
    for j in range(n):
        if not maenohana and i+1<=h[j]:
            mizuyari += 1
        maenohana = i+1<=h[j]
print(mizuyari)

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

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

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

def main():
  n=II()
  l=LI()

  zcnt=l.count(0)
  c=0
  i=0

  while True:
    if zcnt==n:
      break
    if i>=n:
      i=0
    if l[i]>0:
      c+=1
      while True:
        if i<n and l[i]>=1:
          l[i]-=1

          if l[i]==0:
            zcnt+=1

          i+=1
        else:
          break
    else:
      i+=1

  return c

print(main())

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

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

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

def main():
  n=II()
  l=LI()

  zcnt=l.count(0)
  c=0
  i=0

  while True:
    if zcnt==n:
      break
    if i>=n:
      i=0
    if l[i]>0:
      c+=1
      while True:
        if i<n and l[i]>=1:
          l[i]-=1

          if l[i]==0:
            zcnt+=1

          i+=1
        else:
          break
    else:
      i+=1

  return c

print(main())

Solution (Python):

import sys


def solve(N: int, h: "List[int]"):
    result = 0
    for i in range(N):
        if i==0:
            result += h[0]
        elif h[i] > h[i-1]:
            result += h[i] - h[i-1]
    print(result)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    h = [ int(next(tokens)) for _ in range(N) ]  
    solve(N, h)

if __name__ == '__main__':
    main()

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

Solution (Python):
from sys import setrecursionlimit
setrecursionlimit(10**8)


def count_water(h_array):
    
    water = 0
    if len(h_array) == 1:
        water = h_array[0]
        
        return water
    min_h = min(h_array)
    water = min_h
    new_h_array = []
    for h in h_array:
        if h != min_h:
            new_h_array.append(h - min_h)
        else:
            if len(new_h_array) != 0:
                water += count_water(new_h_array)
                new_h_array = []
    else:
        if len(new_h_array) != 0:
            water += count_water(new_h_array)        
    
    return water


N = int(input())
h_array = list(map(int, input().split()))
print(count_water(h_array))

Solution (Python):
N, *h = map(int, open(0).read().split())
ans = sum([max(h[i + 1] - h[i], 0) for i in range(N - 1)])
print(ans + h[0])

Solution (Python):

n = int(input())
lst = list(map(int, input().rstrip().split()))
ans = 0
cur = 0
for i in lst:
    ans += max(0, i - cur)
    cur = i
print(ans)

Solution (Python):

n = int(input())
lst = list(map(int, input().rstrip().split())) + [0]
ans = 0
cur = 0
for n, m in zip(lst, lst[1:]):
    if cur < n:
        cur = n
    if cur > m:
        ans += cur - m
        cur = m
print(ans)

Solution (Python):

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

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

ans = 0
for h_ref in range(1, 101):
    prev = 0
    for i, hi in enumerate(h):
        curr = int(hi >= h_ref)
        if curr > prev: ans += 1
        prev = curr

print(ans)

Solution (Python):
n = int(input())
H = [int(s) for s in input().split()]
H.append(0)
def sub(x,y):
    global H
    print(H,"H")
    
    for i in range(x,y):
        H[i] = H[i] -1
    print(H, "H[i]")
        
count = 0
for i in range(n):
    if H[i] > H[i+1]:
        count += H[i] - H[i+1]
print(count)
"""
def garden(a,b):
    global H
    global count
    if a==b :
        return count
    
    for i in range(a,b):
        if H[i] == 0:
            print("a=%d,b=%d,i=%d,1"%(a,b,i))
            return garden(a, i) + garden(i+1, b)
    
    if sum(H) == 0:
        return count
    sub(a,b)
    count += 1
    print(count,"count")
    garden(a,b)
print(garden(0,n))
"""

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

Solution (Python):
"""
最初の方針
　例えば最大の高さの花に着目し、隣の花2つのうち、高い方の高さにも着目する。
　まずその差を求め、その分だけ花の高さを伸ばす。
　→難しそう

2つめの方針
　もともと花の高さがあり、そこから削っていくような考え方はどうか。
　まずひとつずつ削っていく。
　リストを走査していくイメージで進め、フラグを用意しておく。
　1で削り続けている限りは1。
　そして、高さ0の部分に行き当たるか、一周したら1をansに加算する。
　0の場合は、そのまま走査していき、1に行き当たったらまた上と同様のことを行う。

"""
import copy

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

ans = 0
while sum(H) > 0:
    tmp = []
    flag = 0
    for h in H:
        if h > 0:
            tmp.append(h - 1)
            if flag == 0:
                ans += 1
                flag = 1
        else:
            tmp.append(0)
            flag = 0
    H = copy.deepcopy(tmp)
    
print(ans)

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

for i in range(N):
	if A[i] > A[i+1]:
		cnt += A[i]-A[i+1]

print(cnt)

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()))
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()))
h.append(0)
tmp = 0
water = 0
for i in h:
    if i < tmp:
        water += tmp - i
    tmp = i
print(water)

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

def split_list(_list):
    ret_list = []
    tmp_list = []
    for i in range(len(_list)):
        if _list[i] == 0:
            if len(tmp_list) != 0:
                ret_list.append(tmp_list)
                tmp_list = []

        else:
            tmp_list.append(_list[i])

    if len(tmp_list) != 0:
        ret_list.append(tmp_list)
    return ret_list

def give_min_water(_list):
    _min = min(_list)
    _list = [(num - _min) for num in _list]
    return _min, _list

ret = 0
new_list = split_list(h_list)

while len(new_list) != 0:
    new_list2 = []
    for _list in new_list:
        tmp_count, tmp_list = give_min_water(_list)
        
        
        ret += tmp_count
        tmp_list = split_list(tmp_list)
        if (len(tmp_list) != 0):
            for _list2 in tmp_list:
                new_list2.append(_list2)
    new_list = new_list2
print(ret)

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

def is_ok(state):
    flag = 0
    for i in range(N):
        if state[i] != h[i]:
            return False
    return True

i = 0
cnt = 0
state = [0 for _ in range(N)]
while not is_ok(state):
    if state[i] == h[i]:
        i += 1
        continue
    j = 0
    while i + j < N and state[i+j] != h[i+j]:
        j += 1
    for k in range(i, i+j):
        state[k] += 1
    cnt += 1
print(cnt)

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


if len(h) == 1:
    print(h[0])
    exit()
    
cnt = 0
cnt_line = 0
v = h[0]
for i in range(1, N):
    if h[i] < v:
        cnt += v - cnt_line
        cnt_line = h[i]
    v = h[i]
if h[-1] >= h[-2]:
    cnt += h[-1] - cnt_line
print(cnt)

Solution (Python):

import sys

def get_div_cnt(H):
    c = 1 if H[0] > 0 else 0

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

    return c

def solve(N: int, H: "List[int]"):
    R = 0
    while sum(H) > 0:
        R += get_div_cnt(H)
        for i in range(len(H)):
            if H[i] > 0:
                H[i] -= 1

    print(R)






def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    h = [ int(next(tokens)) for _ in range(N) ]  
    solve(N, h)

if __name__ == '__main__':
    main()

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

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

Solution (Python):
def water(H, start, stop):
    if max(H) == 0:
        return

    
    valley = min(H[start:stop])
    for i in range(start, stop):
        H[i] -= valley
    global ans
    ans += valley

    
    valley_indices = [start + i for i, h in enumerate(H[start:stop]) if h == 0]
    if valley_indices[0] != start:
        valley_indices = [start - 1] + valley_indices
    if valley_indices[-1] != stop - 1:
        valley_indices += [stop]
    for i in range(len(valley_indices) - 1):
        if valley_indices[i + 1] - valley_indices[i] > 1:
            water(H, valley_indices[i] + 1, valley_indices[i + 1])


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

ans = 0
water(H, 0, N)
print(ans)

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

cn = 0
for i in range(n):
    if cn == 0:
        cn += H[i]
    else:
        cn += H[i]-H[i-1] if H[i] > H[i-1] else 0
print(cn)

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):
def oneIsland(lis):
    island=0
    newlis=lis
    newlis.append(False)
    for n,i in enumerate(newlis):
        if n==len(newlis)-1:break
        if i and (not newlis[n+1]):
            island+=1
    return(island)
N=int(input())
h=[int(i) for i in input().split()]
toGrowBoolean=[]
for i in range(max(h)):
    toGrowBoolean.append([k>i for k in h])
print(sum([oneIsland(row) for row in toGrowBoolean]))

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

ans = 0
for i in range(0,101):
    count = 0
    for j in range(n):
        if h[j] - i <= 0 and h[j+1] - i > 0:
            count += 1
    ans += count

print(ans)

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

Solution (Python):
def water(a):
    if len(a)==0:return 0
    ma=min(a)
    x=ma
    l=[]
    for i in range(len(a)):
        y=a[i]-ma
        if y>0:l.append(y)
        else:
            x+=water(l)
            l=[]
    x+=water(l)
    return x

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

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

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

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

print(ans)

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

def main():

    h = H
    max_h = max(h)

    ans = 0
    curr_h = 0

    while curr_h <= max_h:
        prev_h = 0
        for n in h:
            if n > curr_h and prev_h <= curr_h:            
                ans += 1
            prev_h = n
        curr_h += 1

    print(ans)

if __name__ == '__main__':
    main()