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