CodeEff/ECCO huggingface dataset:
# Original solution
def dfs(_h):
    if len(_h) == 0:
        return
    min_h = min(_h)
    global ans
    ans += min_h
    tmp = []
    for i in range(len(_h)):
        if _h[i] == min_h:
            tmp.append(i)
    tmp.append(N)
    head = 0
    sp = []
    for i in tmp:
        sp.append([x - min_h for x in _h[head:i]])
        head = i+1
    for s in sp:
        dfs(s)

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

# Optimized solution
N = int(eval(input()))
h = [int(x) for x in input().split()]

node = sum(h)
path = 0
prev = h[0]
for cur in h[1:]:
    path += min(prev, cur)
    prev = cur
print((node - path))
# Original solution
N = int(eval(input()))
height = [0] + [int(i) for i in input().split()]
count = 0
for i in range(N):
  if height[i+1] > height[i]:
    count += height[i+1] - height[i]
print(count)

# Optimized solution
#coding:utf-8
n = int(eval(input()))
h = list(map(int, input().split()))
cnt = h[0]
for i in range(n-1):
    if h[i+1] > h[i]:
        cnt += h[i+1] - h[i]
print(cnt)
# Original solution
import sys
input = sys.stdin.readline

n = int(eval(input()))
h = [int(x) for x in input().split()]

ans = 0

def solve(l, r):
    global ans
    if l >= r:
        return
    if 0 not in h[l:r]:
        minh = min(h[l:r])
        ans += minh
        for i in range(l, r):
            h[i] -= minh
        solve(l, r)
    else:
        for i in range(l, r):
            if h[i] == 0:
                solve(l, i)
                solve(i + 1, r)
solve(0, n)

print(ans)

# Optimized solution
import sys
input = sys.stdin.readline

n = int(eval(input()))
h = [int(x) for x in input().split()]

ans = 0

def solve(l, r):
    global ans
    if l >= r:
        return
    if 0 not in h[l:r]:
        minh = min(h[l:r])
        ans += minh
        for i in range(l, r):
            h[i] -= minh
        solve(l, r)
    else:
        for i in range(l, r):
            if h[i] == 0:
                solve(l, i)
                l = i + 1
        solve(l, r)
solve(0, n)

print(ans)
# Original solution
N = int(eval(input()))
H = [0] + list(map(int, input().split()))

count = 0
for h in range(max(H)):
	for n in range(1, N + 1):
		if h < H[n] and H[n - 1] <= h:
			count += 1
print(count)


# Optimized solution
import sys

# お約束
args = None
INF = float("inf")
MOD = int(1e9 + 7)
def int1(n):
    return int(n) - 1
def parse(*params):
    if len(params) == 1:
        return params[0](next(args))
    return tuple(p(v) for p, v in zip(params, next(args).split()))
def debug(*v):
    if __debug__:
        print(*v, file=sys.stderr)

# エントリーポイント
def main():
    N = parse(int)
    H = [0] + list(map(int, next(args).split()))

    c = 0
    debug(H)
    for h in range(100):
        for n in range(1, N + 1):
            if H[n - 1] - h <= 0 and 0 < H[n] - h:
                c += 1
    print(c)

if __name__ == '__main__':
    args = iter(sys.stdin.read().split("\n"))
    main()
# Original solution
N=int(eval(input()))
*H,=list(map(int,input().split()))
L=len(H)

ans=False
cnt=0
while ans==False:
    cnt+=1
    m=max(H)
    for i in range(H.index(m), L):
        if H[i]==m:
            H[i]-=1
        else:
            break
    if sum(H)==0: ans=True
print(cnt)

# Optimized solution
N=int(eval(input()))
*H,=list(map(int,input().split()))
L=len(H)

cnt=0
prv=0

for i in range(N):
    if  prv < H[i]:
        cnt+=H[i]-prv
    prv=H[i]
