import java.util.*;
import java.io.*;
public class Main {
    public static void main(String[] args) throws Exception{
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        in.nextToken();
        int N = (int) in.nval;
        int[] a = new int[N];
        for(int i = 0; i < N; i++){
            in.nextToken();
            a[i] = (int) in.nval;
        }
        int j = 0;
        double ans = 0;
        for(int i = 1; i < N; i++){
            if(a[i] < a[j]){
                ans += (a[j] - a[i]);
            }else{
                j = i;
            }
        }
        System.out.format("%.0f",ans);
        System.out.println();
    }
} import java.util.*; util.* java import java.io.*; io.* java public class Main {
    public static void main(String[] args) throws Exception{
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        in.nextToken();
        int N = (int) in.nval;
        int[] a = new int[N];
        for(int i = 0; i < N; i++){
            in.nextToken();
            a[i] = (int) in.nval;
        }
        int j = 0;
        double ans = 0;
        for(int i = 1; i < N; i++){
            if(a[i] < a[j]){
                ans += (a[j] - a[i]);
            }else{
                j = i;
            }
        }
        System.out.format("%.0f",ans);
        System.out.println();
    }
} Main public static void main(String[] args) throws Exception{
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        in.nextToken();
        int N = (int) in.nval;
        int[] a = new int[N];
        for(int i = 0; i < N; i++){
            in.nextToken();
            a[i] = (int) in.nval;
        }
        int j = 0;
        double ans = 0;
        for(int i = 1; i < N; i++){
            if(a[i] < a[j]){
                ans += (a[j] - a[i]);
            }else{
                j = i;
            }
        }
        System.out.format("%.0f",ans);
        System.out.println();
    } main {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        in.nextToken();
        int N = (int) in.nval;
        int[] a = new int[N];
        for(int i = 0; i < N; i++){
            in.nextToken();
            a[i] = (int) in.nval;
        }
        int j = 0;
        double ans = 0;
        for(int i = 1; i < N; i++){
            if(a[i] < a[j]){
                ans += (a[j] - a[i]);
            }else{
                j = i;
            }
        }
        System.out.format("%.0f",ans);
        System.out.println();
    } StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); in new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))) in.nextToken() in.nextToken in int N = (int) in.nval; N (int) in.nval in in.nval int[] a = new int[N]; a new int[N] N for(int i = 0; i < N; i++){
            in.nextToken();
            a[i] = (int) in.nval;
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            in.nextToken();
            a[i] = (int) in.nval;
        } {
            in.nextToken();
            a[i] = (int) in.nval;
        } in.nextToken() in.nextToken in a[i] = (int) in.nval a[i] a i (int) in.nval in in.nval int j = 0; j 0 double ans = 0; ans 0 for(int i = 1; i < N; i++){
            if(a[i] < a[j]){
                ans += (a[j] - a[i]);
            }else{
                j = i;
            }
        } int i = 1; int i = 1; i 1 i < N i N i++ i++ i {
            if(a[i] < a[j]){
                ans += (a[j] - a[i]);
            }else{
                j = i;
            }
        } {
            if(a[i] < a[j]){
                ans += (a[j] - a[i]);
            }else{
                j = i;
            }
        } if(a[i] < a[j]){
                ans += (a[j] - a[i]);
            }else{
                j = i;
            } a[i] < a[j] a[i] a i a[j] a j {
                ans += (a[j] - a[i]);
            } ans += (a[j] - a[i]) ans (a[j] - a[i]) a[j] a j a[i] a i {
                j = i;
            } j = i j i System.out.format("%.0f",ans) System.out.format System.out System System.out "%.0f" ans System.out.println() System.out.println System.out System System.out String[] args args public class Main {
    public static void main(String[] args) throws Exception{
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        in.nextToken();
        int N = (int) in.nval;
        int[] a = new int[N];
        for(int i = 0; i < N; i++){
            in.nextToken();
            a[i] = (int) in.nval;
        }
        int j = 0;
        double ans = 0;
        for(int i = 1; i < N; i++){
            if(a[i] < a[j]){
                ans += (a[j] - a[i]);
            }else{
                j = i;
            }
        }
        System.out.format("%.0f",ans);
        System.out.println();
    }
} public class Main {
    public static void main(String[] args) throws Exception{
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        in.nextToken();
        int N = (int) in.nval;
        int[] a = new int[N];
        for(int i = 0; i < N; i++){
            in.nextToken();
            a[i] = (int) in.nval;
        }
        int j = 0;
        double ans = 0;
        for(int i = 1; i < N; i++){
            if(a[i] < a[j]){
                ans += (a[j] - a[i]);
            }else{
                j = i;
            }
        }
        System.out.format("%.0f",ans);
        System.out.println();
    }
} Main
import java.util.*;
import java.io.*;
public class Main {
    public static void main(String[] args) throws Exception{
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        in.nextToken();
        int N = (int) in.nval;
        int[] a = new int[N];
        for(int i = 0; i < N; i++){
            in.nextToken();
            a[i] = (int) in.nval;
        }
        int j = 0;
        double ans = 0;
        for(int i = 1; i < N; i++){
            if(a[i] < a[j]){
                ans += (a[j] - a[i]);
            }else{
                j = i;
            }
        }
        System.out.format("%.0f",ans);
        System.out.println();
    }
}
