using System;

class Program {
    static void Main (string[] args) {
        int N = int.Parse (Console.ReadLine ());
        string[] input = Console.ReadLine ().Split (' ');
        int[] A = new int[N];
        int step = 0;
        for (int i = 0; i < N; i++) {
            A[i] = int.Parse (input[i]);
            if (i > 0) {
                if (A[i] < A[i - 1]) {
                    step += (A[i - 1] - A[i]);
                    A[i] += (A[i - 1] - A[i]);
                }
            }
        }
        //出力
        Console.WriteLine (step.ToString ());
    }
}