//package beginner176;

import java.util.Scanner;

public class Main {


    public static void main(String[] args){
        Scanner in = new Scanner(System.in);

        int N = in.nextInt();
        int max = 0;

        long result = 0;
        for(int i = 0; i < N; i++){
            int x = in.nextInt();
            if(x < max){
                result += max - x;
            }
            max = Math.max(max, x);
        }
        System.out.println(result);
    }
} import java.util.Scanner; Scanner java.util public class Main {


    public static void main(String[] args){
        Scanner in = new Scanner(System.in);

        int N = in.nextInt();
        int max = 0;

        long result = 0;
        for(int i = 0; i < N; i++){
            int x = in.nextInt();
            if(x < max){
                result += max - x;
            }
            max = Math.max(max, x);
        }
        System.out.println(result);
    }
} Main public static void main(String[] args){
        Scanner in = new Scanner(System.in);

        int N = in.nextInt();
        int max = 0;

        long result = 0;
        for(int i = 0; i < N; i++){
            int x = in.nextInt();
            if(x < max){
                result += max - x;
            }
            max = Math.max(max, x);
        }
        System.out.println(result);
    } main {
        Scanner in = new Scanner(System.in);

        int N = in.nextInt();
        int max = 0;

        long result = 0;
        for(int i = 0; i < N; i++){
            int x = in.nextInt();
            if(x < max){
                result += max - x;
            }
            max = Math.max(max, x);
        }
        System.out.println(result);
    } Scanner in = new Scanner(System.in); in new Scanner(System.in) int N = in.nextInt(); N in.nextInt() in.nextInt in int max = 0; max 0 long result = 0; result 0 for(int i = 0; i < N; i++){
            int x = in.nextInt();
            if(x < max){
                result += max - x;
            }
            max = Math.max(max, x);
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            int x = in.nextInt();
            if(x < max){
                result += max - x;
            }
            max = Math.max(max, x);
        } {
            int x = in.nextInt();
            if(x < max){
                result += max - x;
            }
            max = Math.max(max, x);
        } int x = in.nextInt(); x in.nextInt() in.nextInt in if(x < max){
                result += max - x;
            } x < max x max {
                result += max - x;
            } result += max - x result max - x max x max = Math.max(max, x) max Math.max(max, x) Math.max Math max x System.out.println(result) System.out.println System.out System System.out result String[] args args public class Main {


    public static void main(String[] args){
        Scanner in = new Scanner(System.in);

        int N = in.nextInt();
        int max = 0;

        long result = 0;
        for(int i = 0; i < N; i++){
            int x = in.nextInt();
            if(x < max){
                result += max - x;
            }
            max = Math.max(max, x);
        }
        System.out.println(result);
    }
} public class Main {


    public static void main(String[] args){
        Scanner in = new Scanner(System.in);

        int N = in.nextInt();
        int max = 0;

        long result = 0;
        for(int i = 0; i < N; i++){
            int x = in.nextInt();
            if(x < max){
                result += max - x;
            }
            max = Math.max(max, x);
        }
        System.out.println(result);
    }
} Main
//package beginner176;

import java.util.Scanner;

public class Main {


    public static void main(String[] args){
        Scanner in = new Scanner(System.in);

        int N = in.nextInt();
        int max = 0;

        long result = 0;
        for(int i = 0; i < N; i++){
            int x = in.nextInt();
            if(x < max){
                result += max - x;
            }
            max = Math.max(max, x);
        }
        System.out.println(result);
    }
}
