
import java.util.Scanner;
import java.util.Comparator;
import java.util.Arrays;
import java.math.BigInteger;
class Main{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    int N = scan.nextInt();
    long A[] = new long[N];
    long add = 0;
    A[0] = scan.nextLong();
    for(int i = 1; i < N; i++){
      A[i] = scan.nextLong();
      if(A[i] < A[i-1]){
        add += A[i-1] -A[i];
        A[i] = A[i-1];
      }
    }
    System.out.println(add);
  }
} import java.util.Scanner; Scanner java.util import java.util.Comparator; Comparator java.util import java.util.Arrays; Arrays java.util import java.math.BigInteger; BigInteger java.math class Main{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    int N = scan.nextInt();
    long A[] = new long[N];
    long add = 0;
    A[0] = scan.nextLong();
    for(int i = 1; i < N; i++){
      A[i] = scan.nextLong();
      if(A[i] < A[i-1]){
        add += A[i-1] -A[i];
        A[i] = A[i-1];
      }
    }
    System.out.println(add);
  }
} Main public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    int N = scan.nextInt();
    long A[] = new long[N];
    long add = 0;
    A[0] = scan.nextLong();
    for(int i = 1; i < N; i++){
      A[i] = scan.nextLong();
      if(A[i] < A[i-1]){
        add += A[i-1] -A[i];
        A[i] = A[i-1];
      }
    }
    System.out.println(add);
  } main {
    Scanner scan = new Scanner(System.in);
    int N = scan.nextInt();
    long A[] = new long[N];
    long add = 0;
    A[0] = scan.nextLong();
    for(int i = 1; i < N; i++){
      A[i] = scan.nextLong();
      if(A[i] < A[i-1]){
        add += A[i-1] -A[i];
        A[i] = A[i-1];
      }
    }
    System.out.println(add);
  } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int N = scan.nextInt(); N scan.nextInt() scan.nextInt scan long A[] = new long[N]; A new long[N] N long add = 0; add 0 A[0] = scan.nextLong() A[0] A 0 scan.nextLong() scan.nextLong scan for(int i = 1; i < N; i++){
      A[i] = scan.nextLong();
      if(A[i] < A[i-1]){
        add += A[i-1] -A[i];
        A[i] = A[i-1];
      }
    } int i = 1; int i = 1; i 1 i < N i N i++ i++ i {
      A[i] = scan.nextLong();
      if(A[i] < A[i-1]){
        add += A[i-1] -A[i];
        A[i] = A[i-1];
      }
    } {
      A[i] = scan.nextLong();
      if(A[i] < A[i-1]){
        add += A[i-1] -A[i];
        A[i] = A[i-1];
      }
    } A[i] = scan.nextLong() A[i] A i scan.nextLong() scan.nextLong scan if(A[i] < A[i-1]){
        add += A[i-1] -A[i];
        A[i] = A[i-1];
      } A[i] < A[i-1] A[i] A i A[i-1] A i-1 i 1 {
        add += A[i-1] -A[i];
        A[i] = A[i-1];
      } add += A[i-1] -A[i] add A[i-1] -A[i] A[i-1] A i-1 i 1 A[i] A i A[i] = A[i-1] A[i] A i A[i-1] A i-1 i 1 System.out.println(add) System.out.println System.out System System.out add String[] args args

import java.util.Scanner;
import java.util.Comparator;
import java.util.Arrays;
import java.math.BigInteger;
class Main{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    int N = scan.nextInt();
    long A[] = new long[N];
    long add = 0;
    A[0] = scan.nextLong();
    for(int i = 1; i < N; i++){
      A[i] = scan.nextLong();
      if(A[i] < A[i-1]){
        add += A[i-1] -A[i];
        A[i] = A[i-1];
      }
    }
    System.out.println(add);
  }
}