import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

  public static void main(String[] args) throws Exception{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int n = Integer.parseInt(br.readLine());
    String[] data = br.readLine().split(" ");
    long[] a = new long[n];
    for(int i = 0; i < n; i++){
      a[i] = Long.parseLong(data[i]);
    }
    long answer = 0;
    long max = a[0];
    for(int i = 1; i < n; i++){
      max = Math.max(max, a[i]);
      answer += Math.abs(max - a[i]);
    }
    System.out.println(answer);
  }
} import java.io.BufferedReader; BufferedReader java.io import java.io.InputStreamReader; InputStreamReader java.io public class Main {

  public static void main(String[] args) throws Exception{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int n = Integer.parseInt(br.readLine());
    String[] data = br.readLine().split(" ");
    long[] a = new long[n];
    for(int i = 0; i < n; i++){
      a[i] = Long.parseLong(data[i]);
    }
    long answer = 0;
    long max = a[0];
    for(int i = 1; i < n; i++){
      max = Math.max(max, a[i]);
      answer += Math.abs(max - a[i]);
    }
    System.out.println(answer);
  }
} Main public static void main(String[] args) throws Exception{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int n = Integer.parseInt(br.readLine());
    String[] data = br.readLine().split(" ");
    long[] a = new long[n];
    for(int i = 0; i < n; i++){
      a[i] = Long.parseLong(data[i]);
    }
    long answer = 0;
    long max = a[0];
    for(int i = 1; i < n; i++){
      max = Math.max(max, a[i]);
      answer += Math.abs(max - a[i]);
    }
    System.out.println(answer);
  } main {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int n = Integer.parseInt(br.readLine());
    String[] data = br.readLine().split(" ");
    long[] a = new long[n];
    for(int i = 0; i < n; i++){
      a[i] = Long.parseLong(data[i]);
    }
    long answer = 0;
    long max = a[0];
    for(int i = 1; i < n; i++){
      max = Math.max(max, a[i]);
      answer += Math.abs(max - a[i]);
    }
    System.out.println(answer);
  } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) int n = Integer.parseInt(br.readLine()); n Integer.parseInt(br.readLine()) Integer.parseInt Integer br.readLine() br.readLine br String[] data = br.readLine().split(" "); data br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " long[] a = new long[n]; a new long[n] n for(int i = 0; i < n; i++){
      a[i] = Long.parseLong(data[i]);
    } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
      a[i] = Long.parseLong(data[i]);
    } {
      a[i] = Long.parseLong(data[i]);
    } a[i] = Long.parseLong(data[i]) a[i] a i Long.parseLong(data[i]) Long.parseLong Long data[i] data i long answer = 0; answer 0 long max = a[0]; max a[0] a 0 for(int i = 1; i < n; i++){
      max = Math.max(max, a[i]);
      answer += Math.abs(max - a[i]);
    } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
      max = Math.max(max, a[i]);
      answer += Math.abs(max - a[i]);
    } {
      max = Math.max(max, a[i]);
      answer += Math.abs(max - a[i]);
    } max = Math.max(max, a[i]) max Math.max(max, a[i]) Math.max Math max a[i] a i answer += Math.abs(max - a[i]) answer Math.abs(max - a[i]) Math.abs Math max - a[i] max a[i] a i System.out.println(answer) System.out.println System.out System System.out answer String[] args args public class Main {

  public static void main(String[] args) throws Exception{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int n = Integer.parseInt(br.readLine());
    String[] data = br.readLine().split(" ");
    long[] a = new long[n];
    for(int i = 0; i < n; i++){
      a[i] = Long.parseLong(data[i]);
    }
    long answer = 0;
    long max = a[0];
    for(int i = 1; i < n; i++){
      max = Math.max(max, a[i]);
      answer += Math.abs(max - a[i]);
    }
    System.out.println(answer);
  }
} public class Main {

  public static void main(String[] args) throws Exception{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int n = Integer.parseInt(br.readLine());
    String[] data = br.readLine().split(" ");
    long[] a = new long[n];
    for(int i = 0; i < n; i++){
      a[i] = Long.parseLong(data[i]);
    }
    long answer = 0;
    long max = a[0];
    for(int i = 1; i < n; i++){
      max = Math.max(max, a[i]);
      answer += Math.abs(max - a[i]);
    }
    System.out.println(answer);
  }
} Main
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

  public static void main(String[] args) throws Exception{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int n = Integer.parseInt(br.readLine());
    String[] data = br.readLine().split(" ");
    long[] a = new long[n];
    for(int i = 0; i < n; i++){
      a[i] = Long.parseLong(data[i]);
    }
    long answer = 0;
    long max = a[0];
    for(int i = 1; i < n; i++){
      max = Math.max(max, a[i]);
      answer += Math.abs(max - a[i]);
    }
    System.out.println(answer);
  }
}