#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (int &i : v) {
    cin >> i;
  }
  int sum = 0;
  int mx = v[0];
  for (int i = 1; i < n; i++) {
    if (v[i] < mx) {
      sum += mx - v[i];
    }
    if (v[i] > mx) {
      mx = v[i];
    }
  }
  cout << sum << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  solve();
  return 0;
}
