import java.math.*;
import java.util.*;
import java.io.*;
  
public class Main { 
  
    static BufferedReader in;
    static PrintWriter out = new PrintWriter(System.out);
    static String file = "../in";
    static int test = 10; // 0 for local testing, 1 for std input
    static int inf = 1_000_000, mod = 1_000_000_007;



    void swap(int[]ary, int i, int j)
    {
        int t = ary[i];
        ary[i] = ary[j];
        ary[j] = t;
    }

    void swap(char[]ary, int i, int j)
    {
        char t = ary[i];
        ary[i] = ary[j];
        ary[j] = t;
    }
    
    String[] split() throws Exception
    {
        return in.readLine().split(" ");
    }

    int readInt() throws Exception
    {
        return Integer.valueOf(in.readLine());
    }

    long readLong() throws Exception
    {
        return Long.valueOf(in.readLine());
    }

    long[] toLongArray() throws Exception
    {
        String[] sp = split();
        int n = sp.length;
        long[] ary = new long[n];
        for(int i = 0; i < n; i++) ary[i] = Long.valueOf(sp[i]);
        return ary;
    }

    int[] toIntArray() throws Exception
    {
        String[] sp = split();
        int n = sp.length;
        int[] ary = new int[n];
        for(int i = 0; i < n; i++) ary[i] = Integer.valueOf(sp[i]);
        return ary;
    }

    String reverse(String str)
    {
        return new StringBuilder(str).reverse().toString();
    }


    long pow(long a, int pow)
    {
        long res = 1;
        while(pow > 0)
        {
            if(pow % 2 != 0) {
                res *= a;
                res %= mod;
            }
            a = a * a;
            a %= mod;
            pow /= 2;
        }

        return res;
    }

    public static void main(String[] args) throws Exception
    {
        int _k = Integer.valueOf("1");
        if(test > 0) in = new BufferedReader(new InputStreamReader(System.in));
        else in = new BufferedReader(new FileReader(file));
        if(test < 0) {String[] str = in.readLine().split(" ");
        char[] gg = in.readLine().toCharArray();}
/***********************************************************************/
/***********************************************************************/
/***********************************************************************/
/***********************************************************************/
        // System.out.println((-100 + 0) / 2);

        // char[] c = new char[4];
        // c[1] = '.';
        // for(char i = '0'; i <= '9'; i++)
        // {
        //     for(char j = '0'; j <= '9'; j++)
        //     {
        //         for(char k = '0'; k <= '9'; k++)
        //         {
        //             c[0] = i;
        //             c[2] = j;
        //             c[3] = k;
        //             double d = Double.valueOf(String.valueOf(c));
        //             int v = (int)(d * 100);
        //             String s1 = String.valueOf("" + c[0] + c[2] + c[3]),
        //                 s2 = String.valueOf(v);
        //             System.out.printf("%s %s %b\n", s1, s2, s1.equals(s2));
        //         }
        //     }
        // }


        new Main().c();
        out.flush();
    }
    void f() throws Exception
    {
        int k = readInt();
        String s = in.readLine();
        int n = s.length();
        long prod = 1;
        int mod = 1_000_000_007;
        for(int i = 0; i < k; i++)
        {
            n++;
            prod = (prod * 26 * n) % mod;
        }
        System.out.println(prod);

    }

    void e() throws Exception
    {
        int[] ary = toIntArray();
        int n = ary[0], k = ary[1];
        ary = toIntArray();
        int lo = 1, hi = 1 << 30;
        while(lo < hi)
        {
            int m = (lo + hi) / 2;
            int cut = numCut(ary, m);
            // System.out.printf("%d %d\n", m, cut);
            if(cut > k) lo = m + 1;
            else hi = m;
        }
        out.println(lo);
    }

    int numCut(int[] ary, int thres)
    {
        int count = 0;
        for(int v : ary)
        {
            if(v > thres)
            count += v / thres;
            // count += v % thres > 0 ? 1 : 0;
        }
        return count;
    }

    void d() throws Exception
    {
        int n = readInt();
        char[] c = in.readLine().toCharArray();
        int i = 0, j = n - 1, re = 0;
        while(i < j)
        {
            while(i < j && c[i] == 'R') i++;
            while(i < j && c[j] == 'W') j--;
            if(i < j)
            {
                swap(c, i, j);
                i++;
                j--;
                re++;
            }
        }
        out.println(re);
    }



    void c() throws Exception
    {
        int n = readInt();
        int[] ary = toIntArray();
        long sum = 0;
        int mini = 0;
        for(int i = 0; i < n; i++)
        {
            if(ary[i] < mini) sum += mini - ary[i];
            mini = Math.max(ary[i], mini);
        }
        out.println(sum);
        
    }

    int gcd(int a, int b)
    {
        if(b == 0) return a;
         return gcd(b, a % b);
    }

    void b() throws Exception
    {
        String str = in.readLine();
        int sum = 0;
        for(char c : str.toCharArray())
        {
            int val = c - '0';
            sum += val;
            sum %= 9;
        }
        if(sum == 0) out.println("Yes");
        else out.println("No");
        
    }

    boolean transform(String s1, String s2)
    {
        // from s1 -> s2
        int m = s1.length(), n = s2.length();
        if(m < n) return false;
        int j = n - 1;
        for(int i = m - 1; i >=0; i--)
        {
            if(s1.charAt(i) == s2.charAt(j)) j--;
            if(j < 0) return true;
        }
        return false;
    }

