#include <bits/stdc++.h>
using namespace std;
 
namespace cp {
    #define pb push_back
    #define ins insert
    #define sz(x) (int)(x).size()
    #define all(x) (x).begin(), (x).end()
    typedef long long ll;
    typedef vector<int> vi;
    
    const ll INF = 9223372036854775807;
    const ll MOD = 1000000000 + 7;
 
    // to_string methods
    string ts(int n) { return to_string(n); }
    string ts(double n) { return to_string(n); }
    string ts(long double n) { return to_string(n); }
    string ts(long long n) { return to_string(n); }
    string ts(char c) { return to_string(c); }
    string ts(bool b) { return b ? "true" : "false"; }
    string ts(string s) { return s; }
 
    template <typename T, typename V>
    string ts(map<T,V> lastIndex);
 
    template<typename Container>
    string ts(const Container &v);
 
    template <typename T>
    string ts(T arr[], int len);
    
    template <typename T, typename V>
    string ts(pair<T,V> p) {
        return "{" + ts(p.first) + ", " + ts(p.second) + "}";
    }
    
    template <typename T>
    string ts(T arr[], int len) {
        ostringstream oss;
        oss << "[";
        string delim = "";
        for (int i = 0; i < len; i++) {
     	  oss << delim << ts(arr[i]);
     	  delim = ", ";
        }
        oss << "]";
        return oss.str();
    }
    
    template<typename Container>
    string ts(const Container &v) {
        ostringstream oss;
        oss << "[";
        string delim = "";
        for (const auto &x : v) {
     	  oss << delim << ts(x);
     	  delim = ", ";
        }
        oss << "]";
        return oss.str();
    }
    
    template <typename T, typename V>
    string ts(map<T,V> lastIndex) {
        ostringstream oss;
        oss << "[";
        string delim = "";
        for (pair<T,V> p : lastIndex) {
     	  oss << delim;
     	  oss << "{" << ts(p.first) << ": " << ts(p.second) << "}";
     	  delim = ", ";
        }
        oss << "]";
        return oss.str();
    }
    
    // Hashing algorithm https://codeforces.com/blog/entry/62393
    struct custom_hash {
 	   static uint64_t splitmix64(uint64_t x) {
 	       // http://xorshift.di.unimi.it/splitmix64.c
 	       x += 0x9e3779b97f4a7c15;
 	       x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
 	       x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
 	       return x ^ (x >> 31);
 	   }
 	  
 	   size_t operator()(uint64_t x) const {
           return splitmix64(x + (chrono::steady_clock::now().time_since_epoch().count()));
       }
    };
    
    // IO
    void setI(string s) { freopen(s.c_str(), "r", stdin); }
    void setO(string s) { freopen(s.c_str(), "w", stdout); }
    void setIO(string s1, string s2) { setI(s1); setO(s2); }
    void setIO() { setI("input.txt"); setO("output.txt"); }
    void setUsacoIO(string s) { setIO(s + ".in", s + ".out"); }
    
    // Sorting lambda syntax
    // auto cmp = [&](int x, int y) {
    //     return x < y;
    // };
 
   // sort(it1, it2, cmp);
    // set<int, decltype(cmp)> s(cmp); // same for map
    // priority_queue<int, vector<int>, decltype(cmp)> pq(cmp); // remember pq.top() is the largest by default (descending order)
}
using namespace cp;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
    
    int n;
    cin >> n;
    
    int arr[n];
    for (int i = 0; i < n; i++) cin >> arr[i];
    
    ll ret = 0;
    for (int i = 0; i < n - 1; i++) {
    	ret += max(0, arr[i] - arr[i + 1]);
    	arr[i + 1] += max(0, arr[i] - arr[i + 1]);
	}
	
	cout << ret << endl;

    return 0;
}


Measured memory_bytes: 4376