a = int(eval(input()))

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

b.insert(0, 0)

b.insert(len(b), 0)

f = 0



while True:

    s = -1

    e = -1

    for i in range(len(b)):

        if b[i] != 0 and s == -1:

            s = i

        elif b[i] == 0 and s != -1 and b[i-1] == 0:

            s = i

        elif b[i] == 0 and s != -1:

            e = i

            break

    if s != -1 and e != -1:

        f += 1

        for i in range(s, e):

            b[i] -= 1

    elif s == -1 and e == -1:

        print(f)

        exit()
a = int(eval(input()))

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

b.insert(0, 0)

b.append(0)

d = 0

while True:

    for i in range(1, len(b)):

        if b[i] != 0 and b[i-1] == 0:

            for j in range(i, len(b)):

                if b[j] == 0 and b[j-1] != 0:

                    d += 1

                    for k in range(i, j):

                        b[k] -= 1

                    break

    if sum(b) == 0:

        break

print(d)

a = int(eval(input()))
b = list(map(int, input().split()))
b.insert(0, 0)
b.insert(len(b), 0)
f = 0
while True:
    s = -1
    e = -1
    for i in range(len(b)):
        if b[i] != 0 and s == -1:
            s = i
        elif b[i] == 0 and s != -1 and b[i - 1] == 0:
            s = i
        elif b[i] == 0 and s != -1:
            e = i
            break
    if s != -1 and e != -1:
        f += 1
        for i in range(s, e):
            b[i] -= 1
    elif s == -1 and e == -1:
        print(f)
        exit()

a = int(eval(input()))
b = list(map(int, input().split()))
b.insert(0, 0)
b.append(0)
d = 0
while True:
    for i in range(1, len(b)):
        if b[i] != 0 and b[i - 1] == 0:
            for j in range(i, len(b)):
                if b[j] == 0 and b[j - 1] != 0:
                    d += 1
                    for k in range(i, j):
                        b[k] -= 1
                    break
    if sum(b) == 0:
        break
print(d)

-b.insert(len(b), 0) -f = 0 +b.append(0) +d = 0 -    s = -1 -    e = -1 -    for i in range(len(b)): -        if b[i] != 0 and s == -1: -            s = i -        elif b[i] == 0 and s != -1 and b[i - 1] == 0: -            s = i -        elif b[i] == 0 and s != -1: -            e = i -            break -    if s != -1 and e != -1: -        f += 1 -        for i in range(s, e): -            b[i] -= 1 -    elif s == -1 and e == -1: -        print(f) -        exit() +    for i in range(1, len(b)): +        if b[i] != 0 and b[i - 1] == 0: +            for j in range(i, len(b)): +                if b[j] == 0 and b[j - 1] != 0: +                    d += 1 +                    for k in range(i, j): +                        b[k] -= 1 +                    break +    if sum(b) == 0: +        break +print(d)