print(cnt)
# Original solution
N = int(eval(input()))
H = list(map(int,input().split()))
A = []
while sum(H)>0:
    hmin = 101
    cur = 0
    for i in range(N):
        h = H[i]
        if h>0:
            hmin = min(hmin,h)
        elif hmin<=100:
            A.append(hmin)
            for j in range(cur,i):
                H[j] -= hmin
            cur = i+1
            hmin = 101
        else:
            cur = i+1
    if cur<N:
        A.append(hmin)
        for j in range(cur,N):
            H[j] -= hmin
        hmin = 101
print((sum(A)))

# Optimized solution
N = int(eval(input()))
H = list(map(int,input().split()))
cnt = 0
while True:
    flag = 0
    for i in range(N):
        if H[i]>0:
            cur = i
            flag = 1
            break
    if flag==0:break
    cmin = H[cur]
    flag = 1
    for i in range(cur,N):
        if H[i]>0 and flag==1:
            cmin = min(cmin,H[i])
        elif H[i]>0 and flag==0:
            cur = i
            cmin = H[i]
            flag = 1
        else:
            cnt += cmin
            for k in range(cur,i):
                H[k] -= cmin
            cur = i
            cmin = 0
            flag = 0
    cnt += cmin
    for k in range(cur,N):
        H[k] -= cmin
print(cnt)
# Original solution
N = int(eval(input()))
H = list(map(int,input().split()))
cnt = 0
while True:
    flag = 0
    for i in range(N):
        if H[i]>0:
            cur = i
            flag = 1
            break
    if flag==0:break
    cmin = H[cur]
    flag = 1
    for i in range(cur,N):
        if H[i]>0 and flag==1:
            cmin = min(cmin,H[i])
        elif H[i]>0 and flag==0:
            cur = i
            cmin = H[i]
            flag = 1
        else:
            cnt += cmin
            for k in range(cur,i):
                H[k] -= cmin
            cur = i
            cmin = 0
            flag = 0
    cnt += cmin
    for k in range(cur,N):
        H[k] -= cmin
print(cnt)

