import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
	    Scanner sc = new Scanner(System.in);
	    int n = sc.nextInt();
	    int bfLength = 0;
	    long stepSum = 0;
	    for (int i = 0; i < n; i++) {
		    int a = sc.nextInt();
		    if (bfLength > a) {
		    	stepSum += (bfLength - a);
		    } else {
		    	bfLength = a;
		    }
	    }
	    System.out.println(stepSum);
	}
} 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();
	    int bfLength = 0;
	    long stepSum = 0;
	    for (int i = 0; i < n; i++) {
		    int a = sc.nextInt();
		    if (bfLength > a) {
		    	stepSum += (bfLength - a);
		    } else {
		    	bfLength = a;
		    }
	    }
	    System.out.println(stepSum);
	}
} Main public static void main(String[] args) {
	    Scanner sc = new Scanner(System.in);
	    int n = sc.nextInt();
	    int bfLength = 0;
	    long stepSum = 0;
	    for (int i = 0; i < n; i++) {
		    int a = sc.nextInt();
		    if (bfLength > a) {
		    	stepSum += (bfLength - a);
		    } else {
		    	bfLength = a;
		    }
	    }
	    System.out.println(stepSum);
	} main {
	    Scanner sc = new Scanner(System.in);
	    int n = sc.nextInt();
	    int bfLength = 0;
	    long stepSum = 0;
	    for (int i = 0; i < n; i++) {
		    int a = sc.nextInt();
		    if (bfLength > a) {
		    	stepSum += (bfLength - a);
		    } else {
		    	bfLength = a;
		    }
	    }
	    System.out.println(stepSum);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int bfLength = 0; bfLength 0 long stepSum = 0; stepSum 0 for (int i = 0; i < n; i++) {
		    int a = sc.nextInt();
		    if (bfLength > a) {
		    	stepSum += (bfLength - a);
		    } else {
		    	bfLength = a;
		    }
	    } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
		    int a = sc.nextInt();
		    if (bfLength > a) {
		    	stepSum += (bfLength - a);
		    } else {
		    	bfLength = a;
		    }
	    } {
		    int a = sc.nextInt();
		    if (bfLength > a) {
		    	stepSum += (bfLength - a);
		    } else {
		    	bfLength = a;
		    }
	    } int a = sc.nextInt(); a sc.nextInt() sc.nextInt sc if (bfLength > a) {
		    	stepSum += (bfLength - a);
		    } else {
		    	bfLength = a;
		    } bfLength > a bfLength a {
		    	stepSum += (bfLength - a);
		    } stepSum += (bfLength - a) stepSum (bfLength - a) bfLength a {
		    	bfLength = a;
		    } bfLength = a bfLength a System.out.println(stepSum) System.out.println System.out System System.out stepSum String[] args args public class Main {
	public static void main(String[] args) {
	    Scanner sc = new Scanner(System.in);
	    int n = sc.nextInt();
	    int bfLength = 0;
	    long stepSum = 0;
	    for (int i = 0; i < n; i++) {
		    int a = sc.nextInt();
		    if (bfLength > a) {
		    	stepSum += (bfLength - a);
		    } else {
		    	bfLength = a;
		    }
	    }
	    System.out.println(stepSum);
	}
} public class Main {
	public static void main(String[] args) {
	    Scanner sc = new Scanner(System.in);
	    int n = sc.nextInt();
	    int bfLength = 0;
	    long stepSum = 0;
	    for (int i = 0; i < n; i++) {
		    int a = sc.nextInt();
		    if (bfLength > a) {
		    	stepSum += (bfLength - a);
		    } else {
		    	bfLength = a;
		    }
	    }
	    System.out.println(stepSum);
	}
} Main
import java.util.Scanner;

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