import java.util.*;

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

public class Main {
	public static void main(String[] args){
 
        Scanner sc = null;
        sc = new Scanner(System.in);
        long ans = 0;
        long max = 0;
        long work = 0;
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            work = sc.nextLong();
            if (i != 0 && work < max) {
                ans += (max - work);
            }else if(work > max){
                max = work;
            }
        }
        sc.close();
        System.out.println(ans);   
      }
}