n=int(eval(input()))

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

a=[0]*n

ans=0

while True:

    flag=0

    for i in range(n):

        if h[i]>a[i]:

            a[i]+=1

            flag=1

        elif h[i]==a[i] and flag:

            break

    if flag:

        ans+=1

    else:

        break

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

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

ans = 0

while True:

    f = 0

    if all(x==0 for x in h):

        break

    for i in range(n):

        if f and h[i] == 0:

            break

        if h[i]>0:

            h[i] -= 1

            f = 1

    ans += 1

    

print(ans)
n = int(eval(input()))
h = list(map(int, input().split()))
a = [0] * n
ans = 0
while True:
    flag = 0
    for i in range(n):
        if h[i] > a[i]:
            a[i] += 1
            flag = 1
        elif h[i] == a[i] and flag:
            break
    if flag:
        ans += 1
    else:
        break
print(ans)

n = int(eval(input()))
h = list(map(int, input().split()))
ans = 0
while True:
    f = 0
    if all(x == 0 for x in h):
        break
    for i in range(n):
        if f and h[i] == 0:
            break
        if h[i] > 0:
            h[i] -= 1
            f = 1
    ans += 1
print(ans)

-a = [0] * n -    flag = 0 +    f = 0 +    if all(x == 0 for x in h): +        break -        if h[i] > a[i]: -            a[i] += 1 -            flag = 1 -        elif h[i] == a[i] and flag: +        if f and h[i] == 0: -    if flag: -        ans += 1 -    else: -        break +        if h[i] > 0: +            h[i] -= 1 +            f = 1 +    ans += 1