    void a() throws Exception
    {
        int[] ary = toIntArray();
        int n = ary[0], x = ary[1], t = ary[2];
        int mult = n / x;
        if(n % x != 0) mult++;
        out.println(mult * t);
        
    }
} import java.math.*; math.* java import java.util.*; util.* java import java.io.*; io.* java public class Main { 
  
    static BufferedReader in;
    static PrintWriter out = new PrintWriter(System.out);
    static String file = "../in";
    static int test = 10; // 0 for local testing, 1 for std input
    static int inf = 1_000_000, mod = 1_000_000_007;



    void swap(int[]ary, int i, int j)
    {
        int t = ary[i];
        ary[i] = ary[j];
        ary[j] = t;
    }

    void swap(char[]ary, int i, int j)
    {
        char t = ary[i];
        ary[i] = ary[j];
        ary[j] = t;
    }
    
    String[] split() throws Exception
    {
        return in.readLine().split(" ");
    }

    int readInt() throws Exception
    {
        return Integer.valueOf(in.readLine());
    }

    long readLong() throws Exception
    {
        return Long.valueOf(in.readLine());
    }

    long[] toLongArray() throws Exception
    {
        String[] sp = split();
        int n = sp.length;
        long[] ary = new long[n];
        for(int i = 0; i < n; i++) ary[i] = Long.valueOf(sp[i]);
        return ary;
    }

    int[] toIntArray() throws Exception
    {
        String[] sp = split();
        int n = sp.length;
        int[] ary = new int[n];
        for(int i = 0; i < n; i++) ary[i] = Integer.valueOf(sp[i]);
        return ary;
    }

    String reverse(String str)
    {
        return new StringBuilder(str).reverse().toString();
    }


    long pow(long a, int pow)
    {
        long res = 1;
        while(pow > 0)
        {
            if(pow % 2 != 0) {
                res *= a;
                res %= mod;
            }
            a = a * a;
            a %= mod;
            pow /= 2;
        }

        return res;
    }

    public static void main(String[] args) throws Exception
    {
        int _k = Integer.valueOf("1");
        if(test > 0) in = new BufferedReader(new InputStreamReader(System.in));
        else in = new BufferedReader(new FileReader(file));
        if(test < 0) {String[] str = in.readLine().split(" ");
        char[] gg = in.readLine().toCharArray();}
/***********************************************************************/
/***********************************************************************/
/***********************************************************************/
/***********************************************************************/
        // System.out.println((-100 + 0) / 2);

        // char[] c = new char[4];
        // c[1] = '.';
        // for(char i = '0'; i <= '9'; i++)
        // {
        //     for(char j = '0'; j <= '9'; j++)
        //     {
        //         for(char k = '0'; k <= '9'; k++)
        //         {
        //             c[0] = i;
        //             c[2] = j;
        //             c[3] = k;
        //             double d = Double.valueOf(String.valueOf(c));
        //             int v = (int)(d * 100);
        //             String s1 = String.valueOf("" + c[0] + c[2] + c[3]),
        //                 s2 = String.valueOf(v);
        //             System.out.printf("%s %s %b\n", s1, s2, s1.equals(s2));
        //         }
        //     }
        // }


        new Main().c();
        out.flush();
    }
    void f() throws Exception
    {
        int k = readInt();
        String s = in.readLine();
        int n = s.length();
        long prod = 1;
        int mod = 1_000_000_007;
        for(int i = 0; i < k; i++)
        {
            n++;
            prod = (prod * 26 * n) % mod;
        }
        System.out.println(prod);

    }

    void e() throws Exception
    {
        int[] ary = toIntArray();
        int n = ary[0], k = ary[1];
        ary = toIntArray();
        int lo = 1, hi = 1 << 30;
        while(lo < hi)
        {
            int m = (lo + hi) / 2;
            int cut = numCut(ary, m);
            // System.out.printf("%d %d\n", m, cut);
            if(cut > k) lo = m + 1;
            else hi = m;
        }
        out.println(lo);
    }

    int numCut(int[] ary, int thres)
    {
        int count = 0;
        for(int v : ary)
        {
            if(v > thres)
            count += v / thres;
            // count += v % thres > 0 ? 1 : 0;
        }
        return count;
    }

    void d() throws Exception
    {
        int n = readInt();
        char[] c = in.readLine().toCharArray();
        int i = 0, j = n - 1, re = 0;
        while(i < j)
        {
            while(i < j && c[i] == 'R') i++;
            while(i < j && c[j] == 'W') j--;
            if(i < j)
            {
                swap(c, i, j);
                i++;
                j--;
                re++;
            }
        }
        out.println(re);
    }



    void c() throws Exception
    {
        int n = readInt();
        int[] ary = toIntArray();
        long sum = 0;
        int mini = 0;
        for(int i = 0; i < n; i++)
        {
            if(ary[i] < mini) sum += mini - ary[i];
            mini = Math.max(ary[i], mini);
        }
        out.println(sum);
        
    }

    int gcd(int a, int b)
    {
        if(b == 0) return a;
         return gcd(b, a % b);
    }

    void b() throws Exception
    {
        String str = in.readLine();
        int sum = 0;
        for(char c : str.toCharArray())
        {
            int val = c - '0';
            sum += val;
            sum %= 9;
        }
        if(sum == 0) out.println("Yes");
        else out.println("No");
        
    }

    boolean transform(String s1, String s2)
    {
        // from s1 -> s2
        int m = s1.length(), n = s2.length();
        if(m < n) return false;
        int j = n - 1;
        for(int i = m - 1; i >=0; i--)
        {
            if(s1.charAt(i) == s2.charAt(j)) j--;
            if(j < 0) return true;
        }
        return false;
    }

    void a() throws Exception
    {
        int[] ary = toIntArray();
        int n = ary[0], x = ary[1], t = ary[2];
        int mult = n / x;
        if(n % x != 0) mult++;
        out.println(mult * t);
        
    }
} Main static BufferedReader in; in static PrintWriter out = new PrintWriter(System.out); out new PrintWriter(System.out) static String file = "../in"; file "../in" static int test = 10; // 0 for local testing, 1 for std input test 10 static int inf = 1_000_000 inf 1_000_000 mod = 1_000_000_007; mod 1_000_000_007 void swap(int[]ary, int i, int j)
    {
        int t = ary[i];
        ary[i] = ary[j];
        ary[j] = t;
    } swap {
        int t = ary[i];
        ary[i] = ary[j];
        ary[j] = t;
    } int t = ary[i]; t ary[i] ary i ary[i] = ary[j] ary[i] ary i ary[j] ary j ary[j] = t ary[j] ary j t int[]ary ary int i i int j j void swap(char[]ary, int i, int j)
    {
        char t = ary[i];
        ary[i] = ary[j];
        ary[j] = t;
    } swap {
        char t = ary[i];
        ary[i] = ary[j];
        ary[j] = t;
    } char t = ary[i]; t ary[i] ary i ary[i] = ary[j] ary[i] ary i ary[j] ary j ary[j] = t ary[j] ary j t char[]ary ary int i i int j j String[] split() throws Exception
    {
        return in.readLine().split(" ");
    } split {
        return in.readLine().split(" ");
    } return in.readLine().split(" "); in.readLine().split(" ") in.readLine().split in.readLine() in.readLine in " " int readInt() throws Exception
    {
        return Integer.valueOf(in.readLine());
    } readInt {
        return Integer.valueOf(in.readLine());
    } return Integer.valueOf(in.readLine()); Integer.valueOf(in.readLine()) Integer.valueOf Integer in.readLine() in.readLine in long readLong() throws Exception
    {
        return Long.valueOf(in.readLine());
    } readLong {
        return Long.valueOf(in.readLine());
    } return Long.valueOf(in.readLine()); Long.valueOf(in.readLine()) Long.valueOf Long in.readLine() in.readLine in long[] toLongArray() throws Exception
    {
        String[] sp = split();
        int n = sp.length;
        long[] ary = new long[n];
        for(int i = 0; i < n; i++) ary[i] = Long.valueOf(sp[i]);
        return ary;
    } toLongArray {
        String[] sp = split();
        int n = sp.length;
        long[] ary = new long[n];
        for(int i = 0; i < n; i++) ary[i] = Long.valueOf(sp[i]);
        return ary;
    } String[] sp = split(); sp split() split int n = sp.length; n sp.length sp sp.length long[] ary = new long[n]; ary new long[n] n for(int i = 0; i < n; i++) ary[i] = Long.valueOf(sp[i]); int i = 0; int i = 0; i 0 i < n i n i++ i++ i ary[i] = Long.valueOf(sp[i]); ary[i] = Long.valueOf(sp[i]) ary[i] ary i Long.valueOf(sp[i]) Long.valueOf Long sp[i] sp i return ary; ary int[] toIntArray() throws Exception
    {
        String[] sp = split();
        int n = sp.length;
        int[] ary = new int[n];
        for(int i = 0; i < n; i++) ary[i] = Integer.valueOf(sp[i]);
        return ary;
    } toIntArray {
        String[] sp = split();
        int n = sp.length;
        int[] ary = new int[n];
        for(int i = 0; i < n; i++) ary[i] = Integer.valueOf(sp[i]);
        return ary;
    } String[] sp = split(); sp split() split int n = sp.length; n sp.length sp sp.length int[] ary = new int[n]; ary new int[n] n for(int i = 0; i < n; i++) ary[i] = Integer.valueOf(sp[i]); int i = 0; int i = 0; i 0 i < n i n i++ i++ i ary[i] = Integer.valueOf(sp[i]); ary[i] = Integer.valueOf(sp[i]) ary[i] ary i Integer.valueOf(sp[i]) Integer.valueOf Integer sp[i] sp i return ary; ary String reverse(String str)
    {
        return new StringBuilder(str).reverse().toString();
    } reverse {
        return new StringBuilder(str).reverse().toString();
    } return new StringBuilder(str).reverse().toString(); new StringBuilder(str).reverse().toString() new StringBuilder(str).reverse().toString new StringBuilder(str).reverse() new StringBuilder(str).reverse new StringBuilder(str) String str str long pow(long a, int pow)
    {
        long res = 1;
        while(pow > 0)
        {
            if(pow % 2 != 0) {
                res *= a;
                res %= mod;
            }
            a = a * a;
            a %= mod;
            pow /= 2;
        }

        return res;
    } pow {
        long res = 1;
        while(pow > 0)
        {
            if(pow % 2 != 0) {
                res *= a;
                res %= mod;
            }
            a = a * a;
            a %= mod;
            pow /= 2;
        }

        return res;
    } long res = 1; res 1 while(pow > 0)
        {
            if(pow % 2 != 0) {
                res *= a;
                res %= mod;
            }
            a = a * a;
            a %= mod;
            pow /= 2;
        } pow > 0 pow 0 {
            if(pow % 2 != 0) {
                res *= a;
                res %= mod;
            }
            a = a * a;
            a %= mod;
            pow /= 2;
        } if(pow % 2 != 0) {
                res *= a;
                res %= mod;
            } pow % 2 != 0 pow % 2 pow 2 0 {
                res *= a;
                res %= mod;
            } res *= a res a res %= mod res mod a = a * a a a * a a a a %= mod a mod pow /= 2 pow 2 return res; res long a a int pow pow public static void main(String[] args) throws Exception
    {
        int _k = Integer.valueOf("1");
        if(test > 0) in = new BufferedReader(new InputStreamReader(System.in));
        else in = new BufferedReader(new FileReader(file));
        if(test < 0) {String[] str = in.readLine().split(" ");
        char[] gg = in.readLine().toCharArray();}
/***********************************************************************/
/***********************************************************************/
/***********************************************************************/
/***********************************************************************/
        // System.out.println((-100 + 0) / 2);

        // char[] c = new char[4];
        // c[1] = '.';
        // for(char i = '0'; i <= '9'; i++)
        // {
        //     for(char j = '0'; j <= '9'; j++)
        //     {
        //         for(char k = '0'; k <= '9'; k++)
        //         {
        //             c[0] = i;
        //             c[2] = j;
        //             c[3] = k;
        //             double d = Double.valueOf(String.valueOf(c));
        //             int v = (int)(d * 100);
        //             String s1 = String.valueOf("" + c[0] + c[2] + c[3]),
        //                 s2 = String.valueOf(v);
        //             System.out.printf("%s %s %b\n", s1, s2, s1.equals(s2));
        //         }
        //     }
        // }


        new Main().c();
        out.flush();
    } main {
        int _k = Integer.valueOf("1");
        if(test > 0) in = new BufferedReader(new InputStreamReader(System.in));
        else in = new BufferedReader(new FileReader(file));
        if(test < 0) {String[] str = in.readLine().split(" ");
        char[] gg = in.readLine().toCharArray();}
/***********************************************************************/
/***********************************************************************/
/***********************************************************************/
/***********************************************************************/
        // System.out.println((-100 + 0) / 2);

        // char[] c = new char[4];
        // c[1] = '.';
        // for(char i = '0'; i <= '9'; i++)
        // {
        //     for(char j = '0'; j <= '9'; j++)
        //     {
        //         for(char k = '0'; k <= '9'; k++)
        //         {
        //             c[0] = i;
        //             c[2] = j;
        //             c[3] = k;
        //             double d = Double.valueOf(String.valueOf(c));
        //             int v = (int)(d * 100);
        //             String s1 = String.valueOf("" + c[0] + c[2] + c[3]),
        //                 s2 = String.valueOf(v);
        //             System.out.printf("%s %s %b\n", s1, s2, s1.equals(s2));
        //         }
        //     }
        // }


        new Main().c();
        out.flush();
    } int _k = Integer.valueOf("1"); _k Integer.valueOf("1") Integer.valueOf Integer "1" if(test > 0) in = new BufferedReader(new InputStreamReader(System.in));
        else in = new BufferedReader(new FileReader(file)); test > 0 test 0 in = new BufferedReader(new InputStreamReader(System.in)) in new BufferedReader(new InputStreamReader(System.in)) in = new BufferedReader(new FileReader(file)) in new BufferedReader(new FileReader(file)) if(test < 0) {String[] str = in.readLine().split(" ");
        char[] gg = in.readLine().toCharArray();} test < 0 test 0 {String[] str = in.readLine().split(" ");
        char[] gg = in.readLine().toCharArray();} String[] str = in.readLine().split(" "); str in.readLine().split(" ") in.readLine().split in.readLine() in.readLine in " " char[] gg = in.readLine().toCharArray(); gg in.readLine().toCharArray() in.readLine().toCharArray in.readLine() in.readLine in new Main().c() new Main().c new Main() out.flush() out.flush out String[] args args void f() throws Exception
    {
        int k = readInt();
        String s = in.readLine();
        int n = s.length();
        long prod = 1;
        int mod = 1_000_000_007;
        for(int i = 0; i < k; i++)
        {
            n++;
            prod = (prod * 26 * n) % mod;
        }
        System.out.println(prod);

    } f {
        int k = readInt();
        String s = in.readLine();
        int n = s.length();
        long prod = 1;
        int mod = 1_000_000_007;
        for(int i = 0; i < k; i++)
        {
            n++;
            prod = (prod * 26 * n) % mod;
        }
        System.out.println(prod);

    } int k = readInt(); k readInt() readInt String s = in.readLine(); s in.readLine() in.readLine in int n = s.length(); n s.length() s.length s long prod = 1; prod 1 int mod = 1_000_000_007; mod 1_000_000_007 for(int i = 0; i < k; i++)
        {
            n++;
            prod = (prod * 26 * n) % mod;
        } int i = 0; int i = 0; i 0 i < k i k i++ i++ i {
            n++;
            prod = (prod * 26 * n) % mod;
        } {
            n++;
            prod = (prod * 26 * n) % mod;
        } n++ n prod = (prod * 26 * n) % mod prod (prod * 26 * n) % mod (prod * 26 * n) prod * 26 * n prod 26 n mod System.out.println(prod) System.out.println System.out System System.out prod void e() throws Exception
    {
        int[] ary = toIntArray();
        int n = ary[0], k = ary[1];
        ary = toIntArray();
        int lo = 1, hi = 1 << 30;
        while(lo < hi)
        {
            int m = (lo + hi) / 2;
            int cut = numCut(ary, m);
            // System.out.printf("%d %d\n", m, cut);
            if(cut > k) lo = m + 1;
            else hi = m;
        }
        out.println(lo);
    } e {
        int[] ary = toIntArray();
        int n = ary[0], k = ary[1];
        ary = toIntArray();
        int lo = 1, hi = 1 << 30;
        while(lo < hi)
        {
            int m = (lo + hi) / 2;
            int cut = numCut(ary, m);
            // System.out.printf("%d %d\n", m, cut);
            if(cut > k) lo = m + 1;
            else hi = m;
        }
        out.println(lo);
    } int[] ary = toIntArray(); ary toIntArray() toIntArray int n = ary[0] n ary[0] ary 0 k = ary[1]; k ary[1] ary 1 ary = toIntArray() ary toIntArray() toIntArray int lo = 1 lo 1 hi = 1 << 30; hi 1 << 30 1 30 while(lo < hi)
        {
            int m = (lo + hi) / 2;
            int cut = numCut(ary, m);
            // System.out.printf("%d %d\n", m, cut);
            if(cut > k) lo = m + 1;
            else hi = m;
        } lo < hi lo hi {
            int m = (lo + hi) / 2;
            int cut = numCut(ary, m);
            // System.out.printf("%d %d\n", m, cut);
            if(cut > k) lo = m + 1;
            else hi = m;
        } int m = (lo + hi) / 2; m (lo + hi) / 2 (lo + hi) lo hi 2 int cut = numCut(ary, m); cut numCut(ary, m) numCut ary m if(cut > k) lo = m + 1;
            else hi = m; cut > k cut k lo = m + 1 lo m + 1 m 1 hi = m hi m out.println(lo) out.println out lo int numCut(int[] ary, int thres)
    {
        int count = 0;
        for(int v : ary)
        {
            if(v > thres)
            count += v / thres;
            // count += v % thres > 0 ? 1 : 0;
        }
        return count;
    } numCut {
        int count = 0;
        for(int v : ary)
        {
            if(v > thres)
            count += v / thres;
            // count += v % thres > 0 ? 1 : 0;
        }
        return count;
    } int count = 0; count 0 for(int v : ary)
        {
            if(v > thres)
            count += v / thres;
            // count += v % thres > 0 ? 1 : 0;
        } int v ary {
            if(v > thres)
            count += v / thres;
            // count += v % thres > 0 ? 1 : 0;
        } {
            if(v > thres)
            count += v / thres;
            // count += v % thres > 0 ? 1 : 0;
        } if(v > thres)
            count += v / thres; v > thres v thres count += v / thres count v / thres v thres return count; count int[] ary ary int thres thres void d() throws Exception
    {
        int n = readInt();
        char[] c = in.readLine().toCharArray();
        int i = 0, j = n - 1, re = 0;
        while(i < j)
        {
            while(i < j && c[i] == 'R') i++;
            while(i < j && c[j] == 'W') j--;
            if(i < j)
            {
                swap(c, i, j);
                i++;
                j--;
                re++;
            }
        }
        out.println(re);
    } d {
        int n = readInt();
        char[] c = in.readLine().toCharArray();
        int i = 0, j = n - 1, re = 0;
        while(i < j)
        {
            while(i < j && c[i] == 'R') i++;
            while(i < j && c[j] == 'W') j--;
            if(i < j)
            {
                swap(c, i, j);
                i++;
                j--;
                re++;
            }
        }
        out.println(re);
    } int n = readInt(); n readInt() readInt char[] c = in.readLine().toCharArray(); c in.readLine().toCharArray() in.readLine().toCharArray in.readLine() in.readLine in int i = 0 i 0 j = n - 1 j n - 1 n 1 re = 0; re 0 while(i < j)
        {
            while(i < j && c[i] == 'R') i++;
            while(i < j && c[j] == 'W') j--;
            if(i < j)
            {
                swap(c, i, j);
                i++;
                j--;
                re++;
            }
        } i < j i j {
            while(i < j && c[i] == 'R') i++;
            while(i < j && c[j] == 'W') j--;
            if(i < j)
            {
                swap(c, i, j);
                i++;
                j--;
                re++;
            }
        } while(i < j && c[i] == 'R') i++; i < j && c[i] == 'R' i < j i j c[i] == 'R' c[i] c i 'R' i++ i while(i < j && c[j] == 'W') j--; i < j && c[j] == 'W' i < j i j c[j] == 'W' c[j] c j 'W' j-- j if(i < j)
            {
                swap(c, i, j);
                i++;
                j--;
                re++;
            } i < j i j {
                swap(c, i, j);
                i++;
                j--;
                re++;
            } swap(c, i, j) swap c i j i++ i j-- j re++ re out.println(re) out.println out re void c() throws Exception
    {
        int n = readInt();
        int[] ary = toIntArray();
        long sum = 0;
        int mini = 0;
        for(int i = 0; i < n; i++)
        {
            if(ary[i] < mini) sum += mini - ary[i];
            mini = Math.max(ary[i], mini);
        }
        out.println(sum);
        
    } c {
        int n = readInt();
        int[] ary = toIntArray();
        long sum = 0;
        int mini = 0;
        for(int i = 0; i < n; i++)
        {
            if(ary[i] < mini) sum += mini - ary[i];
            mini = Math.max(ary[i], mini);
        }
        out.println(sum);
        
    } int n = readInt(); n readInt() readInt int[] ary = toIntArray(); ary toIntArray() toIntArray long sum = 0; sum 0 int mini = 0; mini 0 for(int i = 0; i < n; i++)
        {
            if(ary[i] < mini) sum += mini - ary[i];
            mini = Math.max(ary[i], mini);
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            if(ary[i] < mini) sum += mini - ary[i];
            mini = Math.max(ary[i], mini);
        } {
            if(ary[i] < mini) sum += mini - ary[i];
            mini = Math.max(ary[i], mini);
        } if(ary[i] < mini) sum += mini - ary[i]; ary[i] < mini ary[i] ary i mini sum += mini - ary[i] sum mini - ary[i] mini ary[i] ary i mini = Math.max(ary[i], mini) mini Math.max(ary[i], mini) Math.max Math ary[i] ary i mini out.println(sum) out.println out sum int gcd(int a, int b)
    {
        if(b == 0) return a;
         return gcd(b, a % b);
    } gcd {
        if(b == 0) return a;
         return gcd(b, a % b);
    } if(b == 0) return a; b == 0 b 0 return a; a return gcd(b, a % b); gcd(b, a % b) gcd b a % b a b int a a int b b void b() throws Exception
    {
        String str = in.readLine();
        int sum = 0;
        for(char c : str.toCharArray())
        {
            int val = c - '0';
            sum += val;
            sum %= 9;
        }
        if(sum == 0) out.println("Yes");
        else out.println("No");
        
    } b {
        String str = in.readLine();
        int sum = 0;
        for(char c : str.toCharArray())
        {
            int val = c - '0';
            sum += val;
            sum %= 9;
        }
        if(sum == 0) out.println("Yes");
        else out.println("No");
        
    } String str = in.readLine(); str in.readLine() in.readLine in int sum = 0; sum 0 for(char c : str.toCharArray())
        {
            int val = c - '0';
            sum += val;
            sum %= 9;
        } char c str.toCharArray() str.toCharArray str {
            int val = c - '0';
            sum += val;
            sum %= 9;
        } {
            int val = c - '0';
            sum += val;
            sum %= 9;
        } int val = c - '0'; val c - '0' c '0' sum += val sum val sum %= 9 sum 9 if(sum == 0) out.println("Yes");
        else out.println("No"); sum == 0 sum 0 out.println("Yes") out.println out "Yes" out.println("No") out.println out "No" boolean transform(String s1, String s2)
    {
        // from s1 -> s2
        int m = s1.length(), n = s2.length();
        if(m < n) return false;
        int j = n - 1;
        for(int i = m - 1; i >=0; i--)
        {
            if(s1.charAt(i) == s2.charAt(j)) j--;
            if(j < 0) return true;
        }
        return false;
    } transform {
        // from s1 -> s2
        int m = s1.length(), n = s2.length();
        if(m < n) return false;
        int j = n - 1;
        for(int i = m - 1; i >=0; i--)
        {
            if(s1.charAt(i) == s2.charAt(j)) j--;
            if(j < 0) return true;
        }
        return false;
    } int m = s1.length() m s1.length() s1.length s1 n = s2.length(); n s2.length() s2.length s2 if(m < n) return false; m < n m n return false; false int j = n - 1; j n - 1 n 1 for(int i = m - 1; i >=0; i--)
        {
            if(s1.charAt(i) == s2.charAt(j)) j--;
            if(j < 0) return true;
        } int i = m - 1; int i = m - 1; i m - 1 m 1 i >=0 i 0 i-- i-- i {
            if(s1.charAt(i) == s2.charAt(j)) j--;
            if(j < 0) return true;
        } {
            if(s1.charAt(i) == s2.charAt(j)) j--;
            if(j < 0) return true;
        } if(s1.charAt(i) == s2.charAt(j)) j--; s1.charAt(i) == s2.charAt(j) s1.charAt(i) s1.charAt s1 i s2.charAt(j) s2.charAt s2 j j-- j if(j < 0) return true; j < 0 j 0 return true; true return false; false String s1 s1 String s2 s2 void a() throws Exception
    {
        int[] ary = toIntArray();
        int n = ary[0], x = ary[1], t = ary[2];
        int mult = n / x;
        if(n % x != 0) mult++;
        out.println(mult * t);
        
    } a {
        int[] ary = toIntArray();
        int n = ary[0], x = ary[1], t = ary[2];
        int mult = n / x;
        if(n % x != 0) mult++;
        out.println(mult * t);
        
    } int[] ary = toIntArray(); ary toIntArray() toIntArray int n = ary[0] n ary[0] ary 0 x = ary[1] x ary[1] ary 1 t = ary[2]; t ary[2] ary 2 int mult = n / x; mult n / x n x if(n % x != 0) mult++; n % x != 0 n % x n x 0 mult++ mult out.println(mult * t) out.println out mult * t mult t public class Main { 
  
    static BufferedReader in;
    static PrintWriter out = new PrintWriter(System.out);
    static String file = "../in";
    static int test = 10; // 0 for local testing, 1 for std input
    static int inf = 1_000_000, mod = 1_000_000_007;



    void swap(int[]ary, int i, int j)
    {
        int t = ary[i];
        ary[i] = ary[j];
        ary[j] = t;
    }

    void swap(char[]ary, int i, int j)
    {
        char t = ary[i];
        ary[i] = ary[j];
        ary[j] = t;
    }
    
    String[] split() throws Exception
    {
        return in.readLine().split(" ");
    }

    int readInt() throws Exception
    {
        return Integer.valueOf(in.readLine());
    }

    long readLong() throws Exception
    {
        return Long.valueOf(in.readLine());
    }

    long[] toLongArray() throws Exception
    {
        String[] sp = split();
        int n = sp.length;
        long[] ary = new long[n];
        for(int i = 0; i < n; i++) ary[i] = Long.valueOf(sp[i]);
        return ary;
    }

    int[] toIntArray() throws Exception
    {
        String[] sp = split();
        int n = sp.length;
        int[] ary = new int[n];
        for(int i = 0; i < n; i++) ary[i] = Integer.valueOf(sp[i]);
        return ary;
    }

    String reverse(String str)
    {
        return new StringBuilder(str).reverse().toString();
    }


    long pow(long a, int pow)
    {
        long res = 1;
        while(pow > 0)
        {
            if(pow % 2 != 0) {
                res *= a;
                res %= mod;
            }
            a = a * a;
            a %= mod;
            pow /= 2;
        }

        return res;
    }

    public static void main(String[] args) throws Exception
    {
        int _k = Integer.valueOf("1");
        if(test > 0) in = new BufferedReader(new InputStreamReader(System.in));
        else in = new BufferedReader(new FileReader(file));
        if(test < 0) {String[] str = in.readLine().split(" ");
        char[] gg = in.readLine().toCharArray();}
/***********************************************************************/
/***********************************************************************/
/***********************************************************************/
/***********************************************************************/
        // System.out.println((-100 + 0) / 2);

        // char[] c = new char[4];
        // c[1] = '.';
        // for(char i = '0'; i <= '9'; i++)
        // {
        //     for(char j = '0'; j <= '9'; j++)
        //     {
        //         for(char k = '0'; k <= '9'; k++)
        //         {
        //             c[0] = i;
        //             c[2] = j;
        //             c[3] = k;
        //             double d = Double.valueOf(String.valueOf(c));
        //             int v = (int)(d * 100);
        //             String s1 = String.valueOf("" + c[0] + c[2] + c[3]),
        //                 s2 = String.valueOf(v);
        //             System.out.printf("%s %s %b\n", s1, s2, s1.equals(s2));
        //         }
        //     }
        // }


        new Main().c();
        out.flush();
    }
    void f() throws Exception
    {
        int k = readInt();
        String s = in.readLine();
        int n = s.length();
        long prod = 1;
        int mod = 1_000_000_007;
        for(int i = 0; i < k; i++)
        {
            n++;
            prod = (prod * 26 * n) % mod;
        }
        System.out.println(prod);

    }

    void e() throws Exception
    {
        int[] ary = toIntArray();
        int n = ary[0], k = ary[1];
        ary = toIntArray();
        int lo = 1, hi = 1 << 30;
        while(lo < hi)
        {
            int m = (lo + hi) / 2;
            int cut = numCut(ary, m);
            // System.out.printf("%d %d\n", m, cut);
            if(cut > k) lo = m + 1;
            else hi = m;
        }
        out.println(lo);
    }

    int numCut(int[] ary, int thres)
    {
        int count = 0;
        for(int v : ary)
        {
            if(v > thres)
            count += v / thres;
            // count += v % thres > 0 ? 1 : 0;
        }
        return count;
    }

    void d() throws Exception
    {
        int n = readInt();
        char[] c = in.readLine().toCharArray();
        int i = 0, j = n - 1, re = 0;
        while(i < j)
        {
            while(i < j && c[i] == 'R') i++;
            while(i < j && c[j] == 'W') j--;
            if(i < j)
            {
                swap(c, i, j);
                i++;
                j--;
                re++;
            }
        }
        out.println(re);
    }



    void c() throws Exception
    {
        int n = readInt();
        int[] ary = toIntArray();
        long sum = 0;
        int mini = 0;
        for(int i = 0; i < n; i++)
        {
            if(ary[i] < mini) sum += mini - ary[i];
            mini = Math.max(ary[i], mini);
        }
        out.println(sum);
        
    }

    int gcd(int a, int b)
    {
        if(b == 0) return a;
         return gcd(b, a % b);
    }

    void b() throws Exception
    {
        String str = in.readLine();
        int sum = 0;
        for(char c : str.toCharArray())
        {
            int val = c - '0';
            sum += val;
            sum %= 9;
        }
        if(sum == 0) out.println("Yes");
        else out.println("No");
        
    }

    boolean transform(String s1, String s2)
    {
        // from s1 -> s2
        int m = s1.length(), n = s2.length();
        if(m < n) return false;
        int j = n - 1;
        for(int i = m - 1; i >=0; i--)
        {
            if(s1.charAt(i) == s2.charAt(j)) j--;
            if(j < 0) return true;
        }
        return false;
    }

    void a() throws Exception
    {
        int[] ary = toIntArray();
        int n = ary[0], x = ary[1], t = ary[2];
        int mult = n / x;
        if(n % x != 0) mult++;
        out.println(mult * t);
        
    }
} public class Main { 
  
    static BufferedReader in;
    static PrintWriter out = new PrintWriter(System.out);
    static String file = "../in";
    static int test = 10; // 0 for local testing, 1 for std input
    static int inf = 1_000_000, mod = 1_000_000_007;



    void swap(int[]ary, int i, int j)
    {
        int t = ary[i];
        ary[i] = ary[j];
        ary[j] = t;
    }

    void swap(char[]ary, int i, int j)
    {
        char t = ary[i];
        ary[i] = ary[j];
        ary[j] = t;
    }
    
    String[] split() throws Exception
    {
        return in.readLine().split(" ");
    }

    int readInt() throws Exception
    {
        return Integer.valueOf(in.readLine());
    }

    long readLong() throws Exception
    {
        return Long.valueOf(in.readLine());
    }

    long[] toLongArray() throws Exception
    {
        String[] sp = split();
        int n = sp.length;
        long[] ary = new long[n];
        for(int i = 0; i < n; i++) ary[i] = Long.valueOf(sp[i]);
        return ary;
    }

    int[] toIntArray() throws Exception
    {
        String[] sp = split();
        int n = sp.length;
        int[] ary = new int[n];
        for(int i = 0; i < n; i++) ary[i] = Integer.valueOf(sp[i]);
        return ary;
    }

    String reverse(String str)
    {
        return new StringBuilder(str).reverse().toString();
    }


    long pow(long a, int pow)
    {
        long res = 1;
        while(pow > 0)
        {
            if(pow % 2 != 0) {
                res *= a;
                res %= mod;
            }
            a = a * a;
            a %= mod;
            pow /= 2;
        }

        return res;
    }

    public static void main(String[] args) throws Exception
    {
        int _k = Integer.valueOf("1");
        if(test > 0) in = new BufferedReader(new InputStreamReader(System.in));
        else in = new BufferedReader(new FileReader(file));
        if(test < 0) {String[] str = in.readLine().split(" ");
        char[] gg = in.readLine().toCharArray();}
/***********************************************************************/
/***********************************************************************/
/***********************************************************************/
/***********************************************************************/
        // System.out.println((-100 + 0) / 2);

        // char[] c = new char[4];
        // c[1] = '.';
        // for(char i = '0'; i <= '9'; i++)
        // {
        //     for(char j = '0'; j <= '9'; j++)
        //     {
        //         for(char k = '0'; k <= '9'; k++)
        //         {
        //             c[0] = i;
        //             c[2] = j;
        //             c[3] = k;
        //             double d = Double.valueOf(String.valueOf(c));
        //             int v = (int)(d * 100);
        //             String s1 = String.valueOf("" + c[0] + c[2] + c[3]),
        //                 s2 = String.valueOf(v);
        //             System.out.printf("%s %s %b\n", s1, s2, s1.equals(s2));
        //         }
        //     }
        // }


        new Main().c();
        out.flush();
    }
    void f() throws Exception
    {
        int k = readInt();
        String s = in.readLine();
        int n = s.length();
        long prod = 1;
        int mod = 1_000_000_007;
        for(int i = 0; i < k; i++)
        {
            n++;
            prod = (prod * 26 * n) % mod;
        }
        System.out.println(prod);

    }

    void e() throws Exception
    {
        int[] ary = toIntArray();
        int n = ary[0], k = ary[1];
        ary = toIntArray();
        int lo = 1, hi = 1 << 30;
        while(lo < hi)
        {
            int m = (lo + hi) / 2;
            int cut = numCut(ary, m);
            // System.out.printf("%d %d\n", m, cut);
            if(cut > k) lo = m + 1;
            else hi = m;
        }
        out.println(lo);
    }

    int numCut(int[] ary, int thres)
    {
        int count = 0;
        for(int v : ary)
        {
            if(v > thres)
            count += v / thres;
            // count += v % thres > 0 ? 1 : 0;
        }
        return count;
    }

    void d() throws Exception
    {
        int n = readInt();
        char[] c = in.readLine().toCharArray();
        int i = 0, j = n - 1, re = 0;
        while(i < j)
        {
            while(i < j && c[i] == 'R') i++;
            while(i < j && c[j] == 'W') j--;
            if(i < j)
            {
                swap(c, i, j);
                i++;
                j--;
                re++;
            }
        }
        out.println(re);
    }



    void c() throws Exception
    {
        int n = readInt();
        int[] ary = toIntArray();
        long sum = 0;
        int mini = 0;
        for(int i = 0; i < n; i++)
        {
            if(ary[i] < mini) sum += mini - ary[i];
            mini = Math.max(ary[i], mini);
        }
        out.println(sum);
        
    }

    int gcd(int a, int b)
    {
        if(b == 0) return a;
         return gcd(b, a % b);
    }

    void b() throws Exception
    {
        String str = in.readLine();
        int sum = 0;
        for(char c : str.toCharArray())
        {
            int val = c - '0';
            sum += val;
            sum %= 9;
        }
        if(sum == 0) out.println("Yes");
        else out.println("No");
        
    }

    boolean transform(String s1, String s2)
    {
        // from s1 -> s2
        int m = s1.length(), n = s2.length();
        if(m < n) return false;
        int j = n - 1;
        for(int i = m - 1; i >=0; i--)
        {
            if(s1.charAt(i) == s2.charAt(j)) j--;
            if(j < 0) return true;
        }
        return false;
    }

    void a() throws Exception
    {
        int[] ary = toIntArray();
        int n = ary[0], x = ary[1], t = ary[2];
        int mult = n / x;
        if(n % x != 0) mult++;
        out.println(mult * t);
        
    }
} Main
import java.math.*;
import java.util.*;
import java.io.*;
  
