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

class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String n = br.readLine();
        int person = Integer.parseInt(n);
        long[] hi = new long[person];
        String[] str = br.readLine().split(" ");

        for (int i = 0; i < person; i++) {
            hi[i] = Long.parseLong(str[i]);
        }

        long sum = 0;
        for (int i = 1; i < person; i++) {
            if (hi[i - 1] > hi[i]) {
                long diff = (hi[i - 1] - hi[i]);
                sum += diff;
                hi[i] += diff;
            }
        }
        System.out.println(sum);
    }
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String n = br.readLine();
        int person = Integer.parseInt(n);
        long[] hi = new long[person];
        String[] str = br.readLine().split(" ");

        for (int i = 0; i < person; i++) {
            hi[i] = Long.parseLong(str[i]);
        }

        long sum = 0;
        for (int i = 1; i < person; i++) {
            if (hi[i - 1] > hi[i]) {
                long diff = (hi[i - 1] - hi[i]);
                sum += diff;
                hi[i] += diff;
            }
        }
        System.out.println(sum);
    }
} Main public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String n = br.readLine();
        int person = Integer.parseInt(n);
        long[] hi = new long[person];
        String[] str = br.readLine().split(" ");

        for (int i = 0; i < person; i++) {
            hi[i] = Long.parseLong(str[i]);
        }

        long sum = 0;
        for (int i = 1; i < person; i++) {
            if (hi[i - 1] > hi[i]) {
                long diff = (hi[i - 1] - hi[i]);
                sum += diff;
                hi[i] += diff;
            }
        }
        System.out.println(sum);
    } main {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String n = br.readLine();
        int person = Integer.parseInt(n);
        long[] hi = new long[person];
        String[] str = br.readLine().split(" ");

        for (int i = 0; i < person; i++) {
            hi[i] = Long.parseLong(str[i]);
        }

        long sum = 0;
        for (int i = 1; i < person; i++) {
            if (hi[i - 1] > hi[i]) {
                long diff = (hi[i - 1] - hi[i]);
                sum += diff;
                hi[i] += diff;
            }
        }
        System.out.println(sum);
    } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String n = br.readLine(); n br.readLine() br.readLine br int person = Integer.parseInt(n); person Integer.parseInt(n) Integer.parseInt Integer n long[] hi = new long[person]; hi new long[person] person String[] str = br.readLine().split(" "); str br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " for (int i = 0; i < person; i++) {
            hi[i] = Long.parseLong(str[i]);
        } int i = 0; int i = 0; i 0 i < person i person i++ i++ i {
            hi[i] = Long.parseLong(str[i]);
        } {
            hi[i] = Long.parseLong(str[i]);
        } hi[i] = Long.parseLong(str[i]) hi[i] hi i Long.parseLong(str[i]) Long.parseLong Long str[i] str i long sum = 0; sum 0 for (int i = 1; i < person; i++) {
            if (hi[i - 1] > hi[i]) {
                long diff = (hi[i - 1] - hi[i]);
                sum += diff;
                hi[i] += diff;
            }
        } int i = 1; int i = 1; i 1 i < person i person i++ i++ i {
            if (hi[i - 1] > hi[i]) {
                long diff = (hi[i - 1] - hi[i]);
                sum += diff;
                hi[i] += diff;
            }
        } {
            if (hi[i - 1] > hi[i]) {
                long diff = (hi[i - 1] - hi[i]);
                sum += diff;
                hi[i] += diff;
            }
        } if (hi[i - 1] > hi[i]) {
                long diff = (hi[i - 1] - hi[i]);
                sum += diff;
                hi[i] += diff;
            } hi[i - 1] > hi[i] hi[i - 1] hi i - 1 i 1 hi[i] hi i {
                long diff = (hi[i - 1] - hi[i]);
                sum += diff;
                hi[i] += diff;
            } long diff = (hi[i - 1] - hi[i]); diff (hi[i - 1] - hi[i]) hi[i - 1] hi i - 1 i 1 hi[i] hi i sum += diff sum diff hi[i] += diff hi[i] hi i diff System.out.println(sum) System.out.println System.out System System.out sum String[] args args
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String n = br.readLine();
        int person = Integer.parseInt(n);
        long[] hi = new long[person];
        String[] str = br.readLine().split(" ");

        for (int i = 0; i < person; i++) {
            hi[i] = Long.parseLong(str[i]);
        }

        long sum = 0;
        for (int i = 1; i < person; i++) {
            if (hi[i - 1] > hi[i]) {
                long diff = (hi[i - 1] - hi[i]);
                sum += diff;
                hi[i] += diff;
            }
        }
        System.out.println(sum);
    }
}
