#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  vector<long long> v(n);
  cin >> n;
  for (long long i = 0; i < n; i++) cin >> v[i];
  unsigned long long result = 0;
  long long mx = 0;
  for (long long i = 0; i < n; i++) {
    mx = max(v[i], mx);
    result += mx - v[i];
  }
  cout << result;
}
