#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
bool tests = false;
void solve() {
  int n;
  cin >> n;
  vi a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  int maxi = a[0];
  int ct = 0;
  for (int i = 1; i < n; i++) {
    ct += max(0, maxi - a[i]);
    maxi = max(maxi, a[i]);
  }
  cout << ct << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  if (tests) {
    cin >> t;
  }
  for (int somethingStupid = (1); somethingStupid <= ((t)); somethingStupid++)
    solve();
  return 0;
}
