#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <queue>
#include <cstring>

using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using PP = pair<ll, pair<ll,ll>>;
#define rep(i,n) for (ll i = 0; i < (n); ++i)

ll n, a, t, ans;

int main() {
    cin >> n;
    t = 0;
    ans = 0;
    rep(i,n){
        cin >> a;
        ll add = max(0,t-a);
        t = add+a;
        ans += add;
    }
    cout << ans << endl;
    return 0;
}
