import java.util.*;

class Main{

  public static void main(String args[]){

    Scanner s=new Scanner(System.in);
    int N=s.nextInt();
    long ans=0;
    long max=0;
    for(int i=0;i<N;i++){
      long height=s.nextInt();
      if(height<=max) ans+=max-height;
      else max=height;
    }
    System.out.println(ans);
    
  }  
} import java.util.*; util.* java class Main{

  public static void main(String args[]){

    Scanner s=new Scanner(System.in);
    int N=s.nextInt();
    long ans=0;
    long max=0;
    for(int i=0;i<N;i++){
      long height=s.nextInt();
      if(height<=max) ans+=max-height;
      else max=height;
    }
    System.out.println(ans);
    
  }  
} Main public static void main(String args[]){

    Scanner s=new Scanner(System.in);
    int N=s.nextInt();
    long ans=0;
    long max=0;
    for(int i=0;i<N;i++){
      long height=s.nextInt();
      if(height<=max) ans+=max-height;
      else max=height;
    }
    System.out.println(ans);
    
  } main {

    Scanner s=new Scanner(System.in);
    int N=s.nextInt();
    long ans=0;
    long max=0;
    for(int i=0;i<N;i++){
      long height=s.nextInt();
      if(height<=max) ans+=max-height;
      else max=height;
    }
    System.out.println(ans);
    
  } Scanner s=new Scanner(System.in); s new Scanner(System.in) int N=s.nextInt(); N s.nextInt() s.nextInt s long ans=0; ans 0 long max=0; max 0 for(int i=0;i<N;i++){
      long height=s.nextInt();
      if(height<=max) ans+=max-height;
      else max=height;
    } int i=0; int i=0; i 0 i<N i N i++ i++ i {
      long height=s.nextInt();
      if(height<=max) ans+=max-height;
      else max=height;
    } {
      long height=s.nextInt();
      if(height<=max) ans+=max-height;
      else max=height;
    } long height=s.nextInt(); height s.nextInt() s.nextInt s if(height<=max) ans+=max-height;
      else max=height; height<=max height max ans+=max-height ans max-height max height max=height max height System.out.println(ans) System.out.println System.out System System.out ans String args[] args
import java.util.*;

class Main{

  public static void main(String args[]){

    Scanner s=new Scanner(System.in);
    int N=s.nextInt();
    long ans=0;
    long max=0;
    for(int i=0;i<N;i++){
      long height=s.nextInt();
      if(height<=max) ans+=max-height;
      else max=height;
    }
    System.out.println(ans);
    
  }  
}