#include <bits/stdc++.h>

using namespace std;

template<typename S, typename T>
ostream& operator<<(ostream& out, const pair<S, T> p) {
    out << "(" << p.first << "," << p.second << ")";
    return out;
}

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& v) {
    for (auto a: v)
        out << a << " ";
    return out;
}

template<typename T>
ostream& operator<<(ostream& out, const set<T>& S) {
    for (auto a: S)
        cout << a << " ";
    return out;
}

template<typename T>
ostream& operator<<(ostream& out, const multiset<T>& S) {
    for (auto a: S)
        cout << a << " ";
    return out;
}

template<typename S, typename T>
ostream& operator<<(ostream& out, const map<S, T>& M) {
    for (auto m: M)
        cout << "(" << m.first << "->" << m.second << ") ";
    return out;
}

template<typename S, typename T>
pair<S, T> operator+(pair<S, T> a, pair<S, T> b) {
    return make_pair(a.first + b.first, a.second + b.second);
}

template<typename S, typename T>
pair<S, T> operator-(pair<S, T> a, pair<S, T> b) {
    return make_pair(a.first - b.first, a.second - b.second);
}

template<typename T>
struct Prefix {
    int n;
    vector<T> prefix_sum_val;
    vector<T> prefix_min_val;
    vector<T> suffix_min_val;
    vector<T> prefix_max_val;
    vector<T> suffix_max_val;
    const T inf = numeric_limits<T>::max();
    
    void buildup(vector<T>& A) {
        n = A.size();
        prefix_sum_val.resize(n);
        for (int i = 0; i < n; i++) {
            if (i == 0)
                prefix_sum_val[i] = A[i];
            else
                prefix_sum_val[i] = prefix_sum_val[i-1] + A[i];
        }
        prefix_min_val.resize(n);
        for (int i = 0; i < n; i++) {
            if (i == 0)
                prefix_min_val[i] = A[i];
            else
                prefix_min_val[i] = min(prefix_min_val[i-1], A[i]);
        }
        suffix_min_val.resize(n);
        for (int i = n - 1; i >= 0; i--) {
            if (i == n - 1)
                suffix_min_val[i] = A[i];
            else
                suffix_min_val[i] = min(suffix_min_val[i+1], A[i]);
        }
        prefix_max_val.resize(n);
        for (int i = 0; i < n; i++) {
            if (i == 0)
                prefix_max_val[i] = A[i];
            else
                prefix_max_val[i] = max(prefix_max_val[i-1], A[i]);
        }
        suffix_max_val.resize(n);
        for (int i = n - 1; i >= 0; i--) {
            if (i == n - 1)
                suffix_max_val[i] = A[i];
            else
                suffix_max_val[i] = max(suffix_max_val[i+1], A[i]);
        }
    }
    
    T query_sum_value(int x, int y) {
        T ans = prefix_sum_val[y];
        if (x)
            ans -= prefix_sum_val[x-1];
        return ans;
    }
    
    T query_minimum_value(int x, int y) {
        if (x == 0)
            return prefix_min_val[y];
        else if (y == n - 1)
            return suffix_min_val[x];
        return -inf;
    }
    
    T query_maximum_value(int x, int y) {
        if (x == 0)
            return prefix_max_val[y];
        else if (y == n - 1)
            return suffix_max_val[x];
        return -inf;
    }

    vector<T> unwind() {
        vector<T> A(n);
        for (int i = 0; i < n; i++)
            A[i] = query_sum_value(i, i);
        return A;
    }
};

Prefix<long long> prefix;

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int n;
    vector<long long> A;
    cin >> n;
    A.resize(n);
    for (int i = 0; i < n; i++)
        cin >> A[i];
    long long ans = 0;
    prefix.buildup(A);
    for (int i = 1; i < n; i++) {
        long long max_pre = prefix.query_maximum_value(0, i - 1);
        if (A[i] < max_pre)
            ans += max_pre - A[i];
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 12696