import java.util.*;

public class Main {
    public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	int n = sc.nextInt();
	int[] a = new int[n];
	for (int i=0; i<n; i++) a[i] = sc.nextInt();
	int low = a[0];
	long sum = 0;
	for (int i=1; i<n; i++) {
	    if (low <= a[i]) low = a[i];
	    else {
		sum = sum + low - a[i];
	    }
	}
	System.out.print(sum);
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	int n = sc.nextInt();
	int[] a = new int[n];
	for (int i=0; i<n; i++) a[i] = sc.nextInt();
	int low = a[0];
	long sum = 0;
	for (int i=1; i<n; i++) {
	    if (low <= a[i]) low = a[i];
	    else {
		sum = sum + low - a[i];
	    }
	}
	System.out.print(sum);
    }
} Main public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	int n = sc.nextInt();
	int[] a = new int[n];
	for (int i=0; i<n; i++) a[i] = sc.nextInt();
	int low = a[0];
	long sum = 0;
	for (int i=1; i<n; i++) {
	    if (low <= a[i]) low = a[i];
	    else {
		sum = sum + low - a[i];
	    }
	}
	System.out.print(sum);
    } main {
	Scanner sc = new Scanner(System.in);
	int n = sc.nextInt();
	int[] a = new int[n];
	for (int i=0; i<n; i++) a[i] = sc.nextInt();
	int low = a[0];
	long sum = 0;
	for (int i=1; i<n; i++) {
	    if (low <= a[i]) low = a[i];
	    else {
		sum = sum + low - a[i];
	    }
	}
	System.out.print(sum);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int[] a = new int[n]; a new int[n] n for (int i=0; i<n; i++) a[i] = sc.nextInt(); int i=0; int i=0; i 0 i<n i n i++ i++ i a[i] = sc.nextInt(); a[i] = sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc int low = a[0]; low a[0] a 0 long sum = 0; sum 0 for (int i=1; i<n; i++) {
	    if (low <= a[i]) low = a[i];
	    else {
		sum = sum + low - a[i];
	    }
	} int i=1; int i=1; i 1 i<n i n i++ i++ i {
	    if (low <= a[i]) low = a[i];
	    else {
		sum = sum + low - a[i];
	    }
	} {
	    if (low <= a[i]) low = a[i];
	    else {
		sum = sum + low - a[i];
	    }
	} if (low <= a[i]) low = a[i];
	    else {
		sum = sum + low - a[i];
	    } low <= a[i] low a[i] a i low = a[i] low a[i] a i {
		sum = sum + low - a[i];
	    } sum = sum + low - a[i] sum sum + low - a[i] sum + low sum low a[i] a i System.out.print(sum) System.out.print System.out System System.out sum String[] args args public class Main {
    public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	int n = sc.nextInt();
	int[] a = new int[n];
	for (int i=0; i<n; i++) a[i] = sc.nextInt();
	int low = a[0];
	long sum = 0;
	for (int i=1; i<n; i++) {
	    if (low <= a[i]) low = a[i];
	    else {
		sum = sum + low - a[i];
	    }
	}
	System.out.print(sum);
    }
} public class Main {
    public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	int n = sc.nextInt();
	int[] a = new int[n];
	for (int i=0; i<n; i++) a[i] = sc.nextInt();
	int low = a[0];
	long sum = 0;
	for (int i=1; i<n; i++) {
	    if (low <= a[i]) low = a[i];
	    else {
		sum = sum + low - a[i];
	    }
	}
	System.out.print(sum);
    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	int n = sc.nextInt();
	int[] a = new int[n];
	for (int i=0; i<n; i++) a[i] = sc.nextInt();
	int low = a[0];
	long sum = 0;
	for (int i=1; i<n; i++) {
	    if (low <= a[i]) low = a[i];
	    else {
		sum = sum + low - a[i];
	    }
	}
	System.out.print(sum);
    }
}