public class Main { 
  
    static BufferedReader in;
    static PrintWriter out = new PrintWriter(System.out);
    static String file = "../in";
    static int test = 10; // 0 for local testing, 1 for std input
    static int inf = 1_000_000, mod = 1_000_000_007;



    void swap(int[]ary, int i, int j)
    {
        int t = ary[i];
        ary[i] = ary[j];
        ary[j] = t;
    }

    void swap(char[]ary, int i, int j)
    {
        char t = ary[i];
        ary[i] = ary[j];
        ary[j] = t;
    }
    
    String[] split() throws Exception
    {
        return in.readLine().split(" ");
    }

    int readInt() throws Exception
    {
        return Integer.valueOf(in.readLine());
    }

    long readLong() throws Exception
    {
        return Long.valueOf(in.readLine());
    }

    long[] toLongArray() throws Exception
    {
        String[] sp = split();
        int n = sp.length;
        long[] ary = new long[n];
        for(int i = 0; i < n; i++) ary[i] = Long.valueOf(sp[i]);
        return ary;
    }

    int[] toIntArray() throws Exception
    {
        String[] sp = split();
        int n = sp.length;
        int[] ary = new int[n];
        for(int i = 0; i < n; i++) ary[i] = Integer.valueOf(sp[i]);
        return ary;
    }

