#include <bits/stdc++.h>
const long long INF = 1LL << 60;
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;
}
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> moji(n);
  for (int i = 0; i < n; i++) cin >> moji.at(i);
  int counter = 0;
  for (int i = 1; i <= n - 1; i++) {
    if (moji.at(i - 1) <= moji.at(i)) {
      continue;
    } else {
      counter += moji.at(i - 1) - moji.at(i);
      moji.at(i) = moji.at(i - 1);
    }
  }
  cout << counter << endl;
}
