#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;
}
const int mod = 1000000007, MAX = 200005, INF = 1 << 30;
int main() {
  long long n;
  long long a[n];
  cin >> n;
  for (int i = 0; i < (n); i++) {
    cin >> a[i];
  }
  long long ans = 0;
  long long high = a[0];
  for (int i = 1; i < n; i++) {
    if (high > a[i]) {
      ans += high - a[i];
    } else if (high < a[i]) {
      high = a[i];
    }
  }
  cout << ans << endl;
}
