def main():
    n = int(input())
    a = list(map(int, input().split(" ")))
    top = a[0]
    ans = 0

    for height in a:
        if top < height:
            ans += height - top
            top = height

    print(ans)


main()