    String reverse(String str)
    {
        return new StringBuilder(str).reverse().toString();
    }


    long pow(long a, int pow)
    {
        long res = 1;
        while(pow > 0)
        {
            if(pow % 2 != 0) {
                res *= a;
                res %= mod;
            }
            a = a * a;
            a %= mod;
            pow /= 2;
        }

        return res;
    }

    public static void main(String[] args) throws Exception
    {
        int _k = Integer.valueOf("1");
        if(test > 0) in = new BufferedReader(new InputStreamReader(System.in));
        else in = new BufferedReader(new FileReader(file));
        if(test < 0) {String[] str = in.readLine().split(" ");
        char[] gg = in.readLine().toCharArray();}
/***********************************************************************/
/***********************************************************************/
/***********************************************************************/
/***********************************************************************/
        // System.out.println((-100 + 0) / 2);

        // char[] c = new char[4];
        // c[1] = '.';
        // for(char i = '0'; i <= '9'; i++)
        // {
        //     for(char j = '0'; j <= '9'; j++)
        //     {
        //         for(char k = '0'; k <= '9'; k++)
        //         {
        //             c[0] = i;
        //             c[2] = j;
        //             c[3] = k;
        //             double d = Double.valueOf(String.valueOf(c));
        //             int v = (int)(d * 100);
        //             String s1 = String.valueOf("" + c[0] + c[2] + c[3]),
        //                 s2 = String.valueOf(v);
        //             System.out.printf("%s %s %b\n", s1, s2, s1.equals(s2));
        //         }
        //     }
        // }


        new Main().c();
        out.flush();
    }
    void f() throws Exception
    {
        int k = readInt();
        String s = in.readLine();
        int n = s.length();
        long prod = 1;
        int mod = 1_000_000_007;
        for(int i = 0; i < k; i++)
        {
            n++;
            prod = (prod * 26 * n) % mod;
        }
        System.out.println(prod);

    }

