N = int(eval(input()))

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

ans = 0

while True:

    if max(array) == 0:

        break

    flg = False

    for i in range(N):

        if array[i] == 0:

            if flg:

                break

            continue

        array[i] -= 1

        flg = True

    ans += 1

print(ans)
import itertools

N = int(eval(input()))

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

ans = 0

while sum(arr) != 0:

    tmp = [0]*N

    tmp2 = 0

    for i in range(N):

        if arr[i] != 0:

            tmp[i] = 1

            arr[i] -= 1

    gr = itertools.groupby(tmp)

    for key, group in gr:

        if key != 0:

            tmp2 += 1

    ans += tmp2

print(ans)
N = int(eval(input()))
array = list(map(int, input().split()))
ans = 0
while True:
    if max(array) == 0:
        break
    flg = False
    for i in range(N):
        if array[i] == 0:
            if flg:
                break
            continue
        array[i] -= 1
        flg = True
    ans += 1
print(ans)

import itertools

N = int(eval(input()))
arr = list(map(int, input().split()))
ans = 0
while sum(arr) != 0:
    tmp = [0] * N
    tmp2 = 0
    for i in range(N):
        if arr[i] != 0:
            tmp[i] = 1
            arr[i] -= 1
    gr = itertools.groupby(tmp)
    for key, group in gr:
        if key != 0:
            tmp2 += 1
    ans += tmp2
print(ans)

+import itertools + -array = list(map(int, input().split())) +arr = list(map(int, input().split())) -while True: -    if max(array) == 0: -        break -    flg = False +while sum(arr) != 0: +    tmp = [0] * N +    tmp2 = 0 -        if array[i] == 0: -            if flg: -                break -            continue -        array[i] -= 1 -        flg = True -    ans += 1 +        if arr[i] != 0: +            tmp[i] = 1 +            arr[i] -= 1 +    gr = itertools.groupby(tmp) +    for key, group in gr: +        if key != 0: +            tmp2 += 1 +    ans += tmp2