import java.util.*;
class Main{

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

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

  public static void main(String args[]){
    Scanner sc=new Scanner(System.in);
   int n=sc.nextInt();
    long a=0;
    long arr[]=new long[n];
    for(int i=0;i<n;i++){
      arr[i]=sc.nextInt();
    }
    for(int i=1;i<n;i++){
     if(arr[i]<arr[i-1]){
       long k=arr[i-1]-arr[i];
       a=a+k;
       arr[i]=arr[i]+k;
     }
    }
    System.out.println(a);
  }}
  
