N = int(eval(input()))

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



# Hを1段ずつ減らしていく.

# 値が正の区間 (left, right) が一つ見つかるごとに答えは1増える.



ans = 0

while sum(H) > 0:

    left = right = 0

    prev = 0

    segment = []

    for i, h in enumerate(H):

        if h == 0 and prev != 0: # H = {..., 0, *(left), *, ..., *, 0(right), ...} (*: 正数)

            right = i

            segment.append((left, right))

        elif h != 0 and prev == 0:

            left = i

            if i == N-1: # H = {..., 0, *(left)} (*: 正数)

                right = i + 1

                segment.append((left, right))

        elif h != 0 and i == N-1: #(prev != 0) H = {..., 0, *(left), ..., *, *} (*: 正数)

            right = i + 1

            segment.append((left, right))

        prev = h

    ans += len(segment)

    #  Hの値が正のところを1段減らす.

    for l, r in segment:

        for i in range(l, r):

            H[i] -= 1

print(ans)
N = int(eval(input()))

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



stack = [H]

count = 0

while stack:

    lst = stack.pop()

    m = min(lst)

    count += m

    lst = [l-m for l in lst]

    prev = 0

    left = 0

    for i, h in enumerate(lst):

        if h == 0 and prev != 0: # H = {..., 0, *(left), *, ..., *, 0(right), ...} (*: 正数)

            stack.append(lst[left:i])

        elif h != 0 and prev == 0:

            left = i

            if i == len(lst)-1: # H = {..., 0, *(left)} (*: 正数)

                stack.append(lst[left:i+1])

        elif h != 0 and i == len(lst)-1: #(prev != 0) H = {..., 0, *(left), ..., *, *} (*: 正数)

            stack.append(lst[left:i+1])

        prev = h

print(count)
N = int(eval(input()))
H = list(map(int, input().split()))
# Hを1段ずつ減らしていく.
# 値が正の区間 (left, right) が一つ見つかるごとに答えは1増える.
ans = 0
while sum(H) > 0:
    left = right = 0
    prev = 0
    segment = []
    for i, h in enumerate(H):
        if (
            h == 0 and prev != 0
        ):  # H = {..., 0, *(left), *, ..., *, 0(right), ...} (*: 正数)
            right = i
            segment.append((left, right))
        elif h != 0 and prev == 0:
            left = i
            if i == N - 1:  # H = {..., 0, *(left)} (*: 正数)
                right = i + 1
                segment.append((left, right))
        elif (
            h != 0 and i == N - 1
        ):  # (prev != 0) H = {..., 0, *(left), ..., *, *} (*: 正数)
            right = i + 1
            segment.append((left, right))
        prev = h
    ans += len(segment)
    #  Hの値が正のところを1段減らす.
    for l, r in segment:
        for i in range(l, r):
            H[i] -= 1
print(ans)

N = int(eval(input()))
H = list(map(int, input().split()))
stack = [H]
count = 0
while stack:
    lst = stack.pop()
    m = min(lst)
    count += m
    lst = [l - m for l in lst]
    prev = 0
    left = 0
    for i, h in enumerate(lst):
        if (
            h == 0 and prev != 0
        ):  # H = {..., 0, *(left), *, ..., *, 0(right), ...} (*: 正数)
            stack.append(lst[left:i])
        elif h != 0 and prev == 0:
            left = i
            if i == len(lst) - 1:  # H = {..., 0, *(left)} (*: 正数)
                stack.append(lst[left : i + 1])
        elif (
            h != 0 and i == len(lst) - 1
        ):  # (prev != 0) H = {..., 0, *(left), ..., *, *} (*: 正数)
            stack.append(lst[left : i + 1])
        prev = h
print(count)

-# Hを1段ずつ減らしていく. -# 値が正の区間 (left, right) が一つ見つかるごとに答えは1増える. -ans = 0 -while sum(H) > 0: -    left = right = 0 +stack = [H] +count = 0 +while stack: +    lst = stack.pop() +    m = min(lst) +    count += m +    lst = [l - m for l in lst] -    segment = [] -    for i, h in enumerate(H): +    left = 0 +    for i, h in enumerate(lst): -            right = i -            segment.append((left, right)) +            stack.append(lst[left:i]) -            if i == N - 1:  # H = {..., 0, *(left)} (*: 正数) -                right = i + 1 -                segment.append((left, right)) +            if i == len(lst) - 1:  # H = {..., 0, *(left)} (*: 正数) +                stack.append(lst[left : i + 1]) -            h != 0 and i == N - 1 +            h != 0 and i == len(lst) - 1 -            right = i + 1 -            segment.append((left, right)) +            stack.append(lst[left : i + 1]) -    ans += len(segment) -    #  Hの値が正のところを1段減らす. -    for l, r in segment: -        for i in range(l, r): -            H[i] -= 1 -print(ans) +print(count)