#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> v;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    v.push_back(x);
  }
  int ans = 0;
  int curr_max = v[0];
  for (int i = 1; i < n; ++i) {
    if (v[i] < curr_max) {
      ans += curr_max - v[i];
    } else {
      curr_max = v[i];
    }
  }
  cout << ans << endl;
  return 0;
}
