//package main;

import java.io.*;
import java.util.*;
import java.math.*;

public final class Main {
    BufferedReader br;
    StringTokenizer stk;
    
    public static void main(String[] args) throws Exception {
        new Main().run();
    }
    
    {
        stk = null;
        br = new BufferedReader(new InputStreamReader(System.in));
    }
    
    long mod = 998244353;
    StringBuilder res = new StringBuilder(1000005);
    void run() throws Exception {
        int n = ni();
        long[] a = new long[n];
        long ans = 0;
        for(int i = 0; i < n; i++) {
            a[i] = nl();
        }
        for(int i = 1; i < n; i++) {
            long diff = a[i] - a[i - 1];
            if(diff < 0) {
                ans += -diff;
                a[i] += -diff;
            }
        }
        System.out.println(ans);
    }
    
    //Reader & Writer
    String nextToken() throws Exception {
        if (stk == null || !stk.hasMoreTokens())
            stk = new StringTokenizer(br.readLine(), " ");
        return stk.nextToken();
    }

    String nt() throws Exception {
        return nextToken();
    }

    int ni() throws Exception {
        return Integer.parseInt(nextToken());
    }

    long nl() throws Exception {
        return Long.parseLong(nextToken());
    }
    
    double nd() throws Exception {
        return Double.parseDouble(nextToken());
    }
    
    //Some Misc methods
    int get(int l, int r, int[] a) {
        return l == 0 ? a[r] : a[r] - a[l - 1];
    }
    
    void shuffle(long[] a) {
        Random r = new Random();
        for(int i = 0; i < a.length; i++) {
            int idx = r.nextInt(a.length);
            long temp = a[i];
            a[i] = a[idx];
            a[idx] = temp;
        }
    }
    
