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];
        for (int i = 0; i < N; i++)
            A[i] = int.Parse (input[i]);
        int step = 0;
        for (int i = 1; i < N; i++) {
            if (A[i] < A[i - 1]) {
                step += A[i - 1] - A[i];
                A[i] += A[i - 1] - A[i];
            }
        }
        //出力
        Console.WriteLine (step);
    }
}