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