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())))))
_,h=open(0);s=p=0

for x in map(int,h.split()):s+=max(x-p,0);p=x

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

_, h = open(0)
s = p = 0
for x in map(int, h.split()):
    s += max(x - p, 0)
    p = x
print(s)

-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()))))) +_, h = open(0) +s = p = 0 +for x in map(int, h.split()): +    s += max(x - p, 0) +    p = x +print(s)