#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using PP = pair<ll, pair<ll, ll>>;
int n, a, t, ans;
int main() {
  cin >> n;
  t = 0;
  ans = 0;
  for (ll i = 0; i < (n); ++i) {
    cin >> a;
    int add = max(0, t - a);
    t = add + a;
    ans += add;
  }
  cout << ans << endl;
  return 0;
}
