import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
      int N = sc.nextInt();
      int[] x = new int[N];
        for(int i=0; i<N; i++) {
          int A = sc.nextInt();
          x[i] = A;
        }
        long plus = 0;
        for(int i=1; i<N; i++) {
        if(x[i-1] > x[i]) {
          plus+=x[i-1]-x[i];
          //x[i]+=x[i-1]-x[i];
          x[i]=x[i-1];
        } else {
          continue;
        }
        }
      System.out.println(plus);
    }
  } import java.util.Scanner; Scanner java.util public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
      int N = sc.nextInt();
      int[] x = new int[N];
        for(int i=0; i<N; i++) {
          int A = sc.nextInt();
          x[i] = A;
        }
        long plus = 0;
        for(int i=1; i<N; i++) {
        if(x[i-1] > x[i]) {
          plus+=x[i-1]-x[i];
          //x[i]+=x[i-1]-x[i];
          x[i]=x[i-1];
        } else {
          continue;
        }
        }
      System.out.println(plus);
    }
  } Main public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
      int N = sc.nextInt();
      int[] x = new int[N];
        for(int i=0; i<N; i++) {
          int A = sc.nextInt();
          x[i] = A;
        }
        long plus = 0;
        for(int i=1; i<N; i++) {
        if(x[i-1] > x[i]) {
          plus+=x[i-1]-x[i];
          //x[i]+=x[i-1]-x[i];
          x[i]=x[i-1];
        } else {
          continue;
        }
        }
      System.out.println(plus);
    } main {
Scanner sc = new Scanner(System.in);
      int N = sc.nextInt();
      int[] x = new int[N];
        for(int i=0; i<N; i++) {
          int A = sc.nextInt();
          x[i] = A;
        }
        long plus = 0;
        for(int i=1; i<N; i++) {
        if(x[i-1] > x[i]) {
          plus+=x[i-1]-x[i];
          //x[i]+=x[i-1]-x[i];
          x[i]=x[i-1];
        } else {
          continue;
        }
        }
      System.out.println(plus);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc int[] x = new int[N]; x new int[N] N for(int i=0; i<N; i++) {
          int A = sc.nextInt();
          x[i] = A;
        } int i=0; int i=0; i 0 i<N i N i++ i++ i {
          int A = sc.nextInt();
          x[i] = A;
        } {
          int A = sc.nextInt();
          x[i] = A;
        } int A = sc.nextInt(); A sc.nextInt() sc.nextInt sc x[i] = A x[i] x i A long plus = 0; plus 0 for(int i=1; i<N; i++) {
        if(x[i-1] > x[i]) {
          plus+=x[i-1]-x[i];
          //x[i]+=x[i-1]-x[i];
          x[i]=x[i-1];
        } else {
          continue;
        }
        } int i=1; int i=1; i 1 i<N i N i++ i++ i {
        if(x[i-1] > x[i]) {
          plus+=x[i-1]-x[i];
          //x[i]+=x[i-1]-x[i];
          x[i]=x[i-1];
        } else {
          continue;
        }
        } {
        if(x[i-1] > x[i]) {
          plus+=x[i-1]-x[i];
          //x[i]+=x[i-1]-x[i];
          x[i]=x[i-1];
        } else {
          continue;
        }
        } if(x[i-1] > x[i]) {
          plus+=x[i-1]-x[i];
          //x[i]+=x[i-1]-x[i];
          x[i]=x[i-1];
        } else {
          continue;
        } x[i-1] > x[i] x[i-1] x i-1 i 1 x[i] x i {
          plus+=x[i-1]-x[i];
          //x[i]+=x[i-1]-x[i];
          x[i]=x[i-1];
        } plus+=x[i-1]-x[i] plus x[i-1]-x[i] x[i-1] x i-1 i 1 x[i] x i x[i]=x[i-1] x[i] x i x[i-1] x i-1 i 1 {
          continue;
        } continue; System.out.println(plus) System.out.println System.out System System.out plus String[] args args public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
      int N = sc.nextInt();
      int[] x = new int[N];
        for(int i=0; i<N; i++) {
          int A = sc.nextInt();
          x[i] = A;
        }
        long plus = 0;
        for(int i=1; i<N; i++) {
        if(x[i-1] > x[i]) {
          plus+=x[i-1]-x[i];
          //x[i]+=x[i-1]-x[i];
          x[i]=x[i-1];
        } else {
          continue;
        }
        }
      System.out.println(plus);
    }
  } public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
      int N = sc.nextInt();
      int[] x = new int[N];
        for(int i=0; i<N; i++) {
          int A = sc.nextInt();
          x[i] = A;
        }
        long plus = 0;
        for(int i=1; i<N; i++) {
        if(x[i-1] > x[i]) {
          plus+=x[i-1]-x[i];
          //x[i]+=x[i-1]-x[i];
          x[i]=x[i-1];
        } else {
          continue;
        }
        }
      System.out.println(plus);
    }
  } Main
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
      int N = sc.nextInt();
      int[] x = new int[N];
        for(int i=0; i<N; i++) {
          int A = sc.nextInt();
          x[i] = A;
        }
        long plus = 0;
        for(int i=1; i<N; i++) {
        if(x[i-1] > x[i]) {
          plus+=x[i-1]-x[i];
          //x[i]+=x[i-1]-x[i];
          x[i]=x[i-1];
        } else {
          continue;
        }
        }
      System.out.println(plus);
    }
  }