import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long res = 0;
        long m = 0;
        for (int i = 0; i < n; i++) {
            long a = sc.nextLong();
            m = Math.max(m, a);
            res += m - a;
        }
        System.out.println(res);
        sc.close();
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long res = 0;
        long m = 0;
        for (int i = 0; i < n; i++) {
            long a = sc.nextLong();
            m = Math.max(m, a);
            res += m - a;
        }
        System.out.println(res);
        sc.close();
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long res = 0;
        long m = 0;
        for (int i = 0; i < n; i++) {
            long a = sc.nextLong();
            m = Math.max(m, a);
            res += m - a;
        }
        System.out.println(res);
        sc.close();
    } main {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long res = 0;
        long m = 0;
        for (int i = 0; i < n; i++) {
            long a = sc.nextLong();
            m = Math.max(m, a);
            res += m - a;
        }
        System.out.println(res);
        sc.close();
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc long res = 0; res 0 long m = 0; m 0 for (int i = 0; i < n; i++) {
            long a = sc.nextLong();
            m = Math.max(m, a);
            res += m - a;
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            long a = sc.nextLong();
            m = Math.max(m, a);
            res += m - a;
        } {
            long a = sc.nextLong();
            m = Math.max(m, a);
            res += m - a;
        } long a = sc.nextLong(); a sc.nextLong() sc.nextLong sc m = Math.max(m, a) m Math.max(m, a) Math.max Math m a res += m - a res m - a m a System.out.println(res) System.out.println System.out System System.out res sc.close() sc.close sc String[] args args public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long res = 0;
        long m = 0;
        for (int i = 0; i < n; i++) {
            long a = sc.nextLong();
            m = Math.max(m, a);
            res += m - a;
        }
        System.out.println(res);
        sc.close();
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long res = 0;
        long m = 0;
        for (int i = 0; i < n; i++) {
            long a = sc.nextLong();
            m = Math.max(m, a);
            res += m - a;
        }
        System.out.println(res);
        sc.close();
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long res = 0;
        long m = 0;
        for (int i = 0; i < n; i++) {
            long a = sc.nextLong();
            m = Math.max(m, a);
            res += m - a;
        }
        System.out.println(res);
        sc.close();
    }
}