#include "bits/stdc++.h"
using namespace std;

#define ASC 0
#define DESC 1


#define f first
#define s second
#define pb push_back
#define ppb pop_back
#define mp make_pair

#define fr(i, a, b) for (int i = a; i <= b; i++)
#define rfr(i, a, b) for (int i = b; i >= a; i--)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<char> vc;
typedef vector<float> vf;
typedef vector<double> vd;
typedef map<string, int> msi;
typedef map<int, int> mii;
typedef set<int> si;
typedef unordered_set<int> usi;
typedef unordered_multiset<int> umsi;
typedef multiset<int> mlsi;
typedef set<string> ss;
typedef pair<int, int> pi;

class Utl {
public:
    template<class mixed>
    static void print(mixed value) {
        std::cout << value;
    }

    template<class mixed, class padd>
    static void print(mixed prefix, padd suffix) {
        std::cout  << prefix << suffix;
    }
    
    template<class mixed>
    static void print(mixed value, bool space) {
        if (space) std::cout << value << " ";
        else std::cout << value;
    }
    
    
    template<class mixed>
    static void println(mixed value) {
        std::cout << value << std::endl;
    }
    
    static void println() {
        std::cout << endl;
    }

    
    template<class mixed>
    static void log(vector<mixed> arr, bool nl = false) {
        fr(i, 0, arr.size() - 1) Utl::print(arr[i], " ");
        if (nl) Utl::println();
    }
    
    template<class mixed>
    static void log(mixed arr[], int len, bool nl = false) {
        fr(i, 0, len - 1) Utl::print(arr[i], " ");
        if (nl) Utl::println();
    }
    
    template<class mixed>
    static vector<mixed> sort(vector<mixed> vec, int dir = ASC) {
        if (dir == DESC) std::sort(vec.begin(), vec.end(), greater<mixed>());
        else std::sort(vec.begin(), vec.end());
        return vec;
    }
    
    static vi sort(vi vec, std::function<bool(int, int)> dir) {
        std::sort(vec.begin(), vec.end(), dir);
        return vec;
    }

    static vector<char> split(string str) {
        vector<char> vec;
        for (int i = 0; i < str.size(); i++) vec.pb(str[i]);
        return vec;
    }
    
    template<class mixed>
    static mixed min(vector<mixed> vec) {
        return *min_element(vec.begin(), vec.end());
    }
    
    template<class mixed>
    static mixed max(vector<mixed> vec) {
        return *max_element(vec.begin(), vec.end());
    }
    
    template<class mixed>
    static mixed sum(vector<mixed> vec) {
        return accumulate(vec.begin(), vec.end(), 0);
    }
    
    template<class mixed>
    static vector<mixed> reverse(vector<mixed> vec) {
        std::reverse(vec.begin(), vec.end());
        return vec;
    }
    
    /**
     every, some methods
     */
    template<class mixed>
    static bool every(vector<mixed>vec, std::function<bool(const mixed)> callback) {
        return all_of(vec.begin(), vec.end(), callback);
    }
    
    template<class mixed>
    static bool some(vector<mixed>vec, std::function<bool(const mixed)> callback) {
        return any_of(vec.begin(), vec.end(), callback);
    }
    
    template<class mixed>
    static vector<mixed> map(vector<mixed> vec, std::function<mixed(const mixed)> callback) {
        vector<mixed> ret;
        for (auto &el: vec) ret.pb(callback(el));
        return ret;
    }
    
    template<class mixed>
    static vector<mixed> filter(vector<mixed> vec, std::function<mixed(const mixed)> callback) {
        vector<mixed> ret;
        for (auto &el: vec) {
            if (callback(el)) ret.pb(el);
        }
        return ret;
    }
    
    static string toString(int num) {
        return to_string(num);
    }
    
    static int parseInt(string num) {
        return stoi(num);
    }
};

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

int main() {
    
    ios::sync_with_stdio(false);
    cin.tie(0);
   
    int n;
    cin >> n;
    vi vec;
    
    fr(i, 0, n-1) {
        int x;
        cin >> x;
        vec.pb(x);
    }
    
    ll ans = 0;
    fr(i, 1, n - 1) {
        if (vec[i] < vec[i-1]) {
            ans += vec[i-1] - vec[i];
            vec[i] = vec[i-1];
        }
    }
    
    Utl::println(ans);
    
    return 0;
}



Measured memory_bytes: 4264