
import java.util.Scanner;

/**
 * Created by enpingkuang on 2020/9/10 11:18 下午
 */
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        long max = 0;
        long ans = 0;
        for(int i = 0;i<n;i++){
            int tmp = scanner.nextInt();
            if(tmp < max){
                ans += (max-tmp);
            }else{
                max = Math.max(tmp,max);
            }
        }
        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 n = scanner.nextInt();
        long max = 0;
        long ans = 0;
        for(int i = 0;i<n;i++){
            int tmp = scanner.nextInt();
            if(tmp < max){
                ans += (max-tmp);
            }else{
                max = Math.max(tmp,max);
            }
        }
        System.out.println(ans);
    }
} Main public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        long max = 0;
        long ans = 0;
        for(int i = 0;i<n;i++){
            int tmp = scanner.nextInt();
            if(tmp < max){
                ans += (max-tmp);
            }else{
                max = Math.max(tmp,max);
            }
        }
        System.out.println(ans);
    } main {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        long max = 0;
        long ans = 0;
        for(int i = 0;i<n;i++){
            int tmp = scanner.nextInt();
            if(tmp < max){
                ans += (max-tmp);
            }else{
                max = Math.max(tmp,max);
            }
        }
        System.out.println(ans);
    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int n = scanner.nextInt(); n scanner.nextInt() scanner.nextInt scanner long max = 0; max 0 long ans = 0; ans 0 for(int i = 0;i<n;i++){
            int tmp = scanner.nextInt();
            if(tmp < max){
                ans += (max-tmp);
            }else{
                max = Math.max(tmp,max);
            }
        } int i = 0; int i = 0; i 0 i<n i n i++ i++ i {
            int tmp = scanner.nextInt();
            if(tmp < max){
                ans += (max-tmp);
            }else{
                max = Math.max(tmp,max);
            }
        } {
            int tmp = scanner.nextInt();
            if(tmp < max){
                ans += (max-tmp);
            }else{
                max = Math.max(tmp,max);
            }
        } int tmp = scanner.nextInt(); tmp scanner.nextInt() scanner.nextInt scanner if(tmp < max){
                ans += (max-tmp);
            }else{
                max = Math.max(tmp,max);
            } tmp < max tmp max {
                ans += (max-tmp);
            } ans += (max-tmp) ans (max-tmp) max tmp {
                max = Math.max(tmp,max);
            } max = Math.max(tmp,max) max Math.max(tmp,max) Math.max Math tmp max 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 n = scanner.nextInt();
        long max = 0;
        long ans = 0;
        for(int i = 0;i<n;i++){
            int tmp = scanner.nextInt();
            if(tmp < max){
                ans += (max-tmp);
            }else{
                max = Math.max(tmp,max);
            }
        }
        System.out.println(ans);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        long max = 0;
        long ans = 0;
        for(int i = 0;i<n;i++){
            int tmp = scanner.nextInt();
            if(tmp < max){
                ans += (max-tmp);
            }else{
                max = Math.max(tmp,max);
            }
        }
        System.out.println(ans);
    }
} Main

import java.util.Scanner;

/**
 * Created by enpingkuang on 2020/9/10 11:18 下午
 */
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        long max = 0;
        long ans = 0;
        for(int i = 0;i<n;i++){
            int tmp = scanner.nextInt();
            if(tmp < max){
                ans += (max-tmp);
            }else{
                max = Math.max(tmp,max);
            }
        }
        System.out.println(ans);
    }
}
