N = int(eval(input()))

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

if h == [0]*N:

    print((0))

    exit()

for i in range(1,10001):

    f = False

    for j in range(N):

        if h[j] > 0:

            h[j] -= 1

            f = True

        elif h[j] <= 0 and f:

            break

    if h == [0]*N:

        ans = i

        break

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

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

cnt = 0

for _ in range(10**4):

    if any(H) is False:

        break

    cnt += 1

    f = False

    for i in range(len(H)):

        if H[i] > 0:

            H[i] -= 1

            f = True

        elif f and H[i] == 0:

            break

print(cnt)
N = int(eval(input()))
h = list(map(int, input().split()))
if h == [0] * N:
    print((0))
    exit()
for i in range(1, 10001):
    f = False
    for j in range(N):
        if h[j] > 0:
            h[j] -= 1
            f = True
        elif h[j] <= 0 and f:
            break
    if h == [0] * N:
        ans = i
        break
print(ans)

N = int(eval(input()))
H = list(map(int, input().split()))
cnt = 0
for _ in range(10**4):
    if any(H) is False:
        break
    cnt += 1
    f = False
    for i in range(len(H)):
        if H[i] > 0:
            H[i] -= 1
            f = True
        elif f and H[i] == 0:
            break
print(cnt)

-h = list(map(int, input().split())) -if h == [0] * N: -    print((0)) -    exit() -for i in range(1, 10001): +H = list(map(int, input().split())) +cnt = 0 +for _ in range(10**4): +    if any(H) is False: +        break +    cnt += 1 -    for j in range(N): -        if h[j] > 0: -            h[j] -= 1 +    for i in range(len(H)): +        if H[i] > 0: +            H[i] -= 1 -        elif h[j] <= 0 and f: +        elif f and H[i] == 0: -    if h == [0] * N: -        ans = i -        break -print(ans) +print(cnt)