#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define chmax(x, y) { x = max(x, y); }
#define chmin(x, y) { x = min(x, y); }

int main() {
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i, n){
        cin >> a.at(i);
    }
    ll ans = 0;
    int maxnum = 0;
    rep(i, n){
        chmax(maxnum, a.at(i));
        ans += maxnum - a.at(i);
    }
    cout << min << endl;
    return 0;
}