#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
long long MOD = 1000000007;
int main() {
  int n;
  cin >> n;
  int a[n];
  int maximum = 0;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maximum = max(maximum, a[i]);
    if (maximum != a[i]) {
      sum += maximum - a[i];
    }
  }
  cout << sum << endl;
  return 0;
}
