using System;
class Program
 {
        static void Main(string[] args)
        {
    　　int N = int.Parse(Console.ReadLine()); //標準入力
        string dd = Console.ReadLine();
          string[] A = dd.Split(' ');
        int[] Ai = new int[A.Length];
          
          int maxshincho = 0;
          
          int dai = 0;
          
          for(int i= 0;i<A.Length;i++){
            Ai[i] = int.Parse(A[i]);
          }
          for(int i= 0;i<Ai.Length-1;i++){
            if(maxshincho<Ai[i]){
              maxshincho=Ai[i];
            }
            if(maxshincho>Ai[i+1]){
              dai = dai + maxshincho-Ai[i+1];
            }
          }
          Console.WriteLine("{0}",dai);
        }
 }