import java.util.*;
public class Main{
  public static void main(String[] args){
    Scanner sc =new Scanner(System.in);
    int N =sc.nextInt();
    long A[] =new long[N];
    long ans =0;
    long t =0;
    for(int i=0;i<N;i++){
      A[i]=sc.nextLong();
      t=Math.max(t,A[i]);
      ans =ans +t-A[i];}
    System.out.println(ans);    
  }
} 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 A[] =new long[N];
    long ans =0;
    long t =0;
    for(int i=0;i<N;i++){
      A[i]=sc.nextLong();
      t=Math.max(t,A[i]);
      ans =ans +t-A[i];}
    System.out.println(ans);    
  }
} Main public static void main(String[] args){
    Scanner sc =new Scanner(System.in);
    int N =sc.nextInt();
    long A[] =new long[N];
    long ans =0;
    long t =0;
    for(int i=0;i<N;i++){
      A[i]=sc.nextLong();
      t=Math.max(t,A[i]);
      ans =ans +t-A[i];}
    System.out.println(ans);    
  } main {
    Scanner sc =new Scanner(System.in);
    int N =sc.nextInt();
    long A[] =new long[N];
    long ans =0;
    long t =0;
    for(int i=0;i<N;i++){
      A[i]=sc.nextLong();
      t=Math.max(t,A[i]);
      ans =ans +t-A[i];}
    System.out.println(ans);    
  } Scanner sc =new Scanner(System.in); sc new Scanner(System.in) int N =sc.nextInt(); N sc.nextInt() sc.nextInt sc long A[] =new long[N]; A new long[N] N long ans =0; ans 0 long t =0; t 0 for(int i=0;i<N;i++){
      A[i]=sc.nextLong();
      t=Math.max(t,A[i]);
      ans =ans +t-A[i];} int i=0; int i=0; i 0 i<N i N i++ i++ i {
      A[i]=sc.nextLong();
      t=Math.max(t,A[i]);
      ans =ans +t-A[i];} {
      A[i]=sc.nextLong();
      t=Math.max(t,A[i]);
      ans =ans +t-A[i];} A[i]=sc.nextLong() A[i] A i sc.nextLong() sc.nextLong sc t=Math.max(t,A[i]) t Math.max(t,A[i]) Math.max Math t A[i] A i ans =ans +t-A[i] ans ans +t-A[i] ans +t ans t A[i] A i System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main{
  public static void main(String[] args){
    Scanner sc =new Scanner(System.in);
    int N =sc.nextInt();
    long A[] =new long[N];
    long ans =0;
    long t =0;
    for(int i=0;i<N;i++){
      A[i]=sc.nextLong();
      t=Math.max(t,A[i]);
      ans =ans +t-A[i];}
    System.out.println(ans);    
  }
} public class Main{
  public static void main(String[] args){
    Scanner sc =new Scanner(System.in);
    int N =sc.nextInt();
    long A[] =new long[N];
    long ans =0;
    long t =0;
    for(int i=0;i<N;i++){
      A[i]=sc.nextLong();
      t=Math.max(t,A[i]);
      ans =ans +t-A[i];}
    System.out.println(ans);    
  }
} Main
import java.util.*;
public class Main{
  public static void main(String[] args){
    Scanner sc =new Scanner(System.in);
    int N =sc.nextInt();
    long A[] =new long[N];
    long ans =0;
    long t =0;
    for(int i=0;i<N;i++){
      A[i]=sc.nextLong();
      t=Math.max(t,A[i]);
      ans =ans +t-A[i];}
    System.out.println(ans);    
  }
}