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