#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
int main() {
  int n;
  cin >> n;
  int t;
  cin >> t;
  int ans = 0;
  int mi = t;
  for (int i = 1; i < n; i++) {
    cin >> t;
    mi = max(mi, t);
    if (mi > t) {
      ans += mi - t;
    }
  }
  cout << ans;
  return 0;
}
