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