using System;

class Program
{
  static void Main(string[] args)
  {
    uint N = uint.Parse(Console.ReadLine());
    int[] A = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);

    int curMaxHeight = 0;
    int sum = 0;
    foreach (var a in A)
    {
      if (a >= curMaxHeight)
      {
        curMaxHeight = a;
      }
      else
      {
          sum += curMaxHeight - a;
      }
    }

    Console.WriteLine(sum);
    return;
  }
}
