import numpy as np





def RunLengthEncoding(S):

    res = []

    N = len(S)

    i = 0

    while i < N:

        cnt = 1

        while i < N - 1 and S[i] == S[i + 1]:

            cnt += 1

            i += 1

        res.append(S[i])

        i += 1

    return res





N = int(eval(input()))

A = np.array(input().split(), dtype=int)



ans = 0

flower = np.zeros_like(A)

for i in range(1, A.max() + 1):

    f = flower.copy()

    f[A >= i] = 1

    RLE = RunLengthEncoding(f)

    ans += sum(RLE)



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

H = [0] + list(map(int, input().split())) + [0]



ans = 0

while any(H):

    water = min(h for h in H if h)



    cnt = 0

    bl = 0

    for x, y in zip(H[:-1], H[1:]):

        if x < y and x == 0:

            bl = 1

        elif x > y and y == 0 and bl:

            cnt += 1

            bl = 0

    ans += water * cnt

    H = [max(0, h-water) for h in H]





print(ans)
import numpy as np


def RunLengthEncoding(S):
    res = []
    N = len(S)
    i = 0
    while i < N:
        cnt = 1
        while i < N - 1 and S[i] == S[i + 1]:
            cnt += 1
            i += 1
        res.append(S[i])
        i += 1
    return res


N = int(eval(input()))
A = np.array(input().split(), dtype=int)
ans = 0
flower = np.zeros_like(A)
for i in range(1, A.max() + 1):
    f = flower.copy()
    f[A >= i] = 1
    RLE = RunLengthEncoding(f)
    ans += sum(RLE)
print(ans)

N = int(eval(input()))
H = [0] + list(map(int, input().split())) + [0]
ans = 0
while any(H):
    water = min(h for h in H if h)
    cnt = 0
    bl = 0
    for x, y in zip(H[:-1], H[1:]):
        if x < y and x == 0:
            bl = 1
        elif x > y and y == 0 and bl:
            cnt += 1
            bl = 0
    ans += water * cnt
    H = [max(0, h - water) for h in H]
print(ans)

-import numpy as np - - -def RunLengthEncoding(S): -    res = [] -    N = len(S) -    i = 0 -    while i < N: -        cnt = 1 -        while i < N - 1 and S[i] == S[i + 1]: +N = int(eval(input())) +H = [0] + list(map(int, input().split())) + [0] +ans = 0 +while any(H): +    water = min(h for h in H if h) +    cnt = 0 +    bl = 0 +    for x, y in zip(H[:-1], H[1:]): +        if x < y and x == 0: +            bl = 1 +        elif x > y and y == 0 and bl: -            i += 1 -        res.append(S[i]) -        i += 1 -    return res - - -N = int(eval(input())) -A = np.array(input().split(), dtype=int) -ans = 0 -flower = np.zeros_like(A) -for i in range(1, A.max() + 1): -    f = flower.copy() -    f[A >= i] = 1 -    RLE = RunLengthEncoding(f) -    ans += sum(RLE) +            bl = 0 +    ans += water * cnt +    H = [max(0, h - water) for h in H]