import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] arr = new int[n];
        for(int i=0; i<n; i++)
            arr[i] = in.nextInt();
        int maxSoFar = arr[0];
        long ans = 0;
        for(int i : arr) {
            if(i > maxSoFar) {
                maxSoFar = i;
            } else {
                ans += (long) (maxSoFar - i);
            }
        }
        System.out.println(ans);
        in.close();
    }
} 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[] arr = new int[n];
        for(int i=0; i<n; i++)
            arr[i] = in.nextInt();
        int maxSoFar = arr[0];
        long ans = 0;
        for(int i : arr) {
            if(i > maxSoFar) {
                maxSoFar = i;
            } else {
                ans += (long) (maxSoFar - i);
            }
        }
        System.out.println(ans);
        in.close();
    }
} Main public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] arr = new int[n];
        for(int i=0; i<n; i++)
            arr[i] = in.nextInt();
        int maxSoFar = arr[0];
        long ans = 0;
        for(int i : arr) {
            if(i > maxSoFar) {
                maxSoFar = i;
            } else {
                ans += (long) (maxSoFar - i);
            }
        }
        System.out.println(ans);
        in.close();
    } main {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] arr = new int[n];
        for(int i=0; i<n; i++)
            arr[i] = in.nextInt();
        int maxSoFar = arr[0];
        long ans = 0;
        for(int i : arr) {
            if(i > maxSoFar) {
                maxSoFar = i;
            } else {
                ans += (long) (maxSoFar - i);
            }
        }
        System.out.println(ans);
        in.close();
    } Scanner in = new Scanner(System.in); in new Scanner(System.in) int n = in.nextInt(); n in.nextInt() in.nextInt in int[] arr = new int[n]; arr new int[n] n for(int i=0; i<n; i++)
            arr[i] = in.nextInt(); int i=0; int i=0; i 0 i<n i n i++ i++ i arr[i] = in.nextInt(); arr[i] = in.nextInt() arr[i] arr i in.nextInt() in.nextInt in int maxSoFar = arr[0]; maxSoFar arr[0] arr 0 long ans = 0; ans 0 for(int i : arr) {
            if(i > maxSoFar) {
                maxSoFar = i;
            } else {
                ans += (long) (maxSoFar - i);
            }
        } int i arr {
            if(i > maxSoFar) {
                maxSoFar = i;
            } else {
                ans += (long) (maxSoFar - i);
            }
        } {
            if(i > maxSoFar) {
                maxSoFar = i;
            } else {
                ans += (long) (maxSoFar - i);
            }
        } if(i > maxSoFar) {
                maxSoFar = i;
            } else {
                ans += (long) (maxSoFar - i);
            } i > maxSoFar i maxSoFar {
                maxSoFar = i;
            } maxSoFar = i maxSoFar i {
                ans += (long) (maxSoFar - i);
            } ans += (long) (maxSoFar - i) ans (long) (maxSoFar - i) maxSoFar i System.out.println(ans) System.out.println System.out System System.out ans in.close() in.close in String[] args args public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] arr = new int[n];
        for(int i=0; i<n; i++)
            arr[i] = in.nextInt();
        int maxSoFar = arr[0];
        long ans = 0;
        for(int i : arr) {
            if(i > maxSoFar) {
                maxSoFar = i;
            } else {
                ans += (long) (maxSoFar - i);
            }
        }
        System.out.println(ans);
        in.close();
    }
} public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] arr = new int[n];
        for(int i=0; i<n; i++)
            arr[i] = in.nextInt();
        int maxSoFar = arr[0];
        long ans = 0;
        for(int i : arr) {
            if(i > maxSoFar) {
                maxSoFar = i;
            } else {
                ans += (long) (maxSoFar - i);
            }
        }
        System.out.println(ans);
        in.close();
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] arr = new int[n];
        for(int i=0; i<n; i++)
            arr[i] = in.nextInt();
        int maxSoFar = arr[0];
        long ans = 0;
        for(int i : arr) {
            if(i > maxSoFar) {
                maxSoFar = i;
            } else {
                ans += (long) (maxSoFar - i);
            }
        }
        System.out.println(ans);
        in.close();
    }
}