import java.util.*;

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();
    }
    long sum = 0;
    int Kijun = 0;
    for(int i2=0;i2<N-1;i2++){
      Kijun = A[i2];
      if(A[i2+1]<Kijun){
        sum+=(long)(Kijun-A[i2+1]);
        A[i2+1]=Kijun;
      }
    }
    System.out.println(sum);
  }
} import java.util.*; util.* java 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();
    }
    long sum = 0;
    int Kijun = 0;
    for(int i2=0;i2<N-1;i2++){
      Kijun = A[i2];
      if(A[i2+1]<Kijun){
        sum+=(long)(Kijun-A[i2+1]);
        A[i2+1]=Kijun;
      }
    }
    System.out.println(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();
    }
    long sum = 0;
    int Kijun = 0;
    for(int i2=0;i2<N-1;i2++){
      Kijun = A[i2];
      if(A[i2+1]<Kijun){
        sum+=(long)(Kijun-A[i2+1]);
        A[i2+1]=Kijun;
      }
    }
    System.out.println(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();
    }
    long sum = 0;
    int Kijun = 0;
    for(int i2=0;i2<N-1;i2++){
      Kijun = A[i2];
      if(A[i2+1]<Kijun){
        sum+=(long)(Kijun-A[i2+1]);
        A[i2+1]=Kijun;
      }
    }
    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 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]=sc.nextInt() A[i] A i sc.nextInt() sc.nextInt sc long sum = 0; sum 0 int Kijun = 0; Kijun 0 for(int i2=0;i2<N-1;i2++){
      Kijun = A[i2];
      if(A[i2+1]<Kijun){
        sum+=(long)(Kijun-A[i2+1]);
        A[i2+1]=Kijun;
      }
    } int i2=0; int i2=0; i2 0 i2<N-1 i2 N-1 N 1 i2++ i2++ i2 {
      Kijun = A[i2];
      if(A[i2+1]<Kijun){
        sum+=(long)(Kijun-A[i2+1]);
        A[i2+1]=Kijun;
      }
    } {
      Kijun = A[i2];
      if(A[i2+1]<Kijun){
        sum+=(long)(Kijun-A[i2+1]);
        A[i2+1]=Kijun;
      }
    } Kijun = A[i2] Kijun A[i2] A i2 if(A[i2+1]<Kijun){
        sum+=(long)(Kijun-A[i2+1]);
        A[i2+1]=Kijun;
      } A[i2+1]<Kijun A[i2+1] A i2+1 i2 1 Kijun {
        sum+=(long)(Kijun-A[i2+1]);
        A[i2+1]=Kijun;
      } sum+=(long)(Kijun-A[i2+1]) sum (long)(Kijun-A[i2+1]) Kijun A[i2+1] A i2+1 i2 1 A[i2+1]=Kijun A[i2+1] A i2+1 i2 1 Kijun System.out.println(sum) System.out.println System.out System System.out sum String[] args args
import java.util.*;

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();
    }
    long sum = 0;
    int Kijun = 0;
    for(int i2=0;i2<N-1;i2++){
      Kijun = A[i2];
      if(A[i2+1]<Kijun){
        sum+=(long)(Kijun-A[i2+1]);
        A[i2+1]=Kijun;
      }
    }
    System.out.println(sum);
  }
}