N = int(eval(input()))

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

cnt = 0

while True:

    flag = 0

    for i in range(N):

        if H[i]>0:

            cur = i

            flag = 1

            break

    if flag==0:break

    cmin = H[cur]

    flag = 1

    for i in range(cur,N):

        if H[i]>0 and flag==1:

            cmin = min(cmin,H[i])

        elif H[i]>0 and flag==0:

            cur = i

            cmin = H[i]

            flag = 1

        else:

            cnt += cmin

            for k in range(cur,i):

                H[k] -= cmin

            cur = i

            cmin = 0

            flag = 0

    cnt += cmin

    for k in range(cur,N):

        H[k] -= cmin

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

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

tot = 0

while True:

    if H[0]>0:

        cnt = 1

    else:

        cnt = 0

    for i in range(1,N):

        if H[i]>0 and H[i-1]==0:

            cnt += 1

    tot += cnt

    for i in range(N):

        if H[i]>0:

            H[i] -= 1

    flag = 0

    for i in range(N):

        if H[i]>0:

            flag = 1

            break

    if flag==0:break

print(tot)    
N = int(eval(input()))
H = list(map(int, input().split()))
cnt = 0
while True:
    flag = 0
    for i in range(N):
        if H[i] > 0:
            cur = i
            flag = 1
            break
    if flag == 0:
        break
    cmin = H[cur]
    flag = 1
    for i in range(cur, N):
        if H[i] > 0 and flag == 1:
            cmin = min(cmin, H[i])
        elif H[i] > 0 and flag == 0:
            cur = i
            cmin = H[i]
            flag = 1
        else:
            cnt += cmin
            for k in range(cur, i):
                H[k] -= cmin
            cur = i
            cmin = 0
            flag = 0
    cnt += cmin
    for k in range(cur, N):
        H[k] -= cmin
print(cnt)

N = int(eval(input()))
H = list(map(int, input().split()))
tot = 0
while True:
    if H[0] > 0:
        cnt = 1
    else:
        cnt = 0
    for i in range(1, N):
        if H[i] > 0 and H[i - 1] == 0:
            cnt += 1
    tot += cnt
    for i in range(N):
        if H[i] > 0:
            H[i] -= 1
    flag = 0
    for i in range(N):
        if H[i] > 0:
            flag = 1
            break
    if flag == 0:
        break
print(tot)

-cnt = 0 +tot = 0 +    if H[0] > 0: +        cnt = 1 +    else: +        cnt = 0 +    for i in range(1, N): +        if H[i] > 0 and H[i - 1] == 0: +            cnt += 1 +    tot += cnt +    for i in range(N): +        if H[i] > 0: +            H[i] -= 1 -            cur = i -    cmin = H[cur] -    flag = 1 -    for i in range(cur, N): -        if H[i] > 0 and flag == 1: -            cmin = min(cmin, H[i]) -        elif H[i] > 0 and flag == 0: -            cur = i -            cmin = H[i] -            flag = 1 -        else: -            cnt += cmin -            for k in range(cur, i): -                H[k] -= cmin -            cur = i -            cmin = 0 -            flag = 0 -    cnt += cmin -    for k in range(cur, N): -        H[k] -= cmin -print(cnt) +print(tot)