N = int(eval(input()))

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

if sum(h) == 0:

    print((0))

    exit()

ans = 0

flag = False

while True:

    if h[0] == 0:

        flag = True

    for i in range(N):

        if h[i] == 0 and flag == True:

            continue

        elif h[i] == 0 and flag == False:

            break

        h[i] -= 1

        flag = False

    ans += 1

    if sum(h) == 0:

        break

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)
N = int(eval(input()))
h = list(map(int, input().split()))
if sum(h) == 0:
    print((0))
    exit()
ans = 0
flag = False
while True:
    if h[0] == 0:
        flag = True
    for i in range(N):
        if h[i] == 0 and flag == True:
            continue
        elif h[i] == 0 and flag == False:
            break
        h[i] -= 1
        flag = False
    ans += 1
    if sum(h) == 0:
        break
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)

-h = list(map(int, input().split())) -if sum(h) == 0: -    print((0)) -    exit() +array = list(map(int, input().split())) -flag = False -    if h[0] == 0: -        flag = True +    if max(array) == 0: +        break +    flg = False -        if h[i] == 0 and flag == True: +        if array[i] == 0: +            if flg: +                break -        elif h[i] == 0 and flag == False: -            break -        h[i] -= 1 -        flag = False +        array[i] -= 1 +        flg = True -    if sum(h) == 0: -        break