def solve_range(h, frm, to):

    """[frm, to)"""

    # print(frm, to, h)

    if frm >= to:

        return 0

    res = 0

    start = frm

    for i in range(frm, to):

        h[i] -= 1

        if h[i] < 0:

            if start < i:

                res += solve_range(h, start, i) + 1

            start = i + 1

        elif i == to - 1:

            res += solve_range(h, start, to) + 1

    return res





def solve(h):

    return solve_range(h, 0, n)





n = int(eval(input()))

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



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

    N = int(eval(input()))

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



    cnt = 0

    p = 0

    for h in H:

        cnt += max(0, h - p)

        p = h

    print(cnt)





if __name__ == '__main__':

    main()

def solve_range(h, frm, to):
    """[frm, to)"""
    # print(frm, to, h)
    if frm >= to:
        return 0
    res = 0
    start = frm
    for i in range(frm, to):
        h[i] -= 1
        if h[i] < 0:
            if start < i:
                res += solve_range(h, start, i) + 1
            start = i + 1
        elif i == to - 1:
            res += solve_range(h, start, to) + 1
    return res


def solve(h):
    return solve_range(h, 0, n)


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

def main():
    N = int(eval(input()))
    H = list(map(int, input().split()))
    cnt = 0
    p = 0
    for h in H:
        cnt += max(0, h - p)
        p = h
    print(cnt)


if __name__ == "__main__":
    main()

-def solve_range(h, frm, to): -    """[frm, to)""" -    # print(frm, to, h) -    if frm >= to: -        return 0 -    res = 0 -    start = frm -    for i in range(frm, to): -        h[i] -= 1 -        if h[i] < 0: -            if start < i: -                res += solve_range(h, start, i) + 1 -            start = i + 1 -        elif i == to - 1: -            res += solve_range(h, start, to) + 1 -    return res +def main(): +    N = int(eval(input())) +    H = list(map(int, input().split())) +    cnt = 0 +    p = 0 +    for h in H: +        cnt += max(0, h - p) +        p = h +    print(cnt) -def solve(h): -    return solve_range(h, 0, n) - - -n = int(eval(input())) -h = list(map(int, input().split())) -print((solve(h))) +if __name__ == "__main__": +    main()