import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int last = -1;
        int n = scanner.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) {
            nums[i] = scanner.nextInt();
        }
        long ans = 0;
        for (int i = 0; i < n; i++) {
            int cur = nums[i];
            if (cur < last) {
                ans += (last - cur);
            } else {
                last = cur;
            }
        }
        System.out.println(ans);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int last = -1;
        int n = scanner.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) {
            nums[i] = scanner.nextInt();
        }
        long ans = 0;
        for (int i = 0; i < n; i++) {
            int cur = nums[i];
            if (cur < last) {
                ans += (last - cur);
            } else {
                last = cur;
            }
        }
        System.out.println(ans);
    }
} Main public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int last = -1;
        int n = scanner.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) {
            nums[i] = scanner.nextInt();
        }
        long ans = 0;
        for (int i = 0; i < n; i++) {
            int cur = nums[i];
            if (cur < last) {
                ans += (last - cur);
            } else {
                last = cur;
            }
        }
        System.out.println(ans);
    } main {
        Scanner scanner = new Scanner(System.in);
        int last = -1;
        int n = scanner.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) {
            nums[i] = scanner.nextInt();
        }
        long ans = 0;
        for (int i = 0; i < n; i++) {
            int cur = nums[i];
            if (cur < last) {
                ans += (last - cur);
            } else {
                last = cur;
            }
        }
        System.out.println(ans);
    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int last = -1; last -1 1 int n = scanner.nextInt(); n scanner.nextInt() scanner.nextInt scanner int[] nums = new int[n]; nums new int[n] n for (int i = 0; i < n; i++) {
            nums[i] = scanner.nextInt();
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            nums[i] = scanner.nextInt();
        } {
            nums[i] = scanner.nextInt();
        } nums[i] = scanner.nextInt() nums[i] nums i scanner.nextInt() scanner.nextInt scanner long ans = 0; ans 0 for (int i = 0; i < n; i++) {
            int cur = nums[i];
            if (cur < last) {
                ans += (last - cur);
            } else {
                last = cur;
            }
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            int cur = nums[i];
            if (cur < last) {
                ans += (last - cur);
            } else {
                last = cur;
            }
        } {
            int cur = nums[i];
            if (cur < last) {
                ans += (last - cur);
            } else {
                last = cur;
            }
        } int cur = nums[i]; cur nums[i] nums i if (cur < last) {
                ans += (last - cur);
            } else {
                last = cur;
            } cur < last cur last {
                ans += (last - cur);
            } ans += (last - cur) ans (last - cur) last cur {
                last = cur;
            } last = cur last cur System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int last = -1;
        int n = scanner.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) {
            nums[i] = scanner.nextInt();
        }
        long ans = 0;
        for (int i = 0; i < n; i++) {
            int cur = nums[i];
            if (cur < last) {
                ans += (last - cur);
            } else {
                last = cur;
            }
        }
        System.out.println(ans);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int last = -1;
        int n = scanner.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) {
            nums[i] = scanner.nextInt();
        }
        long ans = 0;
        for (int i = 0; i < n; i++) {
            int cur = nums[i];
            if (cur < last) {
                ans += (last - cur);
            } else {
                last = cur;
            }
        }
        System.out.println(ans);
    }
} Main
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int last = -1;
        int n = scanner.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) {
            nums[i] = scanner.nextInt();
        }
        long ans = 0;
        for (int i = 0; i < n; i++) {
            int cur = nums[i];
            if (cur < last) {
                ans += (last - cur);
            } else {
                last = cur;
            }
        }
        System.out.println(ans);
    }
}
