
import java.io.InputStream;
import java.io.PrintStream;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Main {
  public static void main(String[] args) throws Exception {
    solve(System.in, System.out);
  }

  static void solve(InputStream is, PrintStream os) {
    // Your code here!
    Scanner scan = new Scanner(is);
    int N = scan.nextInt();
    int A[] = new int[N];
    int maxA[] = new int[N];

    int max = 0;

    for(int i = 0; i < N; i++) {
      A[i] = scan.nextInt();
      if(A[i] > max){
        max = A[i];
      }
      maxA[i] = max;
    }

    long ans = 0;

    for(int i = 0; i < N; i++) {
      ans += maxA[i] - A[i];
    }

    os.println(ans);
  }
} import java.io.InputStream; InputStream java.io import java.io.PrintStream; PrintStream java.io import java.math.BigDecimal; BigDecimal java.math import java.util.HashMap; HashMap java.util import java.util.HashSet; HashSet java.util import java.util.Scanner; Scanner java.util import java.util.Set; Set java.util public class Main {
  public static void main(String[] args) throws Exception {
    solve(System.in, System.out);
  }

  static void solve(InputStream is, PrintStream os) {
    // Your code here!
    Scanner scan = new Scanner(is);
    int N = scan.nextInt();
    int A[] = new int[N];
    int maxA[] = new int[N];

    int max = 0;

    for(int i = 0; i < N; i++) {
      A[i] = scan.nextInt();
      if(A[i] > max){
        max = A[i];
      }
      maxA[i] = max;
    }

    long ans = 0;

    for(int i = 0; i < N; i++) {
      ans += maxA[i] - A[i];
    }

    os.println(ans);
  }
} Main public static void main(String[] args) throws Exception {
    solve(System.in, System.out);
  } main {
    solve(System.in, System.out);
  } solve(System.in, System.out) solve System.in System System.in System.out System System.out String[] args args static void solve(InputStream is, PrintStream os) {
    // Your code here!
    Scanner scan = new Scanner(is);
    int N = scan.nextInt();
    int A[] = new int[N];
    int maxA[] = new int[N];

    int max = 0;

    for(int i = 0; i < N; i++) {
      A[i] = scan.nextInt();
      if(A[i] > max){
        max = A[i];
      }
      maxA[i] = max;
    }

    long ans = 0;

    for(int i = 0; i < N; i++) {
      ans += maxA[i] - A[i];
    }

    os.println(ans);
  } solve {
    // Your code here!
    Scanner scan = new Scanner(is);
    int N = scan.nextInt();
    int A[] = new int[N];
    int maxA[] = new int[N];

    int max = 0;

    for(int i = 0; i < N; i++) {
      A[i] = scan.nextInt();
      if(A[i] > max){
        max = A[i];
      }
      maxA[i] = max;
    }

    long ans = 0;

    for(int i = 0; i < N; i++) {
      ans += maxA[i] - A[i];
    }

    os.println(ans);
  } Scanner scan = new Scanner(is); scan new Scanner(is) int N = scan.nextInt(); N scan.nextInt() scan.nextInt scan int A[] = new int[N]; A new int[N] N int maxA[] = new int[N]; maxA new int[N] N int max = 0; max 0 for(int i = 0; i < N; i++) {
      A[i] = scan.nextInt();
      if(A[i] > max){
        max = A[i];
      }
      maxA[i] = max;
    } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
      A[i] = scan.nextInt();
      if(A[i] > max){
        max = A[i];
      }
      maxA[i] = max;
    } {
      A[i] = scan.nextInt();
      if(A[i] > max){
        max = A[i];
      }
      maxA[i] = max;
    } A[i] = scan.nextInt() A[i] A i scan.nextInt() scan.nextInt scan if(A[i] > max){
        max = A[i];
      } A[i] > max A[i] A i max {
        max = A[i];
      } max = A[i] max A[i] A i maxA[i] = max maxA[i] maxA i max long ans = 0; ans 0 for(int i = 0; i < N; i++) {
      ans += maxA[i] - A[i];
    } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
      ans += maxA[i] - A[i];
    } {
      ans += maxA[i] - A[i];
    } ans += maxA[i] - A[i] ans maxA[i] - A[i] maxA[i] maxA i A[i] A i os.println(ans) os.println os ans InputStream is is PrintStream os os public class Main {
  public static void main(String[] args) throws Exception {
    solve(System.in, System.out);
  }

  static void solve(InputStream is, PrintStream os) {
    // Your code here!
    Scanner scan = new Scanner(is);
    int N = scan.nextInt();
    int A[] = new int[N];
    int maxA[] = new int[N];

    int max = 0;

    for(int i = 0; i < N; i++) {
      A[i] = scan.nextInt();
      if(A[i] > max){
        max = A[i];
      }
      maxA[i] = max;
    }

    long ans = 0;

    for(int i = 0; i < N; i++) {
      ans += maxA[i] - A[i];
    }

    os.println(ans);
  }
} public class Main {
  public static void main(String[] args) throws Exception {
    solve(System.in, System.out);
  }

  static void solve(InputStream is, PrintStream os) {
    // Your code here!
    Scanner scan = new Scanner(is);
    int N = scan.nextInt();
    int A[] = new int[N];
    int maxA[] = new int[N];

    int max = 0;

    for(int i = 0; i < N; i++) {
      A[i] = scan.nextInt();
      if(A[i] > max){
        max = A[i];
      }
      maxA[i] = max;
    }

    long ans = 0;

    for(int i = 0; i < N; i++) {
      ans += maxA[i] - A[i];
    }

    os.println(ans);
  }
} Main

import java.io.InputStream;
import java.io.PrintStream;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Main {
  public static void main(String[] args) throws Exception {
    solve(System.in, System.out);
  }

  static void solve(InputStream is, PrintStream os) {
    // Your code here!
    Scanner scan = new Scanner(is);
    int N = scan.nextInt();
    int A[] = new int[N];
    int maxA[] = new int[N];

    int max = 0;

    for(int i = 0; i < N; i++) {
      A[i] = scan.nextInt();
      if(A[i] > max){
        max = A[i];
      }
      maxA[i] = max;
    }

    long ans = 0;

    for(int i = 0; i < N; i++) {
      ans += maxA[i] - A[i];
    }

    os.println(ans);
  }
}
