#include <bits/stdc++.h>
using namespace std;
int main() {
  double N;
  cin >> N;
  vector<double> A(N);
  for (double i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  double ans = 0;
  double maxnum = 0;
  for (double i = 0; i < N; i++) {
    maxnum = max(maxnum, A.at(i));
    double bi = maxnum;
    ans += (bi - A.at(i));
  }
  cout << ans << endl;
}
