import java.io.BufferedReader;
import java.io.*;
import java.util.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Collections;
import java.util.Arrays;

public class Main {
	public static void main(final String[] args){
    
    final Scanner sc = new Scanner(System.in);
    final List<Long> list = new ArrayList<>();
    //final List<Long> list2 = new ArrayList<>();

    int n = sc.nextInt();
    for(int i = 0; i < n ; i++){
        list.add(sc.nextLong());
    }

    long max = list.get(0);
    long sum = 0;

    for(int i = 1; i < n ; i++){
        if(max > list.get(i)){
            sum += max - list.get(i);
        }else{
            max = list.get(i);
        }

        //System.out.println(sum + " " + max);
    }
    System.out.println(sum);

    

    





}
} import java.io.BufferedReader; BufferedReader java.io import java.io.*; io.* java import java.util.*; util.* java import java.util.ArrayList; ArrayList java.util import java.util.Iterator; Iterator java.util import java.util.List; List java.util import java.util.Collections; Collections java.util import java.util.Arrays; Arrays java.util public class Main {
	public static void main(final String[] args){
    
    final Scanner sc = new Scanner(System.in);
    final List<Long> list = new ArrayList<>();
    //final List<Long> list2 = new ArrayList<>();

    int n = sc.nextInt();
    for(int i = 0; i < n ; i++){
        list.add(sc.nextLong());
    }

    long max = list.get(0);
    long sum = 0;

    for(int i = 1; i < n ; i++){
        if(max > list.get(i)){
            sum += max - list.get(i);
        }else{
            max = list.get(i);
        }

        //System.out.println(sum + " " + max);
    }
    System.out.println(sum);

    

    





}
} Main public static void main(final String[] args){
    
    final Scanner sc = new Scanner(System.in);
    final List<Long> list = new ArrayList<>();
    //final List<Long> list2 = new ArrayList<>();

    int n = sc.nextInt();
    for(int i = 0; i < n ; i++){
        list.add(sc.nextLong());
    }

    long max = list.get(0);
    long sum = 0;

    for(int i = 1; i < n ; i++){
        if(max > list.get(i)){
            sum += max - list.get(i);
        }else{
            max = list.get(i);
        }

        //System.out.println(sum + " " + max);
    }
    System.out.println(sum);

    

    





} main {
    
    final Scanner sc = new Scanner(System.in);
    final List<Long> list = new ArrayList<>();
    //final List<Long> list2 = new ArrayList<>();

    int n = sc.nextInt();
    for(int i = 0; i < n ; i++){
        list.add(sc.nextLong());
    }

    long max = list.get(0);
    long sum = 0;

    for(int i = 1; i < n ; i++){
        if(max > list.get(i)){
            sum += max - list.get(i);
        }else{
            max = list.get(i);
        }

        //System.out.println(sum + " " + max);
    }
    System.out.println(sum);

    

    





} final Scanner sc = new Scanner(System.in); sc new Scanner(System.in) final List<Long> list = new ArrayList<>(); list new ArrayList<>() int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc for(int i = 0; i < n ; i++){
        list.add(sc.nextLong());
    } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
        list.add(sc.nextLong());
    } {
        list.add(sc.nextLong());
    } list.add(sc.nextLong()) list.add list sc.nextLong() sc.nextLong sc long max = list.get(0); max list.get(0) list.get list 0 long sum = 0; sum 0 for(int i = 1; i < n ; i++){
        if(max > list.get(i)){
            sum += max - list.get(i);
        }else{
            max = list.get(i);
        }

        //System.out.println(sum + " " + max);
    } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
        if(max > list.get(i)){
            sum += max - list.get(i);
        }else{
            max = list.get(i);
        }

        //System.out.println(sum + " " + max);
    } {
        if(max > list.get(i)){
            sum += max - list.get(i);
        }else{
            max = list.get(i);
        }

        //System.out.println(sum + " " + max);
    } if(max > list.get(i)){
            sum += max - list.get(i);
        }else{
            max = list.get(i);
        } max > list.get(i) max list.get(i) list.get list i {
            sum += max - list.get(i);
        } sum += max - list.get(i) sum max - list.get(i) max list.get(i) list.get list i {
            max = list.get(i);
        } max = list.get(i) max list.get(i) list.get list i System.out.println(sum) System.out.println System.out System System.out sum final String[] args args public class Main {
	public static void main(final String[] args){
    
    final Scanner sc = new Scanner(System.in);
    final List<Long> list = new ArrayList<>();
    //final List<Long> list2 = new ArrayList<>();

    int n = sc.nextInt();
    for(int i = 0; i < n ; i++){
        list.add(sc.nextLong());
    }

    long max = list.get(0);
    long sum = 0;

    for(int i = 1; i < n ; i++){
        if(max > list.get(i)){
            sum += max - list.get(i);
        }else{
            max = list.get(i);
        }

        //System.out.println(sum + " " + max);
    }
    System.out.println(sum);

    

    





}
} public class Main {
	public static void main(final String[] args){
    
    final Scanner sc = new Scanner(System.in);
    final List<Long> list = new ArrayList<>();
    //final List<Long> list2 = new ArrayList<>();

    int n = sc.nextInt();
    for(int i = 0; i < n ; i++){
        list.add(sc.nextLong());
    }

    long max = list.get(0);
    long sum = 0;

    for(int i = 1; i < n ; i++){
        if(max > list.get(i)){
            sum += max - list.get(i);
        }else{
            max = list.get(i);
        }

        //System.out.println(sum + " " + max);
    }
    System.out.println(sum);

    

    





}
} Main
import java.io.BufferedReader;
import java.io.*;
import java.util.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Collections;
import java.util.Arrays;

public class Main {
	public static void main(final String[] args){
    
    final Scanner sc = new Scanner(System.in);
    final List<Long> list = new ArrayList<>();
    //final List<Long> list2 = new ArrayList<>();

    int n = sc.nextInt();
    for(int i = 0; i < n ; i++){
        list.add(sc.nextLong());
    }

    long max = list.get(0);
    long sum = 0;

    for(int i = 1; i < n ; i++){
        if(max > list.get(i)){
            sum += max - list.get(i);
        }else{
            max = list.get(i);
        }

        //System.out.println(sum + " " + max);
    }
    System.out.println(sum);

    

    





}
}