import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main
{
	static int MOD = (int) 1e9 + 7;
	
	static class FenwickTree
	{
		// bit[1] ~ bit[n]
		long[] bit;   // = new long[n+1];
		
		public FenwickTree(int n)
		{
			bit = new long[n+1];
		}
		
		// bit[1] + ... + bit[r]
		long sum(int r)
		{
			long s = 0;
			for( int i=r; i>0; i-=(i & -i) )
			{
				s += bit[i];
			}
			return s;
		}
		
		void add(int idx, int x)
		{
			for( int i=idx; i<bit.length; i+=(i & -i) )
			{
				bit[i] += x;
			}
		}
	}

	static class UnionFind
	{
		int[] par;
		int[] n_nodes;
		int max = 1;
		
		public UnionFind(int n)
		{
			par = new int[n];
			n_nodes = new int[n];
			for( int i=0; i<n; i++ )
			{
				par[i] = i;
				n_nodes[i] = 1;
			}
		}
		
		int root(int x)
		{
			if( par[x] == x ) return x;
			return par[x] = root(par[x]);
		}
		
		void unite(int x, int y)
		{
			int rx = root(x);
			int ry = root(y);
			if( rx == ry ) return;
			par[rx] = ry;
			n_nodes[ry] += n_nodes[rx];
			max = Math.max(max, n_nodes[ry]);
		}
		
		boolean same(int x, int y)
		{
			int rx = root(x);
			int ry = root(y);
			return rx == ry;
		}
	}
	
	static class Calc
	{
		static int gcd(int a, int b)
		{
			if( a == 0 )
			{
				return b;
			}
			return gcd(b%a, a);
		}
		
		static int ceilInt(int a, int b)
		{
			return (a%b==0 ? a/b : a/b+1);
		}

		static long power(long x, long n)
		{
			if( n == 0 )
			{
				return 1;
			}

			if( n % 2 == 0 )
			{
				long X = (x * x) % MOD;
				return power(X, n/2);
			}
			else
			{
				return (x * power(x, n-1)) % MOD;
			}
		}
	}
	
	static class MyScanner
	{
		private BufferedReader in;
		private StringTokenizer st;
		
		public MyScanner(InputStream stream)
		{
			in = new BufferedReader(new InputStreamReader(stream));
		}
		
		public String next()
		{
			while( st == null || !st.hasMoreTokens() )
			{
				try
				{
					String rl = in.readLine();
					if( rl == null )
					{
						return null;
					}
					st = new StringTokenizer(rl);
				}
				catch( IOException e )
				{
					return null;
				}
			}
			return st.nextToken();
		}
		
		public int nextInt()
		{
			return Integer.parseInt(next());
		}
		
		public long nextLong()
		{
			return Long.parseLong(next());
		}
	}
	
	static interface Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out);
	}
	
	static class F implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
		}
	}

	static class E implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
		}
	}

	static class D implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
		}
	}
	
	static class C implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
			int n = in.nextInt();
			long ans = 0;
			int max = 0;
			for( int i=0; i<n; i++ )
			{
				int a = in.nextInt();
				ans += Math.max(0, max-a);
				max = Math.max(max, a);
			}
			out.println(ans);
		}
	}
	
	static class B implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
			String n = in.next();
			int mod = 0;
			for( int i=0; i<n.length(); i++ )
			{
				mod += Integer.parseInt(n.substring(i, i+1));
			}
			out.println(mod%9==0? "Yes" : "No");
		}
	}

	static class A implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
			int n = in.nextInt();
			int x = in.nextInt();
			int t = in.nextInt();
			out.println(Calc.ceilInt(n, x) * t);
		}
	}
	
	public static void main(String[] args)
	{
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		MyScanner in = new MyScanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		Solver solver = new C();
		solver.solve(1, in, out);
		out.close();
	}
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStream; InputStream java.io import java.io.InputStreamReader; InputStreamReader java.io import java.io.OutputStream; OutputStream java.io import java.io.PrintWriter; PrintWriter java.io import java.util.StringTokenizer; StringTokenizer java.util public class Main
{
	static int MOD = (int) 1e9 + 7;
	
	static class FenwickTree
	{
		// bit[1] ~ bit[n]
		long[] bit;   // = new long[n+1];
		
		public FenwickTree(int n)
		{
			bit = new long[n+1];
		}
		
		// bit[1] + ... + bit[r]
		long sum(int r)
		{
			long s = 0;
			for( int i=r; i>0; i-=(i & -i) )
			{
				s += bit[i];
			}
			return s;
		}
		
		void add(int idx, int x)
		{
			for( int i=idx; i<bit.length; i+=(i & -i) )
			{
				bit[i] += x;
			}
		}
	}

	static class UnionFind
	{
		int[] par;
		int[] n_nodes;
		int max = 1;
		
		public UnionFind(int n)
		{
			par = new int[n];
			n_nodes = new int[n];
			for( int i=0; i<n; i++ )
			{
				par[i] = i;
				n_nodes[i] = 1;
			}
		}
		
		int root(int x)
		{
			if( par[x] == x ) return x;
			return par[x] = root(par[x]);
		}
		
		void unite(int x, int y)
		{
			int rx = root(x);
			int ry = root(y);
			if( rx == ry ) return;
			par[rx] = ry;
			n_nodes[ry] += n_nodes[rx];
			max = Math.max(max, n_nodes[ry]);
		}
		
		boolean same(int x, int y)
		{
			int rx = root(x);
			int ry = root(y);
			return rx == ry;
		}
	}
	
	static class Calc
	{
		static int gcd(int a, int b)
		{
			if( a == 0 )
			{
				return b;
			}
			return gcd(b%a, a);
		}
		
		static int ceilInt(int a, int b)
		{
			return (a%b==0 ? a/b : a/b+1);
		}

		static long power(long x, long n)
		{
			if( n == 0 )
			{
				return 1;
			}

			if( n % 2 == 0 )
			{
				long X = (x * x) % MOD;
				return power(X, n/2);
			}
			else
			{
				return (x * power(x, n-1)) % MOD;
			}
		}
	}
	
	static class MyScanner
	{
		private BufferedReader in;
		private StringTokenizer st;
		
		public MyScanner(InputStream stream)
		{
			in = new BufferedReader(new InputStreamReader(stream));
		}
		
		public String next()
		{
			while( st == null || !st.hasMoreTokens() )
			{
				try
				{
					String rl = in.readLine();
					if( rl == null )
					{
						return null;
					}
					st = new StringTokenizer(rl);
				}
				catch( IOException e )
				{
					return null;
				}
			}
			return st.nextToken();
		}
		
		public int nextInt()
		{
			return Integer.parseInt(next());
		}
		
		public long nextLong()
		{
			return Long.parseLong(next());
		}
	}
	
	static interface Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out);
	}
	
	static class F implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
		}
	}

	static class E implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
		}
	}

	static class D implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
		}
	}
	
	static class C implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
			int n = in.nextInt();
			long ans = 0;
			int max = 0;
			for( int i=0; i<n; i++ )
			{
				int a = in.nextInt();
				ans += Math.max(0, max-a);
				max = Math.max(max, a);
			}
			out.println(ans);
		}
	}
	
	static class B implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
			String n = in.next();
			int mod = 0;
			for( int i=0; i<n.length(); i++ )
			{
				mod += Integer.parseInt(n.substring(i, i+1));
			}
			out.println(mod%9==0? "Yes" : "No");
		}
	}

	static class A implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
			int n = in.nextInt();
			int x = in.nextInt();
			int t = in.nextInt();
			out.println(Calc.ceilInt(n, x) * t);
		}
	}
	
	public static void main(String[] args)
	{
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		MyScanner in = new MyScanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		Solver solver = new C();
		solver.solve(1, in, out);
		out.close();
	}
} Main static int MOD = (int) 1e9 + 7; MOD (int) 1e9 + 7 (int) 1e9 7 public static void main(String[] args)
	{
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		MyScanner in = new MyScanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		Solver solver = new C();
		solver.solve(1, in, out);
		out.close();
	} main {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		MyScanner in = new MyScanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		Solver solver = new C();
		solver.solve(1, in, out);
		out.close();
	} InputStream inputStream = System.in; inputStream System.in System System.in OutputStream outputStream = System.out; outputStream System.out System System.out MyScanner in = new MyScanner(inputStream); in new MyScanner(inputStream) PrintWriter out = new PrintWriter(outputStream); out new PrintWriter(outputStream) Solver solver = new C(); solver new C() solver.solve(1, in, out) solver.solve solver 1 in out out.close() out.close out String[] args args static class FenwickTree
	{
		// bit[1] ~ bit[n]
		long[] bit;   // = new long[n+1];
		
		public FenwickTree(int n)
		{
			bit = new long[n+1];
		}
		
		// bit[1] + ... + bit[r]
		long sum(int r)
		{
			long s = 0;
			for( int i=r; i>0; i-=(i & -i) )
			{
				s += bit[i];
			}
			return s;
		}
		
		void add(int idx, int x)
		{
			for( int i=idx; i<bit.length; i+=(i & -i) )
			{
				bit[i] += x;
			}
		}
	} FenwickTree // bit[1] ~ bit[n]
		long[] bit;   // = new long[n+1]; bit public FenwickTree(int n)
		{
			bit = new long[n+1];
		} FenwickTree {
			bit = new long[n+1];
		} bit = new long[n+1] bit new long[n+1] n+1 n 1 int n n // bit[1] + ... + bit[r]
		long sum(int r)
		{
			long s = 0;
			for( int i=r; i>0; i-=(i & -i) )
			{
				s += bit[i];
			}
			return s;
		} sum {
			long s = 0;
			for( int i=r; i>0; i-=(i & -i) )
			{
				s += bit[i];
			}
			return s;
		} long s = 0; s 0 for( int i=r; i>0; i-=(i & -i) )
			{
				s += bit[i];
			} int i=r; int i=r; i r i>0 i 0 i-=(i & -i) i-=(i & -i) i (i & -i) i -i i {
				s += bit[i];
			} {
				s += bit[i];
			} s += bit[i] s bit[i] bit i return s; s int r r void add(int idx, int x)
		{
			for( int i=idx; i<bit.length; i+=(i & -i) )
			{
				bit[i] += x;
			}
		} add {
			for( int i=idx; i<bit.length; i+=(i & -i) )
			{
				bit[i] += x;
			}
		} for( int i=idx; i<bit.length; i+=(i & -i) )
			{
				bit[i] += x;
			} int i=idx; int i=idx; i idx i<bit.length i bit.length bit bit.length i+=(i & -i) i+=(i & -i) i (i & -i) i -i i {
				bit[i] += x;
			} {
				bit[i] += x;
			} bit[i] += x bit[i] bit i x int idx idx int x x static class UnionFind
	{
		int[] par;
		int[] n_nodes;
		int max = 1;
		
		public UnionFind(int n)
		{
			par = new int[n];
			n_nodes = new int[n];
			for( int i=0; i<n; i++ )
			{
				par[i] = i;
				n_nodes[i] = 1;
			}
		}
		
		int root(int x)
		{
			if( par[x] == x ) return x;
			return par[x] = root(par[x]);
		}
		
		void unite(int x, int y)
		{
			int rx = root(x);
			int ry = root(y);
			if( rx == ry ) return;
			par[rx] = ry;
			n_nodes[ry] += n_nodes[rx];
			max = Math.max(max, n_nodes[ry]);
		}
		
		boolean same(int x, int y)
		{
			int rx = root(x);
			int ry = root(y);
			return rx == ry;
		}
	} UnionFind int[] par; par int[] n_nodes; n_nodes int max = 1; max 1 public UnionFind(int n)
		{
			par = new int[n];
			n_nodes = new int[n];
			for( int i=0; i<n; i++ )
			{
				par[i] = i;
				n_nodes[i] = 1;
			}
		} UnionFind {
			par = new int[n];
			n_nodes = new int[n];
			for( int i=0; i<n; i++ )
			{
				par[i] = i;
				n_nodes[i] = 1;
			}
		} par = new int[n] par new int[n] n n_nodes = new int[n] n_nodes new int[n] n for( int i=0; i<n; i++ )
			{
				par[i] = i;
				n_nodes[i] = 1;
			} int i=0; int i=0; i 0 i<n i n i++ i++ i {
				par[i] = i;
				n_nodes[i] = 1;
			} {
				par[i] = i;
				n_nodes[i] = 1;
			} par[i] = i par[i] par i i n_nodes[i] = 1 n_nodes[i] n_nodes i 1 int n n int root(int x)
		{
			if( par[x] == x ) return x;
			return par[x] = root(par[x]);
		} root {
			if( par[x] == x ) return x;
			return par[x] = root(par[x]);
		} if( par[x] == x ) return x; par[x] == x par[x] par x x return x; x return par[x] = root(par[x]); par[x] = root(par[x]) par[x] par x root(par[x]) root par[x] par x int x x void unite(int x, int y)
		{
			int rx = root(x);
			int ry = root(y);
			if( rx == ry ) return;
			par[rx] = ry;
			n_nodes[ry] += n_nodes[rx];
			max = Math.max(max, n_nodes[ry]);
		} unite {
			int rx = root(x);
			int ry = root(y);
			if( rx == ry ) return;
			par[rx] = ry;
			n_nodes[ry] += n_nodes[rx];
			max = Math.max(max, n_nodes[ry]);
		} int rx = root(x); rx root(x) root x int ry = root(y); ry root(y) root y if( rx == ry ) return; rx == ry rx ry return; par[rx] = ry par[rx] par rx ry n_nodes[ry] += n_nodes[rx] n_nodes[ry] n_nodes ry n_nodes[rx] n_nodes rx max = Math.max(max, n_nodes[ry]) max Math.max(max, n_nodes[ry]) Math.max Math max n_nodes[ry] n_nodes ry int x x int y y boolean same(int x, int y)
		{
			int rx = root(x);
			int ry = root(y);
			return rx == ry;
		} same {
			int rx = root(x);
			int ry = root(y);
			return rx == ry;
		} int rx = root(x); rx root(x) root x int ry = root(y); ry root(y) root y return rx == ry; rx == ry rx ry int x x int y y static class Calc
	{
		static int gcd(int a, int b)
		{
			if( a == 0 )
			{
				return b;
			}
			return gcd(b%a, a);
		}
		
		static int ceilInt(int a, int b)
		{
			return (a%b==0 ? a/b : a/b+1);
		}

		static long power(long x, long n)
		{
			if( n == 0 )
			{
				return 1;
			}

			if( n % 2 == 0 )
			{
				long X = (x * x) % MOD;
				return power(X, n/2);
			}
			else
			{
				return (x * power(x, n-1)) % MOD;
			}
		}
	} Calc static int gcd(int a, int b)
		{
			if( a == 0 )
			{
				return b;
			}
			return gcd(b%a, a);
		} gcd {
			if( a == 0 )
			{
				return b;
			}
			return gcd(b%a, a);
		} if( a == 0 )
			{
				return b;
			} a == 0 a 0 {
				return b;
			} return b; b return gcd(b%a, a); gcd(b%a, a) gcd b%a b a a int a a int b b static int ceilInt(int a, int b)
		{
			return (a%b==0 ? a/b : a/b+1);
		} ceilInt {
			return (a%b==0 ? a/b : a/b+1);
		} return (a%b==0 ? a/b : a/b+1); (a%b==0 ? a/b : a/b+1) a%b==0 a%b a b 0 a/b a b a/b+1 a/b a b 1 int a a int b b static long power(long x, long n)
		{
			if( n == 0 )
			{
				return 1;
			}

			if( n % 2 == 0 )
			{
				long X = (x * x) % MOD;
				return power(X, n/2);
			}
			else
			{
				return (x * power(x, n-1)) % MOD;
			}
		} power {
			if( n == 0 )
			{
				return 1;
			}

			if( n % 2 == 0 )
			{
				long X = (x * x) % MOD;
				return power(X, n/2);
			}
			else
			{
				return (x * power(x, n-1)) % MOD;
			}
		} if( n == 0 )
			{
				return 1;
			} n == 0 n 0 {
				return 1;
			} return 1; 1 if( n % 2 == 0 )
			{
				long X = (x * x) % MOD;
				return power(X, n/2);
			}
			else
			{
				return (x * power(x, n-1)) % MOD;
			} n % 2 == 0 n % 2 n 2 0 {
				long X = (x * x) % MOD;
				return power(X, n/2);
			} long X = (x * x) % MOD; X (x * x) % MOD (x * x) x x MOD return power(X, n/2); power(X, n/2) power X n/2 n 2 {
				return (x * power(x, n-1)) % MOD;
			} return (x * power(x, n-1)) % MOD; (x * power(x, n-1)) % MOD (x * power(x, n-1)) x power(x, n-1) power x n-1 n 1 MOD long x x long n n static class MyScanner
	{
		private BufferedReader in;
		private StringTokenizer st;
		
		public MyScanner(InputStream stream)
		{
			in = new BufferedReader(new InputStreamReader(stream));
		}
		
		public String next()
		{
			while( st == null || !st.hasMoreTokens() )
			{
				try
				{
					String rl = in.readLine();
					if( rl == null )
					{
						return null;
					}
					st = new StringTokenizer(rl);
				}
				catch( IOException e )
				{
					return null;
				}
			}
			return st.nextToken();
		}
		
		public int nextInt()
		{
			return Integer.parseInt(next());
		}
		
		public long nextLong()
		{
			return Long.parseLong(next());
		}
	} MyScanner private BufferedReader in; in private StringTokenizer st; st public MyScanner(InputStream stream)
		{
			in = new BufferedReader(new InputStreamReader(stream));
		} MyScanner {
			in = new BufferedReader(new InputStreamReader(stream));
		} in = new BufferedReader(new InputStreamReader(stream)) in new BufferedReader(new InputStreamReader(stream)) InputStream stream stream public String next()
		{
			while( st == null || !st.hasMoreTokens() )
			{
				try
				{
					String rl = in.readLine();
					if( rl == null )
					{
						return null;
					}
					st = new StringTokenizer(rl);
				}
				catch( IOException e )
				{
					return null;
				}
			}
			return st.nextToken();
		} next {
			while( st == null || !st.hasMoreTokens() )
			{
				try
				{
					String rl = in.readLine();
					if( rl == null )
					{
						return null;
					}
					st = new StringTokenizer(rl);
				}
				catch( IOException e )
				{
					return null;
				}
			}
			return st.nextToken();
		} while( st == null || !st.hasMoreTokens() )
			{
				try
				{
					String rl = in.readLine();
					if( rl == null )
					{
						return null;
					}
					st = new StringTokenizer(rl);
				}
				catch( IOException e )
				{
					return null;
				}
			} st == null || !st.hasMoreTokens() st == null st null !st.hasMoreTokens() st.hasMoreTokens() st.hasMoreTokens st {
				try
				{
					String rl = in.readLine();
					if( rl == null )
					{
						return null;
					}
					st = new StringTokenizer(rl);
				}
				catch( IOException e )
				{
					return null;
				}
			} try
				{
					String rl = in.readLine();
					if( rl == null )
					{
						return null;
					}
					st = new StringTokenizer(rl);
				}
				catch( IOException e )
				{
					return null;
				} catch( IOException e )
				{
					return null;
				} IOException e {
					return null;
				} return null; null {
					String rl = in.readLine();
					if( rl == null )
					{
						return null;
					}
					st = new StringTokenizer(rl);
				} String rl = in.readLine(); rl in.readLine() in.readLine in if( rl == null )
					{
						return null;
					} rl == null rl null {
						return null;
					} return null; null st = new StringTokenizer(rl) st new StringTokenizer(rl) return st.nextToken(); st.nextToken() st.nextToken st public int nextInt()
		{
			return Integer.parseInt(next());
		} nextInt {
			return Integer.parseInt(next());
		} return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next public long nextLong()
		{
			return Long.parseLong(next());
		} nextLong {
			return Long.parseLong(next());
		} return Long.parseLong(next()); Long.parseLong(next()) Long.parseLong Long next() next static interface Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out);
	} Solver public void solve(int testNumber, MyScanner in, PrintWriter out); solve int testNumber testNumber MyScanner in in PrintWriter out out static class F implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
		}
	} F public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
		} solve {
		} int testNumber testNumber MyScanner in in PrintWriter out out Solver static class E implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
		}
	} E public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
		} solve {
		} int testNumber testNumber MyScanner in in PrintWriter out out Solver static class D implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
		}
	} D public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
		} solve {
		} int testNumber testNumber MyScanner in in PrintWriter out out Solver static class C implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
			int n = in.nextInt();
			long ans = 0;
			int max = 0;
			for( int i=0; i<n; i++ )
			{
				int a = in.nextInt();
				ans += Math.max(0, max-a);
				max = Math.max(max, a);
			}
			out.println(ans);
		}
	} C public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
			int n = in.nextInt();
			long ans = 0;
			int max = 0;
			for( int i=0; i<n; i++ )
			{
				int a = in.nextInt();
				ans += Math.max(0, max-a);
				max = Math.max(max, a);
			}
			out.println(ans);
		} solve {
			int n = in.nextInt();
			long ans = 0;
			int max = 0;
			for( int i=0; i<n; i++ )
			{
				int a = in.nextInt();
				ans += Math.max(0, max-a);
				max = Math.max(max, a);
			}
			out.println(ans);
		} int n = in.nextInt(); n in.nextInt() in.nextInt in long ans = 0; ans 0 int max = 0; max 0 for( int i=0; i<n; i++ )
			{
				int a = in.nextInt();
				ans += Math.max(0, max-a);
				max = Math.max(max, a);
			} int i=0; int i=0; i 0 i<n i n i++ i++ i {
				int a = in.nextInt();
				ans += Math.max(0, max-a);
				max = Math.max(max, a);
			} {
				int a = in.nextInt();
				ans += Math.max(0, max-a);
				max = Math.max(max, a);
			} int a = in.nextInt(); a in.nextInt() in.nextInt in ans += Math.max(0, max-a) ans Math.max(0, max-a) Math.max Math 0 max-a max a max = Math.max(max, a) max Math.max(max, a) Math.max Math max a out.println(ans) out.println out ans int testNumber testNumber MyScanner in in PrintWriter out out Solver static class B implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
			String n = in.next();
			int mod = 0;
			for( int i=0; i<n.length(); i++ )
			{
				mod += Integer.parseInt(n.substring(i, i+1));
			}
			out.println(mod%9==0? "Yes" : "No");
		}
	} B public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
			String n = in.next();
			int mod = 0;
			for( int i=0; i<n.length(); i++ )
			{
				mod += Integer.parseInt(n.substring(i, i+1));
			}
			out.println(mod%9==0? "Yes" : "No");
		} solve {
			String n = in.next();
			int mod = 0;
			for( int i=0; i<n.length(); i++ )
			{
				mod += Integer.parseInt(n.substring(i, i+1));
			}
			out.println(mod%9==0? "Yes" : "No");
		} String n = in.next(); n in.next() in.next in int mod = 0; mod 0 for( int i=0; i<n.length(); i++ )
			{
				mod += Integer.parseInt(n.substring(i, i+1));
			} int i=0; int i=0; i 0 i<n.length() i n.length() n.length n i++ i++ i {
				mod += Integer.parseInt(n.substring(i, i+1));
			} {
				mod += Integer.parseInt(n.substring(i, i+1));
			} mod += Integer.parseInt(n.substring(i, i+1)) mod Integer.parseInt(n.substring(i, i+1)) Integer.parseInt Integer n.substring(i, i+1) n.substring n i i+1 i 1 out.println(mod%9==0? "Yes" : "No") out.println out mod%9==0? "Yes" : "No" mod%9==0 mod%9 mod 9 0 "Yes" "No" int testNumber testNumber MyScanner in in PrintWriter out out Solver static class A implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
			int n = in.nextInt();
			int x = in.nextInt();
			int t = in.nextInt();
			out.println(Calc.ceilInt(n, x) * t);
		}
	} A public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
			int n = in.nextInt();
			int x = in.nextInt();
			int t = in.nextInt();
			out.println(Calc.ceilInt(n, x) * t);
		} solve {
			int n = in.nextInt();
			int x = in.nextInt();
			int t = in.nextInt();
			out.println(Calc.ceilInt(n, x) * t);
		} int n = in.nextInt(); n in.nextInt() in.nextInt in int x = in.nextInt(); x in.nextInt() in.nextInt in int t = in.nextInt(); t in.nextInt() in.nextInt in out.println(Calc.ceilInt(n, x) * t) out.println out Calc.ceilInt(n, x) * t Calc.ceilInt(n, x) Calc.ceilInt Calc n x t int testNumber testNumber MyScanner in in PrintWriter out out Solver public class Main
{
	static int MOD = (int) 1e9 + 7;
	
	static class FenwickTree
	{
		// bit[1] ~ bit[n]
		long[] bit;   // = new long[n+1];
		
		public FenwickTree(int n)
		{
			bit = new long[n+1];
		}
		
		// bit[1] + ... + bit[r]
		long sum(int r)
		{
			long s = 0;
			for( int i=r; i>0; i-=(i & -i) )
			{
				s += bit[i];
			}
			return s;
		}
		
		void add(int idx, int x)
		{
			for( int i=idx; i<bit.length; i+=(i & -i) )
			{
				bit[i] += x;
			}
		}
	}

	static class UnionFind
	{
		int[] par;
		int[] n_nodes;
		int max = 1;
		
		public UnionFind(int n)
		{
			par = new int[n];
			n_nodes = new int[n];
			for( int i=0; i<n; i++ )
			{
				par[i] = i;
				n_nodes[i] = 1;
			}
		}
		
		int root(int x)
		{
			if( par[x] == x ) return x;
			return par[x] = root(par[x]);
		}
		
		void unite(int x, int y)
		{
			int rx = root(x);
			int ry = root(y);
			if( rx == ry ) return;
			par[rx] = ry;
			n_nodes[ry] += n_nodes[rx];
			max = Math.max(max, n_nodes[ry]);
		}
		
		boolean same(int x, int y)
		{
			int rx = root(x);
			int ry = root(y);
			return rx == ry;
		}
	}
	
	static class Calc
	{
		static int gcd(int a, int b)
		{
			if( a == 0 )
			{
				return b;
			}
			return gcd(b%a, a);
		}
		
		static int ceilInt(int a, int b)
		{
			return (a%b==0 ? a/b : a/b+1);
		}

		static long power(long x, long n)
		{
			if( n == 0 )
			{
				return 1;
			}

			if( n % 2 == 0 )
			{
				long X = (x * x) % MOD;
				return power(X, n/2);
			}
			else
			{
				return (x * power(x, n-1)) % MOD;
			}
		}
	}
	
	static class MyScanner
	{
		private BufferedReader in;
		private StringTokenizer st;
		
		public MyScanner(InputStream stream)
		{
			in = new BufferedReader(new InputStreamReader(stream));
		}
		
		public String next()
		{
			while( st == null || !st.hasMoreTokens() )
			{
				try
				{
					String rl = in.readLine();
					if( rl == null )
					{
						return null;
					}
					st = new StringTokenizer(rl);
				}
				catch( IOException e )
				{
					return null;
				}
			}
			return st.nextToken();
		}
		
		public int nextInt()
		{
			return Integer.parseInt(next());
		}
		
		public long nextLong()
		{
			return Long.parseLong(next());
		}
	}
	
	static interface Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out);
	}
	
	static class F implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
		}
	}

	static class E implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
		}
	}

	static class D implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
		}
	}
	
	static class C implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
			int n = in.nextInt();
			long ans = 0;
			int max = 0;
			for( int i=0; i<n; i++ )
			{
				int a = in.nextInt();
				ans += Math.max(0, max-a);
				max = Math.max(max, a);
			}
			out.println(ans);
		}
	}
	
	static class B implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
			String n = in.next();
			int mod = 0;
			for( int i=0; i<n.length(); i++ )
			{
				mod += Integer.parseInt(n.substring(i, i+1));
			}
			out.println(mod%9==0? "Yes" : "No");
		}
	}

	static class A implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
			int n = in.nextInt();
			int x = in.nextInt();
			int t = in.nextInt();
			out.println(Calc.ceilInt(n, x) * t);
		}
	}
	
	public static void main(String[] args)
	{
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		MyScanner in = new MyScanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		Solver solver = new C();
		solver.solve(1, in, out);
		out.close();
	}
} public class Main
{
	static int MOD = (int) 1e9 + 7;
	
	static class FenwickTree
	{
		// bit[1] ~ bit[n]
		long[] bit;   // = new long[n+1];
		
		public FenwickTree(int n)
		{
			bit = new long[n+1];
		}
		
		// bit[1] + ... + bit[r]
		long sum(int r)
		{
			long s = 0;
			for( int i=r; i>0; i-=(i & -i) )
			{
				s += bit[i];
			}
			return s;
		}
		
		void add(int idx, int x)
		{
			for( int i=idx; i<bit.length; i+=(i & -i) )
			{
				bit[i] += x;
			}
		}
	}

	static class UnionFind
	{
		int[] par;
		int[] n_nodes;
		int max = 1;
		
		public UnionFind(int n)
		{
			par = new int[n];
			n_nodes = new int[n];
			for( int i=0; i<n; i++ )
			{
				par[i] = i;
				n_nodes[i] = 1;
			}
		}
		
		int root(int x)
		{
			if( par[x] == x ) return x;
			return par[x] = root(par[x]);
		}
		
		void unite(int x, int y)
		{
			int rx = root(x);
			int ry = root(y);
			if( rx == ry ) return;
			par[rx] = ry;
			n_nodes[ry] += n_nodes[rx];
			max = Math.max(max, n_nodes[ry]);
		}
		
		boolean same(int x, int y)
		{
			int rx = root(x);
			int ry = root(y);
			return rx == ry;
		}
	}
	
	static class Calc
	{
		static int gcd(int a, int b)
		{
			if( a == 0 )
			{
				return b;
			}
			return gcd(b%a, a);
		}
		
		static int ceilInt(int a, int b)
		{
			return (a%b==0 ? a/b : a/b+1);
		}

		static long power(long x, long n)
		{
			if( n == 0 )
			{
				return 1;
			}

			if( n % 2 == 0 )
			{
				long X = (x * x) % MOD;
				return power(X, n/2);
			}
			else
			{
				return (x * power(x, n-1)) % MOD;
			}
		}
	}
	
	static class MyScanner
	{
		private BufferedReader in;
		private StringTokenizer st;
		
		public MyScanner(InputStream stream)
		{
			in = new BufferedReader(new InputStreamReader(stream));
		}
		
		public String next()
		{
			while( st == null || !st.hasMoreTokens() )
			{
				try
				{
					String rl = in.readLine();
					if( rl == null )
					{
						return null;
					}
					st = new StringTokenizer(rl);
				}
				catch( IOException e )
				{
					return null;
				}
			}
			return st.nextToken();
		}
		
		public int nextInt()
		{
			return Integer.parseInt(next());
		}
		
		public long nextLong()
		{
			return Long.parseLong(next());
		}
	}
	
	static interface Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out);
	}
	
	static class F implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
		}
	}

	static class E implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
		}
	}

	static class D implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
		}
	}
	
	static class C implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
			int n = in.nextInt();
			long ans = 0;
			int max = 0;
			for( int i=0; i<n; i++ )
			{
				int a = in.nextInt();
				ans += Math.max(0, max-a);
				max = Math.max(max, a);
			}
			out.println(ans);
		}
	}
	
	static class B implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
			String n = in.next();
			int mod = 0;
			for( int i=0; i<n.length(); i++ )
			{
				mod += Integer.parseInt(n.substring(i, i+1));
			}
			out.println(mod%9==0? "Yes" : "No");
		}
	}

	static class A implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
			int n = in.nextInt();
			int x = in.nextInt();
			int t = in.nextInt();
			out.println(Calc.ceilInt(n, x) * t);
		}
	}
	
	public static void main(String[] args)
	{
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		MyScanner in = new MyScanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		Solver solver = new C();
		solver.solve(1, in, out);
		out.close();
	}
} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main
{
	static int MOD = (int) 1e9 + 7;
	
	static class FenwickTree
	{
		// bit[1] ~ bit[n]
		long[] bit;   // = new long[n+1];
		
		public FenwickTree(int n)
		{
			bit = new long[n+1];
		}
		
		// bit[1] + ... + bit[r]
		long sum(int r)
		{
			long s = 0;
			for( int i=r; i>0; i-=(i & -i) )
			{
				s += bit[i];
			}
			return s;
		}
		
		void add(int idx, int x)
		{
			for( int i=idx; i<bit.length; i+=(i & -i) )
			{
				bit[i] += x;
			}
		}
	}

	static class UnionFind
	{
		int[] par;
		int[] n_nodes;
		int max = 1;
		
		public UnionFind(int n)
		{
			par = new int[n];
			n_nodes = new int[n];
			for( int i=0; i<n; i++ )
			{
				par[i] = i;
				n_nodes[i] = 1;
			}
		}
		
		int root(int x)
		{
			if( par[x] == x ) return x;
			return par[x] = root(par[x]);
		}
		
		void unite(int x, int y)
		{
			int rx = root(x);
			int ry = root(y);
			if( rx == ry ) return;
			par[rx] = ry;
			n_nodes[ry] += n_nodes[rx];
			max = Math.max(max, n_nodes[ry]);
		}
		
		boolean same(int x, int y)
		{
			int rx = root(x);
			int ry = root(y);
			return rx == ry;
		}
	}
	
	static class Calc
	{
		static int gcd(int a, int b)
		{
			if( a == 0 )
			{
				return b;
			}
			return gcd(b%a, a);
		}
		
		static int ceilInt(int a, int b)
		{
			return (a%b==0 ? a/b : a/b+1);
		}

		static long power(long x, long n)
		{
			if( n == 0 )
			{
				return 1;
			}

			if( n % 2 == 0 )
			{
				long X = (x * x) % MOD;
				return power(X, n/2);
			}
			else
			{
				return (x * power(x, n-1)) % MOD;
			}
		}
	}
	
	static class MyScanner
	{
		private BufferedReader in;
		private StringTokenizer st;
		
		public MyScanner(InputStream stream)
		{
			in = new BufferedReader(new InputStreamReader(stream));
		}
		
		public String next()
		{
			while( st == null || !st.hasMoreTokens() )
			{
				try
				{
					String rl = in.readLine();
					if( rl == null )
					{
						return null;
					}
					st = new StringTokenizer(rl);
				}
				catch( IOException e )
				{
					return null;
				}
			}
			return st.nextToken();
		}
		
		public int nextInt()
		{
			return Integer.parseInt(next());
		}
		
		public long nextLong()
		{
			return Long.parseLong(next());
		}
	}
	
	static interface Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out);
	}
	
	static class F implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
		}
	}

	static class E implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
		}
	}

	static class D implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
		}
	}
	
	static class C implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
			int n = in.nextInt();
			long ans = 0;
			int max = 0;
			for( int i=0; i<n; i++ )
			{
				int a = in.nextInt();
				ans += Math.max(0, max-a);
				max = Math.max(max, a);
			}
			out.println(ans);
		}
	}
	
	static class B implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
			String n = in.next();
			int mod = 0;
			for( int i=0; i<n.length(); i++ )
			{
				mod += Integer.parseInt(n.substring(i, i+1));
			}
			out.println(mod%9==0? "Yes" : "No");
		}
	}

	static class A implements Solver
	{
		public void solve(int testNumber, MyScanner in, PrintWriter out)
		{
			int n = in.nextInt();
			int x = in.nextInt();
			int t = in.nextInt();
			out.println(Calc.ceilInt(n, x) * t);
		}
	}
	
	public static void main(String[] args)
	{
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		MyScanner in = new MyScanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		Solver solver = new C();
		solver.solve(1, in, out);
		out.close();
	}
}