using System;
using System.Linq;

class Program
{
    public static void Main(string[] args)
    {
        var n = int.Parse(Console.ReadLine());
        var aArray = Console.ReadLine().Split();

        int ans = 0;

        int prev = 0;
        for (int i = 0; i < n; i++)
        {
            var current = int.Parse(aArray[i]);

            var f = current - prev;

            if (f >= 0)
            {
                prev = current;
            }
            else
            {
                prev = current - f;
                ans += -f;
            }
        }

        Console.WriteLine(ans);
    }
}
