#include <bits/stdc++.h>
using namespace std;
int main() {
  double n = 0;
  cin >> n;
  vector<double> data(n);
  for (int i = 0; i < n; i++) {
    cin >> data.at(i);
  }
  double ans = 0;
  double max = data.at(0);
  for (int i = 1; i < n; i++) {
    if (max < data.at(i)) {
      max = data.at(i);
    } else {
      ans += max - data.at(i);
    }
  }
  cout << ans << endl;
}
