def solve(l):

    if len(l)<2:

        return max(0,l[0])

    m=10**18

    a=0

    for i in range(len(l)):

        if l[i]<1:

            if i+1<len(l):

                a+=solve(l[i+1:])

            else:

                l=l[:i]

            break

        m=min(m,l[i])

    l=l[:i+1]

    for i in range(len(l)):

        l[i]-=m

    return solve(l)+a+m*(m!=10**18)



n=int(eval(input()))

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

print((solve(h)))
def solve(h):

    if len(h)<2:

        return h[0]

    s=[i for i in range(len(h))if h[i]<1]

    s=[-1]+s+[len(h)]

    a=0

    for l in[h[i+1:j]for i,j in zip(s,s[1:])]:

        if not l:continue

        v=min(l)

        l=[t-v for t in l]

        a+=v+solve(l)

    return a

eval(input())

print((solve(list(map(int,input().split())))))
def solve(l):
    if len(l) < 2:
        return max(0, l[0])
    m = 10**18
    a = 0
    for i in range(len(l)):
        if l[i] < 1:
            if i + 1 < len(l):
                a += solve(l[i + 1 :])
            else:
                l = l[:i]
            break
        m = min(m, l[i])
    l = l[: i + 1]
    for i in range(len(l)):
        l[i] -= m
    return solve(l) + a + m * (m != 10**18)


n = int(eval(input()))
(*h,) = list(map(int, input().split()))
print((solve(h)))

def solve(h):
    if len(h) < 2:
        return h[0]
    s = [i for i in range(len(h)) if h[i] < 1]
    s = [-1] + s + [len(h)]
    a = 0
    for l in [h[i + 1 : j] for i, j in zip(s, s[1:])]:
        if not l:
            continue
        v = min(l)
        l = [t - v for t in l]
        a += v + solve(l)
    return a


eval(input())
print((solve(list(map(int, input().split())))))

-def solve(l): -    if len(l) < 2: -        return max(0, l[0]) -    m = 10**18 +def solve(h): +    if len(h) < 2: +        return h[0] +    s = [i for i in range(len(h)) if h[i] < 1] +    s = [-1] + s + [len(h)] -    for i in range(len(l)): -        if l[i] < 1: -            if i + 1 < len(l): -                a += solve(l[i + 1 :]) -            else: -                l = l[:i] -            break -        m = min(m, l[i]) -    l = l[: i + 1] -    for i in range(len(l)): -        l[i] -= m -    return solve(l) + a + m * (m != 10**18) +    for l in [h[i + 1 : j] for i, j in zip(s, s[1:])]: +        if not l: +            continue +        v = min(l) +        l = [t - v for t in l] +        a += v + solve(l) +    return a -n = int(eval(input())) -(*h,) = list(map(int, input().split())) -print((solve(h))) +eval(input()) +print((solve(list(map(int, input().split())))))