import java.util.*;
public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
      	
      	long[] arr = new long[n];

      	for(int i = 0; i<n; i++){
          arr[i] = sc.nextInt();
        }
		long pre = 0;
      	long now = 0;
      	long sum = 0;
      	for(int i = 0; i<n; i++){
          if (i == 0){
            continue;
          }
          now = arr[i];
          pre = arr[i-1];
          if(now > pre){
            continue;
          }
          long step = pre - now;
          sum += step;
          arr[i] = pre;
        }
      System.out.println(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();
      	
      	long[] arr = new long[n];

      	for(int i = 0; i<n; i++){
          arr[i] = sc.nextInt();
        }
		long pre = 0;
      	long now = 0;
      	long sum = 0;
      	for(int i = 0; i<n; i++){
          if (i == 0){
            continue;
          }
          now = arr[i];
          pre = arr[i-1];
          if(now > pre){
            continue;
          }
          long step = pre - now;
          sum += step;
          arr[i] = pre;
        }
      System.out.println(sum);
    }
} Main public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
      	
      	long[] arr = new long[n];

      	for(int i = 0; i<n; i++){
          arr[i] = sc.nextInt();
        }
		long pre = 0;
      	long now = 0;
      	long sum = 0;
      	for(int i = 0; i<n; i++){
          if (i == 0){
            continue;
          }
          now = arr[i];
          pre = arr[i-1];
          if(now > pre){
            continue;
          }
          long step = pre - now;
          sum += step;
          arr[i] = pre;
        }
      System.out.println(sum);
    } main {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
      	
      	long[] arr = new long[n];

      	for(int i = 0; i<n; i++){
          arr[i] = sc.nextInt();
        }
		long pre = 0;
      	long now = 0;
      	long sum = 0;
      	for(int i = 0; i<n; i++){
          if (i == 0){
            continue;
          }
          now = arr[i];
          pre = arr[i-1];
          if(now > pre){
            continue;
          }
          long step = pre - now;
          sum += step;
          arr[i] = pre;
        }
      System.out.println(sum);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc long[] arr = new long[n]; arr new long[n] n for(int i = 0; i<n; i++){
          arr[i] = sc.nextInt();
        } int i = 0; int i = 0; i 0 i<n i n i++ i++ i {
          arr[i] = sc.nextInt();
        } {
          arr[i] = sc.nextInt();
        } arr[i] = sc.nextInt() arr[i] arr i sc.nextInt() sc.nextInt sc long pre = 0; pre 0 long now = 0; now 0 long sum = 0; sum 0 for(int i = 0; i<n; i++){
          if (i == 0){
            continue;
          }
          now = arr[i];
          pre = arr[i-1];
          if(now > pre){
            continue;
          }
          long step = pre - now;
          sum += step;
          arr[i] = pre;
        } int i = 0; int i = 0; i 0 i<n i n i++ i++ i {
          if (i == 0){
            continue;
          }
          now = arr[i];
          pre = arr[i-1];
          if(now > pre){
            continue;
          }
          long step = pre - now;
          sum += step;
          arr[i] = pre;
        } {
          if (i == 0){
            continue;
          }
          now = arr[i];
          pre = arr[i-1];
          if(now > pre){
            continue;
          }
          long step = pre - now;
          sum += step;
          arr[i] = pre;
        } if (i == 0){
            continue;
          } i == 0 i 0 {
            continue;
          } continue; now = arr[i] now arr[i] arr i pre = arr[i-1] pre arr[i-1] arr i-1 i 1 if(now > pre){
            continue;
          } now > pre now pre {
            continue;
          } continue; long step = pre - now; step pre - now pre now sum += step sum step arr[i] = pre arr[i] arr i pre System.out.println(sum) System.out.println 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();
      	
      	long[] arr = new long[n];

      	for(int i = 0; i<n; i++){
          arr[i] = sc.nextInt();
        }
		long pre = 0;
      	long now = 0;
      	long sum = 0;
      	for(int i = 0; i<n; i++){
          if (i == 0){
            continue;
          }
          now = arr[i];
          pre = arr[i-1];
          if(now > pre){
            continue;
          }
          long step = pre - now;
          sum += step;
          arr[i] = pre;
        }
      System.out.println(sum);
    }
} public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
      	
      	long[] arr = new long[n];

      	for(int i = 0; i<n; i++){
          arr[i] = sc.nextInt();
        }
		long pre = 0;
      	long now = 0;
      	long sum = 0;
      	for(int i = 0; i<n; i++){
          if (i == 0){
            continue;
          }
          now = arr[i];
          pre = arr[i-1];
          if(now > pre){
            continue;
          }
          long step = pre - now;
          sum += step;
          arr[i] = pre;
        }
      System.out.println(sum);
    }
} Main
import java.util.*;
public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
      	
      	long[] arr = new long[n];

      	for(int i = 0; i<n; i++){
          arr[i] = sc.nextInt();
        }
		long pre = 0;
      	long now = 0;
      	long sum = 0;
      	for(int i = 0; i<n; i++){
          if (i == 0){
            continue;
          }
          now = arr[i];
          pre = arr[i-1];
          if(now > pre){
            continue;
          }
          long step = pre - now;
          sum += step;
          arr[i] = pre;
        }
      System.out.println(sum);
    }
}