n = int(eval(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)

import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines

sys.setrecursionlimit(10 ** 7)



n, *h = list(map(int, read().split()))

ans = 0

flag = False

for bf, af in zip(h, h[1:]):

    if bf <= af:

        if flag:

            ans += t - bf

        flag = False

    else:

        if not flag:

            t = bf

        flag = True

if flag:

    ans += t

else:

    ans += h[-1]

print(ans)

n = int(eval(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)

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**7)
n, *h = list(map(int, read().split()))
ans = 0
flag = False
for bf, af in zip(h, h[1:]):
    if bf <= af:
        if flag:
            ans += t - bf
        flag = False
    else:
        if not flag:
            t = bf
        flag = True
if flag:
    ans += t
else:
    ans += h[-1]
print(ans)

-n = int(eval(input())) -h = list(map(int, input().split())) +import sys + +read = sys.stdin.buffer.read +readline = sys.stdin.buffer.readline +readlines = sys.stdin.buffer.readlines +sys.setrecursionlimit(10**7) +n, *h = list(map(int, read().split())) -check = 0 -for i in h: -    if check < i: -        ans += i - check -    check = i +flag = False +for bf, af in zip(h, h[1:]): +    if bf <= af: +        if flag: +            ans += t - bf +        flag = False +    else: +        if not flag: +            t = bf +        flag = True +if flag: +    ans += t +else: +    ans += h[-1]