import java.util.*;

public class Main{
public static void main(String[] args){
	Scanner scanner = new Scanner(System.in);
  	int n = scanner.nextInt();
  	long[] nums = new long[n];
  	for(int i = 0;i < n;i++){
      nums[i] = scanner.nextLong();
    }
  	if(n == 1){
      System.out.println(0);
      return;
    }
  	int l = 0;
  	int r = 1;
  	long sum = 0;
  	while(r < n){
      if(nums[l] > nums[r]){
         sum += Math.abs(nums[l] - nums[r]);
        nums[r] = nums[l];
      }
      l++;
      r++;
    }
      System.out.println(sum);
}
} import java.util.*; util.* java public class Main{
public static void main(String[] args){
	Scanner scanner = new Scanner(System.in);
  	int n = scanner.nextInt();
  	long[] nums = new long[n];
  	for(int i = 0;i < n;i++){
      nums[i] = scanner.nextLong();
    }
  	if(n == 1){
      System.out.println(0);
      return;
    }
  	int l = 0;
  	int r = 1;
  	long sum = 0;
  	while(r < n){
      if(nums[l] > nums[r]){
         sum += Math.abs(nums[l] - nums[r]);
        nums[r] = nums[l];
      }
      l++;
      r++;
    }
      System.out.println(sum);
}
} Main public static void main(String[] args){
	Scanner scanner = new Scanner(System.in);
  	int n = scanner.nextInt();
  	long[] nums = new long[n];
  	for(int i = 0;i < n;i++){
      nums[i] = scanner.nextLong();
    }
  	if(n == 1){
      System.out.println(0);
      return;
    }
  	int l = 0;
  	int r = 1;
  	long sum = 0;
  	while(r < n){
      if(nums[l] > nums[r]){
         sum += Math.abs(nums[l] - nums[r]);
        nums[r] = nums[l];
      }
      l++;
      r++;
    }
      System.out.println(sum);
} main {
	Scanner scanner = new Scanner(System.in);
  	int n = scanner.nextInt();
  	long[] nums = new long[n];
  	for(int i = 0;i < n;i++){
      nums[i] = scanner.nextLong();
    }
  	if(n == 1){
      System.out.println(0);
      return;
    }
  	int l = 0;
  	int r = 1;
  	long sum = 0;
  	while(r < n){
      if(nums[l] > nums[r]){
         sum += Math.abs(nums[l] - nums[r]);
        nums[r] = nums[l];
      }
      l++;
      r++;
    }
      System.out.println(sum);
} Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int n = scanner.nextInt(); n scanner.nextInt() scanner.nextInt scanner long[] nums = new long[n]; nums new long[n] n for(int i = 0;i < n;i++){
      nums[i] = scanner.nextLong();
    } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
      nums[i] = scanner.nextLong();
    } {
      nums[i] = scanner.nextLong();
    } nums[i] = scanner.nextLong() nums[i] nums i scanner.nextLong() scanner.nextLong scanner if(n == 1){
      System.out.println(0);
      return;
    } n == 1 n 1 {
      System.out.println(0);
      return;
    } System.out.println(0) System.out.println System.out System System.out 0 return; int l = 0; l 0 int r = 1; r 1 long sum = 0; sum 0 while(r < n){
      if(nums[l] > nums[r]){
         sum += Math.abs(nums[l] - nums[r]);
        nums[r] = nums[l];
      }
      l++;
      r++;
    } r < n r n {
      if(nums[l] > nums[r]){
         sum += Math.abs(nums[l] - nums[r]);
        nums[r] = nums[l];
      }
      l++;
      r++;
    } if(nums[l] > nums[r]){
         sum += Math.abs(nums[l] - nums[r]);
        nums[r] = nums[l];
      } nums[l] > nums[r] nums[l] nums l nums[r] nums r {
         sum += Math.abs(nums[l] - nums[r]);
        nums[r] = nums[l];
      } sum += Math.abs(nums[l] - nums[r]) sum Math.abs(nums[l] - nums[r]) Math.abs Math nums[l] - nums[r] nums[l] nums l nums[r] nums r nums[r] = nums[l] nums[r] nums r nums[l] nums l l++ l r++ r 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 scanner = new Scanner(System.in);
  	int n = scanner.nextInt();
  	long[] nums = new long[n];
  	for(int i = 0;i < n;i++){
      nums[i] = scanner.nextLong();
    }
  	if(n == 1){
      System.out.println(0);
      return;
    }
  	int l = 0;
  	int r = 1;
  	long sum = 0;
  	while(r < n){
      if(nums[l] > nums[r]){
         sum += Math.abs(nums[l] - nums[r]);
        nums[r] = nums[l];
      }
      l++;
      r++;
    }
      System.out.println(sum);
}
} public class Main{
public static void main(String[] args){
	Scanner scanner = new Scanner(System.in);
  	int n = scanner.nextInt();
  	long[] nums = new long[n];
  	for(int i = 0;i < n;i++){
      nums[i] = scanner.nextLong();
    }
  	if(n == 1){
      System.out.println(0);
      return;
    }
  	int l = 0;
  	int r = 1;
  	long sum = 0;
  	while(r < n){
      if(nums[l] > nums[r]){
         sum += Math.abs(nums[l] - nums[r]);
        nums[r] = nums[l];
      }
      l++;
      r++;
    }
      System.out.println(sum);
}
} Main
import java.util.*;

public class Main{
public static void main(String[] args){
	Scanner scanner = new Scanner(System.in);
  	int n = scanner.nextInt();
  	long[] nums = new long[n];
  	for(int i = 0;i < n;i++){
      nums[i] = scanner.nextLong();
    }
  	if(n == 1){
      System.out.println(0);
      return;
    }
  	int l = 0;
  	int r = 1;
  	long sum = 0;
  	while(r < n){
      if(nums[l] > nums[r]){
         sum += Math.abs(nums[l] - nums[r]);
        nums[r] = nums[l];
      }
      l++;
      r++;
    }
      System.out.println(sum);
}
}