#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  double t, nowmax = 0, ans = 0;
  vector<double> v1;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> t;
    v1.push_back(t);
  }
  for (int i = 0; i < v1.size(); i++) {
    if (v1[i] > nowmax)
      nowmax = v1[i];
    else
      ans += nowmax - v1[i];
  }
  cout << ans;
}
