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

    long ans = 0;
    int max = a[1];
    for(int i = 2; i <= n; i++) {
      if(max > a[i]) {
        ans += (max-a[i]);
      }else{
        max = a[i];
      }
    }

    System.out.println(ans);
  }
} import java.util.Scanner; Scanner 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+1];
    a[0] = 1;
    for(int i = 1; i <= n; i++) {
      a[i] = sc.nextInt();
    }

    long ans = 0;
    int max = a[1];
    for(int i = 2; i <= n; i++) {
      if(max > a[i]) {
        ans += (max-a[i]);
      }else{
        max = a[i];
      }
    }

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

    long ans = 0;
    int max = a[1];
    for(int i = 2; i <= n; i++) {
      if(max > a[i]) {
        ans += (max-a[i]);
      }else{
        max = a[i];
      }
    }

    System.out.println(ans);
  } main {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int[] a = new int[n+1];
    a[0] = 1;
    for(int i = 1; i <= n; i++) {
      a[i] = sc.nextInt();
    }

    long ans = 0;
    int max = a[1];
    for(int i = 2; i <= n; i++) {
      if(max > a[i]) {
        ans += (max-a[i]);
      }else{
        max = a[i];
      }
    }

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

    long ans = 0;
    int max = a[1];
    for(int i = 2; i <= n; i++) {
      if(max > a[i]) {
        ans += (max-a[i]);
      }else{
        max = a[i];
      }
    }

    System.out.println(ans);
  }
}