    void reverse(int[] a) {
        for(int i = 0, j = a.length - 1; i < j; i++, j--) {
            int temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
    }
} import java.io.*; io.* java import java.util.*; util.* java import java.math.*; math.* java public final class Main {
    BufferedReader br;
    StringTokenizer stk;
    
    public static void main(String[] args) throws Exception {
        new Main().run();
    }
    
    {
        stk = null;
        br = new BufferedReader(new InputStreamReader(System.in));
    }
    
    long mod = 998244353;
    StringBuilder res = new StringBuilder(1000005);
    void run() throws Exception {
        int n = ni();
        long[] a = new long[n];
        long ans = 0;
        for(int i = 0; i < n; i++) {
            a[i] = nl();
        }
        for(int i = 1; i < n; i++) {
            long diff = a[i] - a[i - 1];
            if(diff < 0) {
                ans += -diff;
                a[i] += -diff;
            }
        }
        System.out.println(ans);
    }
    
    //Reader & Writer
    String nextToken() throws Exception {
        if (stk == null || !stk.hasMoreTokens())
            stk = new StringTokenizer(br.readLine(), " ");
        return stk.nextToken();
    }

    String nt() throws Exception {
        return nextToken();
    }

    int ni() throws Exception {
        return Integer.parseInt(nextToken());
    }

    long nl() throws Exception {
        return Long.parseLong(nextToken());
    }
    
    double nd() throws Exception {
        return Double.parseDouble(nextToken());
    }
    
    //Some Misc methods
    int get(int l, int r, int[] a) {
        return l == 0 ? a[r] : a[r] - a[l - 1];
    }
    
    void shuffle(long[] a) {
        Random r = new Random();
        for(int i = 0; i < a.length; i++) {
            int idx = r.nextInt(a.length);
            long temp = a[i];
            a[i] = a[idx];
            a[idx] = temp;
        }
    }
    
    void reverse(int[] a) {
        for(int i = 0, j = a.length - 1; i < j; i++, j--) {
            int temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
    }
} Main BufferedReader br; br StringTokenizer stk; stk long mod = 998244353; mod 998244353 StringBuilder res = new StringBuilder(1000005); res new StringBuilder(1000005) public static void main(String[] args) throws Exception {
        new Main().run();
    } main {
        new Main().run();
    } new Main().run() new Main().run new Main() String[] args args void run() throws Exception {
        int n = ni();
        long[] a = new long[n];
        long ans = 0;
        for(int i = 0; i < n; i++) {
            a[i] = nl();
        }
        for(int i = 1; i < n; i++) {
            long diff = a[i] - a[i - 1];
            if(diff < 0) {
                ans += -diff;
                a[i] += -diff;
            }
        }
        System.out.println(ans);
    } run {
        int n = ni();
        long[] a = new long[n];
        long ans = 0;
        for(int i = 0; i < n; i++) {
            a[i] = nl();
        }
        for(int i = 1; i < n; i++) {
            long diff = a[i] - a[i - 1];
            if(diff < 0) {
                ans += -diff;
                a[i] += -diff;
            }
        }
        System.out.println(ans);
    } int n = ni(); n ni() ni long[] a = new long[n]; a new long[n] n long ans = 0; ans 0 for(int i = 0; i < n; i++) {
            a[i] = nl();
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            a[i] = nl();
        } {
            a[i] = nl();
        } a[i] = nl() a[i] a i nl() nl for(int i = 1; i < n; i++) {
            long diff = a[i] - a[i - 1];
            if(diff < 0) {
                ans += -diff;
                a[i] += -diff;
            }
        } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
            long diff = a[i] - a[i - 1];
            if(diff < 0) {
                ans += -diff;
                a[i] += -diff;
            }
        } {
            long diff = a[i] - a[i - 1];
            if(diff < 0) {
                ans += -diff;
                a[i] += -diff;
            }
        } long diff = a[i] - a[i - 1]; diff a[i] - a[i - 1] a[i] a i a[i - 1] a i - 1 i 1 if(diff < 0) {
                ans += -diff;
                a[i] += -diff;
            } diff < 0 diff 0 {
                ans += -diff;
                a[i] += -diff;
            } ans += -diff ans -diff diff a[i] += -diff a[i] a i -diff diff System.out.println(ans) System.out.println System.out System System.out ans //Reader & Writer
    String nextToken() throws Exception {
        if (stk == null || !stk.hasMoreTokens())
            stk = new StringTokenizer(br.readLine(), " ");
        return stk.nextToken();
    } nextToken {
        if (stk == null || !stk.hasMoreTokens())
            stk = new StringTokenizer(br.readLine(), " ");
        return stk.nextToken();
    } if (stk == null || !stk.hasMoreTokens())
            stk = new StringTokenizer(br.readLine(), " "); stk == null || !stk.hasMoreTokens() stk == null stk null !stk.hasMoreTokens() stk.hasMoreTokens() stk.hasMoreTokens stk stk = new StringTokenizer(br.readLine(), " ") stk new StringTokenizer(br.readLine(), " ") return stk.nextToken(); stk.nextToken() stk.nextToken stk String nt() throws Exception {
        return nextToken();
    } nt {
        return nextToken();
    } return nextToken(); nextToken() nextToken int ni() throws Exception {
        return Integer.parseInt(nextToken());
    } ni {
        return Integer.parseInt(nextToken());
    } return Integer.parseInt(nextToken()); Integer.parseInt(nextToken()) Integer.parseInt Integer nextToken() nextToken long nl() throws Exception {
        return Long.parseLong(nextToken());
    } nl {
        return Long.parseLong(nextToken());
    } return Long.parseLong(nextToken()); Long.parseLong(nextToken()) Long.parseLong Long nextToken() nextToken double nd() throws Exception {
        return Double.parseDouble(nextToken());
    } nd {
        return Double.parseDouble(nextToken());
    } return Double.parseDouble(nextToken()); Double.parseDouble(nextToken()) Double.parseDouble Double nextToken() nextToken //Some Misc methods
    int get(int l, int r, int[] a) {
        return l == 0 ? a[r] : a[r] - a[l - 1];
    } get {
        return l == 0 ? a[r] : a[r] - a[l - 1];
    } return l == 0 ? a[r] : a[r] - a[l - 1]; l == 0 ? a[r] : a[r] - a[l - 1] l == 0 l 0 a[r] a r a[r] - a[l - 1] a[r] a r a[l - 1] a l - 1 l 1 int l l int r r int[] a a void shuffle(long[] a) {
        Random r = new Random();
        for(int i = 0; i < a.length; i++) {
            int idx = r.nextInt(a.length);
            long temp = a[i];
            a[i] = a[idx];
            a[idx] = temp;
        }
    } shuffle {
        Random r = new Random();
        for(int i = 0; i < a.length; i++) {
            int idx = r.nextInt(a.length);
            long temp = a[i];
            a[i] = a[idx];
            a[idx] = temp;
        }
    } Random r = new Random(); r new Random() for(int i = 0; i < a.length; i++) {
            int idx = r.nextInt(a.length);
            long temp = a[i];
            a[i] = a[idx];
            a[idx] = temp;
        } int i = 0; int i = 0; i 0 i < a.length i a.length a a.length i++ i++ i {
            int idx = r.nextInt(a.length);
            long temp = a[i];
            a[i] = a[idx];
            a[idx] = temp;
        } {
            int idx = r.nextInt(a.length);
            long temp = a[i];
            a[i] = a[idx];
            a[idx] = temp;
        } int idx = r.nextInt(a.length); idx r.nextInt(a.length) r.nextInt r a.length a a.length long temp = a[i]; temp a[i] a i a[i] = a[idx] a[i] a i a[idx] a idx a[idx] = temp a[idx] a idx temp long[] a a void reverse(int[] a) {
        for(int i = 0, j = a.length - 1; i < j; i++, j--) {
            int temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
    } reverse {
        for(int i = 0, j = a.length - 1; i < j; i++, j--) {
            int temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
    } for(int i = 0, j = a.length - 1; i < j; i++, j--) {
            int temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        } int i = 0, j = a.length - 1; int i = 0 i 0 j = a.length - 1; j a.length - 1 a.length a a.length 1 i < j i j i++, j-- i++ i j-- j {
            int temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        } {
            int temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        } int temp = a[i]; temp a[i] a i a[i] = a[j] a[i] a i a[j] a j a[j] = temp a[j] a j temp int[] a a public final class Main {
    BufferedReader br;
    StringTokenizer stk;
    
    public static void main(String[] args) throws Exception {
        new Main().run();
    }
    
    {
        stk = null;
        br = new BufferedReader(new InputStreamReader(System.in));
    }
    
    long mod = 998244353;
    StringBuilder res = new StringBuilder(1000005);
    void run() throws Exception {
        int n = ni();
        long[] a = new long[n];
        long ans = 0;
        for(int i = 0; i < n; i++) {
            a[i] = nl();
        }
        for(int i = 1; i < n; i++) {
            long diff = a[i] - a[i - 1];
            if(diff < 0) {
                ans += -diff;
                a[i] += -diff;
            }
        }
        System.out.println(ans);
    }
    
    //Reader & Writer
    String nextToken() throws Exception {
        if (stk == null || !stk.hasMoreTokens())
            stk = new StringTokenizer(br.readLine(), " ");
        return stk.nextToken();
    }

    String nt() throws Exception {
        return nextToken();
    }

    int ni() throws Exception {
        return Integer.parseInt(nextToken());
    }

    long nl() throws Exception {
        return Long.parseLong(nextToken());
    }
    
    double nd() throws Exception {
        return Double.parseDouble(nextToken());
    }
    
    //Some Misc methods
    int get(int l, int r, int[] a) {
        return l == 0 ? a[r] : a[r] - a[l - 1];
    }
    
    void shuffle(long[] a) {
        Random r = new Random();
        for(int i = 0; i < a.length; i++) {
            int idx = r.nextInt(a.length);
            long temp = a[i];
            a[i] = a[idx];
            a[idx] = temp;
        }
    }
    
    void reverse(int[] a) {
        for(int i = 0, j = a.length - 1; i < j; i++, j--) {
            int temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
    }
} public final class Main {
    BufferedReader br;
    StringTokenizer stk;
    
    public static void main(String[] args) throws Exception {
        new Main().run();
    }
    
    {
        stk = null;
        br = new BufferedReader(new InputStreamReader(System.in));
    }
    
    long mod = 998244353;
    StringBuilder res = new StringBuilder(1000005);
    void run() throws Exception {
        int n = ni();
        long[] a = new long[n];
        long ans = 0;
        for(int i = 0; i < n; i++) {
            a[i] = nl();
        }
        for(int i = 1; i < n; i++) {
            long diff = a[i] - a[i - 1];
            if(diff < 0) {
                ans += -diff;
                a[i] += -diff;
            }
        }
        System.out.println(ans);
    }
    
    //Reader & Writer
    String nextToken() throws Exception {
        if (stk == null || !stk.hasMoreTokens())
            stk = new StringTokenizer(br.readLine(), " ");
        return stk.nextToken();
    }

    String nt() throws Exception {
        return nextToken();
    }

    int ni() throws Exception {
        return Integer.parseInt(nextToken());
    }

    long nl() throws Exception {
        return Long.parseLong(nextToken());
    }
    
    double nd() throws Exception {
        return Double.parseDouble(nextToken());
    }
    
    //Some Misc methods
    int get(int l, int r, int[] a) {
        return l == 0 ? a[r] : a[r] - a[l - 1];
    }
    
    void shuffle(long[] a) {
        Random r = new Random();
        for(int i = 0; i < a.length; i++) {
            int idx = r.nextInt(a.length);
            long temp = a[i];
            a[i] = a[idx];
            a[idx] = temp;
        }
    }
    
    void reverse(int[] a) {
        for(int i = 0, j = a.length - 1; i < j; i++, j--) {
            int temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
    }
} Main
//package main;

import java.io.*;
import java.util.*;
import java.math.*;

public final class Main {
    BufferedReader br;
    StringTokenizer stk;
    
    public static void main(String[] args) throws Exception {
        new Main().run();
    }
    
    {
        stk = null;
        br = new BufferedReader(new InputStreamReader(System.in));
    }
    
    long mod = 998244353;
    StringBuilder res = new StringBuilder(1000005);
    void run() throws Exception {
        int n = ni();
        long[] a = new long[n];
        long ans = 0;
        for(int i = 0; i < n; i++) {
            a[i] = nl();
        }
        for(int i = 1; i < n; i++) {
            long diff = a[i] - a[i - 1];
            if(diff < 0) {
                ans += -diff;
                a[i] += -diff;
            }
        }
        System.out.println(ans);
    }
    
    //Reader & Writer
    String nextToken() throws Exception {
        if (stk == null || !stk.hasMoreTokens())
            stk = new StringTokenizer(br.readLine(), " ");
        return stk.nextToken();
    }

    String nt() throws Exception {
        return nextToken();
    }

    int ni() throws Exception {
        return Integer.parseInt(nextToken());
    }

    long nl() throws Exception {
        return Long.parseLong(nextToken());
    }
    
    double nd() throws Exception {
        return Double.parseDouble(nextToken());
    }
    
    //Some Misc methods
    int get(int l, int r, int[] a) {
        return l == 0 ? a[r] : a[r] - a[l - 1];
    }
    
    void shuffle(long[] a) {
        Random r = new Random();
        for(int i = 0; i < a.length; i++) {
            int idx = r.nextInt(a.length);
            long temp = a[i];
            a[i] = a[idx];
            a[idx] = temp;
        }
    }
    
    void reverse(int[] a) {
        for(int i = 0, j = a.length - 1; i < j; i++, j--) {
            int temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
    }
}