eval(input())

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

def mizuyari(*f):

  num = 0

  if f == ():

    return 0

  f_min = min(f)

  f_min_index = f.index(f_min)

  num += f_min

  f = [i-f_min for i in f]

  num += mizuyari(*f[:f_min_index])

  num += mizuyari(*f[f_min_index+1:])

  return num



ans = mizuyari(0, *flowers)

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

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

s = [(0, n)]

ans = 0

while s:

    l, r = s.pop()

    if l == r:

        continue

    m = min(a[l:r])

    ans += m

    for i in range(l, r):

        a[i] -= m

    for i in range(l, r):

        if a[i] == 0:

            s.append((l, i))

            s.append((i + 1, r))

            break

print(ans)
eval(input())
flowers = list(map(int, input().split()))


def mizuyari(*f):
    num = 0
    if f == ():
        return 0
    f_min = min(f)
    f_min_index = f.index(f_min)
    num += f_min
    f = [i - f_min for i in f]
    num += mizuyari(*f[:f_min_index])
    num += mizuyari(*f[f_min_index + 1 :])
    return num


ans = mizuyari(0, *flowers)
print(ans)

n = int(eval(input()))
a = list(map(int, input().split()))
s = [(0, n)]
ans = 0
while s:
    l, r = s.pop()
    if l == r:
        continue
    m = min(a[l:r])
    ans += m
    for i in range(l, r):
        a[i] -= m
    for i in range(l, r):
        if a[i] == 0:
            s.append((l, i))
            s.append((i + 1, r))
            break
print(ans)

-eval(input()) -flowers = list(map(int, input().split())) - - -def mizuyari(*f): -    num = 0 -    if f == (): -        return 0 -    f_min = min(f) -    f_min_index = f.index(f_min) -    num += f_min -    f = [i - f_min for i in f] -    num += mizuyari(*f[:f_min_index]) -    num += mizuyari(*f[f_min_index + 1 :]) -    return num - - -ans = mizuyari(0, *flowers) +n = int(eval(input())) +a = list(map(int, input().split())) +s = [(0, n)] +ans = 0 +while s: +    l, r = s.pop() +    if l == r: +        continue +    m = min(a[l:r]) +    ans += m +    for i in range(l, r): +        a[i] -= m +    for i in range(l, r): +        if a[i] == 0: +            s.append((l, i)) +            s.append((i + 1, r)) +            break