    void e() throws Exception
    {
        int[] ary = toIntArray();
        int n = ary[0], k = ary[1];
        ary = toIntArray();
        int lo = 1, hi = 1 << 30;
        while(lo < hi)
        {
            int m = (lo + hi) / 2;
            int cut = numCut(ary, m);
            // System.out.printf("%d %d\n", m, cut);
            if(cut > k) lo = m + 1;
            else hi = m;
        }
        out.println(lo);
    }

    int numCut(int[] ary, int thres)
    {
        int count = 0;
        for(int v : ary)
        {
            if(v > thres)
            count += v / thres;
            // count += v % thres > 0 ? 1 : 0;
        }
        return count;
    }

    void d() throws Exception
    {
        int n = readInt();
        char[] c = in.readLine().toCharArray();
        int i = 0, j = n - 1, re = 0;
        while(i < j)
        {
            while(i < j && c[i] == 'R') i++;
            while(i < j && c[j] == 'W') j--;
            if(i < j)
            {
                swap(c, i, j);
                i++;
                j--;
                re++;
            }
        }
        out.println(re);
    }



    void c() throws Exception
    {
        int n = readInt();
        int[] ary = toIntArray();
        long sum = 0;
        int mini = 0;
        for(int i = 0; i < n; i++)
        {
            if(ary[i] < mini) sum += mini - ary[i];
            mini = Math.max(ary[i], mini);
        }
        out.println(sum);
        
    }

    int gcd(int a, int b)
    {
        if(b == 0) return a;
         return gcd(b, a % b);
    }

    void b() throws Exception
    {
        String str = in.readLine();
        int sum = 0;
        for(char c : str.toCharArray())
        {
            int val = c - '0';
            sum += val;
            sum %= 9;
        }
        if(sum == 0) out.println("Yes");
        else out.println("No");
        
    }

    boolean transform(String s1, String s2)
    {
        // from s1 -> s2
        int m = s1.length(), n = s2.length();
        if(m < n) return false;
        int j = n - 1;
        for(int i = m - 1; i >=0; i--)
        {
            if(s1.charAt(i) == s2.charAt(j)) j--;
            if(j < 0) return true;
        }
        return false;
    }

    void a() throws Exception
    {
        int[] ary = toIntArray();
        int n = ary[0], x = ary[1], t = ary[2];
        int mult = n / x;
        if(n % x != 0) mult++;
        out.println(mult * t);
        
    }
}