import java.io.*;
import java.math.*;
import java.util.*;
 
public class Main {
	static final long MOD = 1000000007L;
	static final int INF = 50000000;
	
	public static void main(String[] args) {
		FastScanner sc = new FastScanner();
		PrintWriter pw = new PrintWriter(System.out);
		
		int N = sc.ni();
		long[] nums = new long[N];
		long[] target = new long[N];
		nums[0] = sc.nl();
		target[0] = nums[0];
		long ans = 0;
		for (int i = 1; i < N; i++) {
			nums[i] = sc.nl();
			target[i] = Math.max(nums[i], target[i-1]);
			ans += (target[i]-nums[i]);
		}
		pw.println(ans);
		pw.close();
	}

    static class FastScanner { 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastScanner() { 
            br = new BufferedReader(new InputStreamReader(System.in)); 
        } 
  
        String next() { 
            while (st == null || !st.hasMoreElements()) { 
                try { 
                    st = new StringTokenizer(br.readLine());
                } catch (IOException  e) { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        }
        
        int ni() { 
            return Integer.parseInt(next()); 
        }
  
        long nl() { 
            return Long.parseLong(next()); 
        } 
  
        double nd() { 
            return Double.parseDouble(next()); 
        } 
  
        String nextLine() {
            String str = ""; 
            try { 
                str = br.readLine(); 
            } catch (IOException e) {
                e.printStackTrace(); 
            } 
            return str;
        }
    }
} import java.io.*; io.* java import java.math.*; math.* java import java.util.*; util.* java public class Main {
	static final long MOD = 1000000007L;
	static final int INF = 50000000;
	
	public static void main(String[] args) {
		FastScanner sc = new FastScanner();
		PrintWriter pw = new PrintWriter(System.out);
		
		int N = sc.ni();
		long[] nums = new long[N];
		long[] target = new long[N];
		nums[0] = sc.nl();
		target[0] = nums[0];
		long ans = 0;
		for (int i = 1; i < N; i++) {
			nums[i] = sc.nl();
			target[i] = Math.max(nums[i], target[i-1]);
			ans += (target[i]-nums[i]);
		}
		pw.println(ans);
		pw.close();
	}

    static class FastScanner { 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastScanner() { 
            br = new BufferedReader(new InputStreamReader(System.in)); 
        } 
  
        String next() { 
            while (st == null || !st.hasMoreElements()) { 
                try { 
                    st = new StringTokenizer(br.readLine());
                } catch (IOException  e) { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        }
        
        int ni() { 
            return Integer.parseInt(next()); 
        }
  
        long nl() { 
            return Long.parseLong(next()); 
        } 
  
        double nd() { 
            return Double.parseDouble(next()); 
        } 
  
        String nextLine() {
            String str = ""; 
            try { 
                str = br.readLine(); 
            } catch (IOException e) {
                e.printStackTrace(); 
            } 
            return str;
        }
    }
} Main static final long MOD = 1000000007L; MOD 1000000007L static final int INF = 50000000; INF 50000000 public static void main(String[] args) {
		FastScanner sc = new FastScanner();
		PrintWriter pw = new PrintWriter(System.out);
		
		int N = sc.ni();
		long[] nums = new long[N];
		long[] target = new long[N];
		nums[0] = sc.nl();
		target[0] = nums[0];
		long ans = 0;
		for (int i = 1; i < N; i++) {
			nums[i] = sc.nl();
			target[i] = Math.max(nums[i], target[i-1]);
			ans += (target[i]-nums[i]);
		}
		pw.println(ans);
		pw.close();
	} main {
		FastScanner sc = new FastScanner();
		PrintWriter pw = new PrintWriter(System.out);
		
		int N = sc.ni();
		long[] nums = new long[N];
		long[] target = new long[N];
		nums[0] = sc.nl();
		target[0] = nums[0];
		long ans = 0;
		for (int i = 1; i < N; i++) {
			nums[i] = sc.nl();
			target[i] = Math.max(nums[i], target[i-1]);
			ans += (target[i]-nums[i]);
		}
		pw.println(ans);
		pw.close();
	} FastScanner sc = new FastScanner(); sc new FastScanner() PrintWriter pw = new PrintWriter(System.out); pw new PrintWriter(System.out) int N = sc.ni(); N sc.ni() sc.ni sc long[] nums = new long[N]; nums new long[N] N long[] target = new long[N]; target new long[N] N nums[0] = sc.nl() nums[0] nums 0 sc.nl() sc.nl sc target[0] = nums[0] target[0] target 0 nums[0] nums 0 long ans = 0; ans 0 for (int i = 1; i < N; i++) {
			nums[i] = sc.nl();
			target[i] = Math.max(nums[i], target[i-1]);
			ans += (target[i]-nums[i]);
		} int i = 1; int i = 1; i 1 i < N i N i++ i++ i {
			nums[i] = sc.nl();
			target[i] = Math.max(nums[i], target[i-1]);
			ans += (target[i]-nums[i]);
		} {
			nums[i] = sc.nl();
			target[i] = Math.max(nums[i], target[i-1]);
			ans += (target[i]-nums[i]);
		} nums[i] = sc.nl() nums[i] nums i sc.nl() sc.nl sc target[i] = Math.max(nums[i], target[i-1]) target[i] target i Math.max(nums[i], target[i-1]) Math.max Math nums[i] nums i target[i-1] target i-1 i 1 ans += (target[i]-nums[i]) ans (target[i]-nums[i]) target[i] target i nums[i] nums i pw.println(ans) pw.println pw ans pw.close() pw.close pw String[] args args static class FastScanner { 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastScanner() { 
            br = new BufferedReader(new InputStreamReader(System.in)); 
        } 
  
        String next() { 
            while (st == null || !st.hasMoreElements()) { 
                try { 
                    st = new StringTokenizer(br.readLine());
                } catch (IOException  e) { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        }
        
        int ni() { 
            return Integer.parseInt(next()); 
        }
  
        long nl() { 
            return Long.parseLong(next()); 
        } 
  
        double nd() { 
            return Double.parseDouble(next()); 
        } 
  
        String nextLine() {
            String str = ""; 
            try { 
                str = br.readLine(); 
            } catch (IOException e) {
                e.printStackTrace(); 
            } 
            return str;
        }
    } FastScanner BufferedReader br; br StringTokenizer st; st public FastScanner() { 
            br = new BufferedReader(new InputStreamReader(System.in)); 
        } FastScanner { 
            br = new BufferedReader(new InputStreamReader(System.in)); 
        } br = new BufferedReader(new InputStreamReader(System.in)) br new BufferedReader(new InputStreamReader(System.in)) String next() { 
            while (st == null || !st.hasMoreElements()) { 
                try { 
                    st = new StringTokenizer(br.readLine());
                } catch (IOException  e) { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        } next { 
            while (st == null || !st.hasMoreElements()) { 
                try { 
                    st = new StringTokenizer(br.readLine());
                } catch (IOException  e) { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        } while (st == null || !st.hasMoreElements()) { 
                try { 
                    st = new StringTokenizer(br.readLine());
                } catch (IOException  e) { 
                    e.printStackTrace(); 
                } 
            } st == null || !st.hasMoreElements() st == null st null !st.hasMoreElements() st.hasMoreElements() st.hasMoreElements st { 
                try { 
                    st = new StringTokenizer(br.readLine());
                } catch (IOException  e) { 
                    e.printStackTrace(); 
                } 
            } try { 
                    st = new StringTokenizer(br.readLine());
                } catch (IOException  e) { 
                    e.printStackTrace(); 
                } catch (IOException  e) { 
                    e.printStackTrace(); 
                } IOException  e { 
                    e.printStackTrace(); 
                } e.printStackTrace() e.printStackTrace e { 
                    st = new StringTokenizer(br.readLine());
                } st = new StringTokenizer(br.readLine()) st new StringTokenizer(br.readLine()) return st.nextToken(); st.nextToken() st.nextToken st int ni() { 
            return Integer.parseInt(next()); 
        } ni { 
            return Integer.parseInt(next()); 
        } return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next long nl() { 
            return Long.parseLong(next()); 
        } nl { 
            return Long.parseLong(next()); 
        } return Long.parseLong(next()); Long.parseLong(next()) Long.parseLong Long next() next double nd() { 
            return Double.parseDouble(next()); 
        } nd { 
            return Double.parseDouble(next()); 
        } return Double.parseDouble(next()); Double.parseDouble(next()) Double.parseDouble Double next() next String nextLine() {
            String str = ""; 
            try { 
                str = br.readLine(); 
            } catch (IOException e) {
                e.printStackTrace(); 
            } 
            return str;
        } nextLine {
            String str = ""; 
            try { 
                str = br.readLine(); 
            } catch (IOException e) {
                e.printStackTrace(); 
            } 
            return str;
        } String str = ""; str "" try { 
                str = br.readLine(); 
            } catch (IOException e) {
                e.printStackTrace(); 
            } catch (IOException e) {
                e.printStackTrace(); 
            } IOException e {
                e.printStackTrace(); 
            } e.printStackTrace() e.printStackTrace e { 
                str = br.readLine(); 
            } str = br.readLine() str br.readLine() br.readLine br return str; str public class Main {
	static final long MOD = 1000000007L;
	static final int INF = 50000000;
	
	public static void main(String[] args) {
		FastScanner sc = new FastScanner();
		PrintWriter pw = new PrintWriter(System.out);
		
		int N = sc.ni();
		long[] nums = new long[N];
		long[] target = new long[N];
		nums[0] = sc.nl();
		target[0] = nums[0];
		long ans = 0;
		for (int i = 1; i < N; i++) {
			nums[i] = sc.nl();
			target[i] = Math.max(nums[i], target[i-1]);
			ans += (target[i]-nums[i]);
		}
		pw.println(ans);
		pw.close();
	}

    static class FastScanner { 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastScanner() { 
            br = new BufferedReader(new InputStreamReader(System.in)); 
        } 
  
        String next() { 
            while (st == null || !st.hasMoreElements()) { 
                try { 
                    st = new StringTokenizer(br.readLine());
                } catch (IOException  e) { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        }
        
        int ni() { 
            return Integer.parseInt(next()); 
        }
  
        long nl() { 
            return Long.parseLong(next()); 
        } 
  
        double nd() { 
            return Double.parseDouble(next()); 
        } 
  
        String nextLine() {
            String str = ""; 
            try { 
                str = br.readLine(); 
            } catch (IOException e) {
                e.printStackTrace(); 
            } 
            return str;
        }
    }
} public class Main {
	static final long MOD = 1000000007L;
	static final int INF = 50000000;
	
	public static void main(String[] args) {
		FastScanner sc = new FastScanner();
		PrintWriter pw = new PrintWriter(System.out);
		
		int N = sc.ni();
		long[] nums = new long[N];
		long[] target = new long[N];
		nums[0] = sc.nl();
		target[0] = nums[0];
		long ans = 0;
		for (int i = 1; i < N; i++) {
			nums[i] = sc.nl();
			target[i] = Math.max(nums[i], target[i-1]);
			ans += (target[i]-nums[i]);
		}
		pw.println(ans);
		pw.close();
	}

    static class FastScanner { 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastScanner() { 
            br = new BufferedReader(new InputStreamReader(System.in)); 
        } 
  
        String next() { 
            while (st == null || !st.hasMoreElements()) { 
                try { 
                    st = new StringTokenizer(br.readLine());
                } catch (IOException  e) { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        }
        
        int ni() { 
            return Integer.parseInt(next()); 
        }
  
        long nl() { 
            return Long.parseLong(next()); 
        } 
  
        double nd() { 
            return Double.parseDouble(next()); 
        } 
  
        String nextLine() {
            String str = ""; 
            try { 
                str = br.readLine(); 
            } catch (IOException e) {
                e.printStackTrace(); 
            } 
            return str;
        }
    }
} Main
import java.io.*;
import java.math.*;
import java.util.*;
 
public class Main {
	static final long MOD = 1000000007L;
	static final int INF = 50000000;
	
	public static void main(String[] args) {
		FastScanner sc = new FastScanner();
		PrintWriter pw = new PrintWriter(System.out);
		
		int N = sc.ni();
		long[] nums = new long[N];
		long[] target = new long[N];
		nums[0] = sc.nl();
		target[0] = nums[0];
		long ans = 0;
		for (int i = 1; i < N; i++) {
			nums[i] = sc.nl();
			target[i] = Math.max(nums[i], target[i-1]);
			ans += (target[i]-nums[i]);
		}
		pw.println(ans);
		pw.close();
	}

    static class FastScanner { 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastScanner() { 
            br = new BufferedReader(new InputStreamReader(System.in)); 
        } 
  
        String next() { 
            while (st == null || !st.hasMoreElements()) { 
                try { 
                    st = new StringTokenizer(br.readLine());
                } catch (IOException  e) { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        }
        
        int ni() { 
            return Integer.parseInt(next()); 
        }
  
        long nl() { 
            return Long.parseLong(next()); 
        } 
  
        double nd() { 
            return Double.parseDouble(next()); 
        } 
  
        String nextLine() {
            String str = ""; 
            try { 
                str = br.readLine(); 
            } catch (IOException e) {
                e.printStackTrace(); 
            } 
            return str;
        }
    }
}