import numpy as np

n = int(eval(input()))

s = np.array(list(map(int, input().split())), dtype='int64')

count = 0

sidx = 0



ls = [s]

count = 0

while len(ls) != 0:

    l = ls.pop()

    mins = min(l)

    l -= mins

    count += mins

    tmpl = np.array([], dtype='int64')

    for i in l:

        if i == 0:

            if len(tmpl) != 0:

                ls.append(tmpl)

            tmpl = np.array([], dtype='int64')

        else:

            tmpl = np.append(tmpl, i)

    else:

        if len(tmpl) != 0:

            ls.append(tmpl)



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

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

count = 0

sidx = 0



ls = [s]

count = 0

while len(ls) != 0:

    l = ls.pop()

    mins = min(l)

    for i in range(len(l)):

        l[i] -= mins

    count += mins

    tmpl = []

    for i in l:

        if i == 0:

            if len(tmpl) != 0:

                ls.append(tmpl)

            tmpl = []

        else:

            tmpl.append(i)

    else:

        if len(tmpl) != 0:

            ls.append(tmpl)



print(count)
import numpy as np

n = int(eval(input()))
s = np.array(list(map(int, input().split())), dtype="int64")
count = 0
sidx = 0
ls = [s]
count = 0
while len(ls) != 0:
    l = ls.pop()
    mins = min(l)
    l -= mins
    count += mins
    tmpl = np.array([], dtype="int64")
    for i in l:
        if i == 0:
            if len(tmpl) != 0:
                ls.append(tmpl)
            tmpl = np.array([], dtype="int64")
        else:
            tmpl = np.append(tmpl, i)
    else:
        if len(tmpl) != 0:
            ls.append(tmpl)
print((int(count)))

n = int(eval(input()))
s = list(map(int, input().split()))
count = 0
sidx = 0
ls = [s]
count = 0
while len(ls) != 0:
    l = ls.pop()
    mins = min(l)
    for i in range(len(l)):
        l[i] -= mins
    count += mins
    tmpl = []
    for i in l:
        if i == 0:
            if len(tmpl) != 0:
                ls.append(tmpl)
            tmpl = []
        else:
            tmpl.append(i)
    else:
        if len(tmpl) != 0:
            ls.append(tmpl)
print(count)

-import numpy as np - -s = np.array(list(map(int, input().split())), dtype="int64") +s = list(map(int, input().split())) -    l -= mins +    for i in range(len(l)): +        l[i] -= mins -    tmpl = np.array([], dtype="int64") +    tmpl = [] -            tmpl = np.array([], dtype="int64") +            tmpl = [] -            tmpl = np.append(tmpl, i) +            tmpl.append(i) -print((int(count))) +print(count)