# Optimized solution
N = int(eval(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)
# Original solution
n = int(eval(input()))
li_h = list(map(int,input().split()))
li_a = [0]*n 
ans = 0
li_count = [True]*100
while True:
    for i in range(n):
        if li_h[i] == 0:
            li_count[i] = False
    temp = li_count[0]
    if temp == True:
        count = 1
    else:
        count = 0
    for i in range(n):
        if temp != li_count[i]:
            temp = li_count[i]
            if temp == True:
                count += 1
    ans += count

    for i in range(n):
        if li_h[i] != 0:
            li_h[i] -= 1
    if li_h == li_a:
        break

print(ans)
   


# Optimized solution
n = int(eval(input()))
a = [int(i) for i in input().split()]
a.append(-1)
ans = 0

while max(a):
    max_a = max(a)
    i = 0
    while i < n and i >= 0:
        if a[i] == max_a:
            a[i] -= 1
            while a[i+1] == max_a:
                i += 1
                a[i] -= 1
            ans += 1
        i += 1
print(ans)
# Original solution
import math
from math import gcd
INF = float("inf")

import sys
input=sys.stdin.readline
sys.setrecursionlimit(500*500)
import itertools
from collections import Counter,deque

def main():
    n = int(eval(input()))
    h = list(map(int, input().split()))
    l = [[0]*100 for _ in range(100)]
    for i in range(len(h)):
        k = h[i]
        for q in range(k):
            l[i][q] = 1
    l = [list(x) for x in zip(*l)]
    ans = 0
    for i in l:
        flg = 0
        for g in i:
            if g == 1:
                if flg == 0:
                    ans += 1
                flg = 1
            if g == 0:
                flg = 0
    print(ans)


if __name__=="__main__":
    main()


# Optimized solution
n = int(eval(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)
# Original solution
n,*h=list(map(int,open(0).read().split()));print((sum(max(0,b-a)for a,b in zip([0]+h[:-1],h))))

# Optimized solution
eval(input());s=p=0
for x in map(int,input().split()):s+=max(x-p,0);p=x
print(s)
# Original solution
n = int(eval(input()))
lis = list(map(int,input().split()))
ans = 0
co = 0
for i in range(n):
    while lis[i] != 0:
        cou = i
        while lis[cou] > 0:
            lis[cou] -= 1
            cou += 1
            if cou == n:
                break
        ans += 1
print(ans)

# Optimized solution
n= int(eval(input()))
lis = list(map(int,input().split()))
ans = 0
status = 0
for high in range(1,101):
    for i in range(n):
        if lis[i] >= high:
            status = 1
        else:
            if status == 1:
                ans += 1
                status = 0
    ans += status
    status = 0
print(ans)
# Original solution
import itertools
import math
import fractions
import functools
import copy
n = int(eval(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)

# Optimized solution
def main():
    n = int(eval(input()))
    # n, m = map(int, input().split())
    h = list(map(int, input().split()))
    # s = input()
    # h = [int(input()) for _ in range(n)]

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



if __name__ == '__main__':
    main()
# Original solution
from collections import deque

def sol():
  q = deque()
  q.append(H)
  ans = 0
  while len(q) != 0:
    prv_H = q.pop()
    min_h = min(prv_H)
    ans += min_h
    
    tmp_H = ''.join([',' if h-min_h == 0 else str(h-min_h)+' ' for h in prv_H])
    for sub_H in tmp_H.split(','):
      if len(sub_H.replace(' ','')) != 0 :
        q.append([int(h) for h in sub_H.split()])
    #print(ans, q) 
  return ans

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


# Optimized solution
import itertools


def get_num_1group(input_list):
    num_group = 0
    for key, elems in itertools.groupby(input_list):
        if key == 1:
            num_group += 1
    return num_group


N = int(eval(input()))
H = list(map(int, input().split()))
maxH = max(H)
hist = [[0 for _ in range(N)] for _ in range(maxH)]
for idx, h in enumerate(H):
    for num in range(h):
        hist[num][idx] = 1

print((sum([get_num_1group(hist[num]) for num in range(maxH)])))
# Original solution
n = int(eval(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)

# Optimized solution
N = int(eval(input()))

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

last = 0
ans = 0
for h in H:
    if h >= last:
        ans += h - last
    else:
        if last == 0:
            ans += h
    last = h

print(ans)
# Original solution
n = int(eval(input()))
h = list(map(int, input().split()))

cnt = 0
while max(set(h)) > 0:
    flg = False
    for i in h:
        if i <= 0:
            flg = False
            continue
        else:
            if not flg:
                cnt += 1
            flg = True
    h = list([x-1 for x in h])

print(cnt)

# Optimized solution
n = int(eval(input()))
h = list(map(int, input().split()))

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

print(ans)
# Original solution
n=int(eval(input()))
Tree_List=list(map(int,input().split()))
ct =0
for i in range(n):
    while Tree_List[i] != 0:
        k = i
        while k + 1 < n and Tree_List[k + 1] != 0: 
            k+=1
        Tree_List[i:k + 1]=[x-1 for x in Tree_List[i:k + 1]]
        ct += 1
        
print(ct)

# Optimized solution
n=int(eval(input()))
Tree_List=list(map(int,input().split()))
ct =0
for i in range(n):
    while Tree_List[i] != 0:
        k = i + 1
        while k < n and Tree_List[k] != 0: 
            k+=1
        Tree_List[i:k]=[x-1 for x in Tree_List[i:k]]
        ct += 1
        
print(ct)
# Original solution
s = int(eval(input()))
a = list(map(int, input().split()))

incFlg = True   #登る場合はTure,　降る場合はFalse
before_height = 0
sum = 0

for i in range(len(a)-1):
    #降る場合
    if a[i] > a[i+1]:
        #その前が登り
        if incFlg:
            sum += a[i] - before_height
            incFlg = False
        #その前が下り
        else:
            before_height = a[i]
    #登る場合
    else:
        if not incFlg:
            before_height = a[i]
        incFlg = True

if incFlg:
    sum += a[len(a)-1] - before_height

print(sum)


# Optimized solution
s = int(eval(input()))
a = list(map(int, input().split()))

incFlg = True   #登る場合はTure,　降る場合はFalse
before_height = 0
sum = 0

for i in range(len(a)-1):
    #降る場合
    if a[i] > a[i+1]:
        #その前が登り
        if incFlg:
            sum += a[i] - before_height
            incFlg = False
        #その前が下り
        # else:
        #     # before_height = a[i]
    #登る場合
    else:
        if not incFlg:
            before_height = a[i]
        incFlg = True

if incFlg:
    sum += a[len(a)-1] - before_height

print(sum)
# Original solution
n=int(eval(input()))
h=list(map(int,input().split()))
ans=0
while sum(h)>0:
    i=0
    while i < n:
        if h[i]>0:
            ans+=1    
            while i<n and h[i]>0:
                h[i]-=1
                i+=1
        i+=1
print(ans)

# Optimized solution
n=int(eval(input()))
h=list(map(int,input().split()))
ans=0
pre=0
for hi in h:
  if hi-pre>0:
    ans+=hi-pre
  pre=hi
print(ans)
# Original solution
N = int(eval(input()))
H = list(map(int, input().split()))
H.append(0)

t = 1
ans = 0
while t > 0:
    p = H[-1]
    t = 0
    for i in range(N):
        if H[i] > 0 and p <= 0:
            ans += 1
        p = H[i]
        H[i] -= 1
        t = max(H[i], t)
        #print(ans, H[i], t)

print(ans)


# Optimized solution
N = int(eval(input()))
H = list(map(int, input().split()))

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

    pre = h

print(ans)
# Original solution
N = int(eval(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)

# Optimized solution
N = int(eval(input()))
h = list(map(int, input().split()))

h += [0]
prev = 0
imos = [None] * (N + 1)
for i in range(N + 1):
    imos[i] = h[i] - prev
    prev = h[i]
    
# print("#", h)
# print("#", imos)
ans = sum([int(item > 0) * item for item in imos])
print(ans)
# Original solution
def solve(l):
    if len(l)<2:
        return max(0,l[0])
    m=10**18
    a=0
    for i in range(len(l)):
        if l[i]<1:
            if i+1<len(l):
                a+=solve(l[i+1:])
            else:
                l=l[:i]
            break
        m=min(m,l[i])
    l=l[:i+1]
    for i in range(len(l)):
        l[i]-=m
    return solve(l)+a+m*(m!=10**18)

n=int(eval(input()))
*h,=list(map(int,input().split()))
print((solve(h)))

# Optimized solution
def solve(h):
    if len(h)<2:
        return h[0]
    s=[i for i in range(len(h))if h[i]<1]
    s=[-1]+s+[len(h)]
    a=0
    for l in[h[i+1:j]for i,j in zip(s,s[1:])]:
        if not l:continue
        v=min(l)
        l=[t-v for t in l]
        a+=v+solve(l)
    return a
eval(input())
print((solve(list(map(int,input().split())))))
# Original solution
N = int(eval(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)

# Optimized solution
N = int(eval(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)
# Original solution
a = int(eval(input()))
b = [int(i) for i in input().split()]

numbers = min(b)

b = [i - min(b) for i in b]

theend = False

while theend == False:
  
    if b.count(0) == a:
        theend = True
        break

    indexnotzero = 0

    for i in range(len(b)):
        if b[i] == 0:
            continue
        else:
            indexnotzero = i
            break

    if indexnotzero == len(b) - 1:
        numbers += b[len(b) - 1]
        break

    try:
        length = b.index(0, indexnotzero)
    except ValueError:
        length = len(b)
    minimum = min(b[indexnotzero:length])

    numbers += minimum

    b[indexnotzero:length] = [i - minimum for i in b[indexnotzero:length]]

print(numbers)

# Optimized solution
a = int(eval(input()))
b = [int(i) for i in input().split()]

numbers = 0
theend = False

while theend == False:
    if b.count(0) == a:
        theend = True
        break

    indexnotzero = 0

    for i in range(len(b)):
        if b[i] == 0:
            continue
        else:
            indexnotzero = i
            break

    if indexnotzero == len(b) - 1:
        numbers += b[len(b) - 1]
        break

    try:
        length = b.index(0, indexnotzero)
    except ValueError:
        length = len(b)
    minimum = min(b[indexnotzero:length])

    numbers += minimum

    b[indexnotzero:length] = [i - minimum for i in b[indexnotzero:length]]

print(numbers)
# Original solution
a = int(eval(input()))
b = [int(i) for i in input().split()]

numbers = 0
theend = False

while theend == False:
    if b.count(0) == a:
        theend = True
        break

    indexnotzero = 0

    for i in range(len(b)):
        if b[i] == 0:
            continue
        else:
            indexnotzero = i
            break

    if indexnotzero == len(b) - 1:
        numbers += b[len(b) - 1]
        break

    try:
        length = b.index(0, indexnotzero)
    except ValueError:
        length = len(b)
    minimum = min(b[indexnotzero:length])

    numbers += minimum

    b[indexnotzero:length] = [i - minimum for i in b[indexnotzero:length]]

print(numbers)

# Optimized solution
N = int(eval(input()))
lis_h = list(map(int, input().split()))

water = 0
before = 0
for i in lis_h:
    if i > before:
        water += i - before
    before = i

print(water)
# Original solution
n = int(eval(input()))
h = list(map(int, input().split()))
h.insert(0, 0)
h.append(0)
cnt = 0
while True:
    s = -1
    e = -1
    m_n = 10 ** 10
    for i in range(1, n + 2):
        if h[i] != 0 and h[i-1] == 0:
            s = i
            m_n = min(m_n, h[i])
            for j in range(i + 1, n + 2):
                if h[j] == 0 and h[j - 1] != 0:
                    e = j - 1
                    break
                else:
                    m_n = min(m_n, h[j])
    if s == -1 and e == -1:
        print(cnt)
        exit()
    else:
        cnt += m_n
        for i in range(s, e + 1):
            h[i] -= m_n



# Optimized solution
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    h = list(map(int, input().rstrip('\n').split()))
    l = 0
    cnt = 0
    for i in range(n):
        if l < h[i]:
            cnt += h[i] - l
        l = h[i]
    print(cnt)


if __name__ == '__main__':
    slove()
# Original solution
n = int(eval(input()))
h = list(map(int, input().split()))

t = [0] * n
ans = 0

for i in range(n):
    while t[i] < h[i]:
        for j in range(i, n):
            if t[j] < h[j]:
                t[j] += 1
            else:
                break

        ans += 1

print(ans)

# Optimized solution
n = int(eval(input()))
h = list(map(int, input().split()))

s = [0] * n
cnt = 0

while True:
    flag = 0
    for i in range(n):
        if h[i] != s[i]:
            s[i] += 1
            flag = 1
        elif h[i] == s[i] and flag:
            cnt += 1
            flag = 0
    if flag:
        cnt += 1

    if h == s:
        break

print(cnt)
# Original solution
N = int(eval(input()))
H = list(map(int, input().split()))
cnt = 0
while any(H):
  st = False
  for i, v in enumerate(H):
#    print(H, cnt)
#    print(v)
    if v > 0:
      st = True
      H[i] -= 1
#      print(H)
    else:
      if st == True:
        cnt += 1
        st = False
        break
  if st == True:
    cnt += 1
    st = False
print(cnt)
    
      

# Optimized solution
n = int(eval(input()))
H = [0] + list(map(int, input().split())) + [0]
ans = sum([max(0, j - i) for i, j in zip(H, H[1:])])
print(ans)
# Original solution
n = int(eval(input()))
h = list(int(i) for i in input().split())
count = 0
while not all(i==0 for i in h):
    if 0 in h:
        zero_index = [i for i,x in enumerate(h) if x==0]
        for i in range(len(zero_index)):
            if i==0:
                if not all(h[j]==0 for j in range(zero_index[i])):
                    for j in range(zero_index[i]):
                        h[j] -= 1
                    count += 1
            elif i==len(zero_index)-1 and not zero_index[i]==n-1:
                if not all(h[j]==0 for j in range(zero_index[i]+1,n)):
                    for j in range(zero_index[i]+1,n):
                        h[j] -= 1
                    count += 1
            else:
                if not all(h[j]==0 for j in range(zero_index[i-1]+1,zero_index[i])):
                    for j in range(zero_index[i-1]+1, zero_index[i]):
                        h[j] -= 1
                    count += 1
    else:
        for i in range(n):
            h[i] -=1
        count += 1
print(count)

# Optimized solution
n = int(eval(input()))
h = list(int(i) for i in input().split())
count = 0
while not all(height==0 for height in h):
    index = [i for i,x in enumerate(h) if not x==0]
    for i in range(len(index)):
        h[index[i]] -= 1
        if not i==0:
            if index[i]-index[i-1]>1:
                count += 1
    count += 1
print(count)
# Original solution
n=int(eval(input()))
h=[int(i) for i in input().split()]
j=0
ans=0
while True:
  if h[0]>j:
    ans+=1
  for i in range(1,n):
    if h[i-1]<=j and h [i]>j:
      ans+=1
  j+=1
  if j==max(h):
    break
print(ans) 

# Optimized solution
n=int(eval(input()))
h=[int(i) for i in input().split()]
j=0
ans=0
while j<max(h):
  if h[0]>j:
    ans+=1
  for i in range(1,n):
    if h[i-1]<=j and h [i]>j:
      ans+=1
  j+=1
print(ans)
# Original solution
import sys
from bisect import *
from heapq import *
from collections import *
from itertools import *
from functools import *
from math import *
from fractions import *

sys.setrecursionlimit(100000000)
input = lambda: sys.stdin.readline().rstrip()

def main():
    N = int(eval(input()))
    h = list(map(int, input().split()))
    ans = 0
    while sum(h) > 0:
        for i in range(N):
            for j in reversed(list(range(1, N - i + 1))):
                if min(h[i:i + j]) > 0:
                    for k in range(i, i + j):
                        h[k] -= 1
                    ans += 1
                    break
            else:
                continue
            break
    print(ans)

main()


# Optimized solution
import sys
from bisect import *
from heapq import *
from collections import *
from itertools import *
from functools import *
from math import *
from fractions import *

sys.setrecursionlimit(100000000)
input = lambda: sys.stdin.readline().rstrip()

def f(arr):
    m = min(arr)
    for i in range(len(arr)):
        arr[i] -= m
    buf = []
    ret = m
    for i in arr:
        if i == 0:
            if len(buf) > 0:
                ret += f(buf)
                buf = []
        else:
            buf.append(i)
    if len(buf) > 0:
        ret += f(buf)
    return ret

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

main()
# Original solution
N = int(eval(input()))
H = list(map(int, input().split()))
count = 0
H.append(0)
for lv in range(1, 101):
    flag = False
    for h in H:
        if flag and h < lv:
            count += 1
            flag = False
        elif not flag and h >= lv:
            flag = True
print(count)


# Optimized solution
N=int(eval(input()))
*H,=list(map(int, input().split()))
H = [0] + H
ans = 0
for i in range(1,N+1):
    w = H[i] - H[i-1]
    if w > 0: ans += w
print(ans)