#include <bits/stdc++.h>
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;
using ll = long long;
const ll MOD = 1000000007;
const ll INF = MOD * MOD;
const int inf = (1 << 29);
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < (n); i++) cin >> a[i];
  int res = 0;
  int highest = a[0];
  for (int i = 1; i < n; i++) {
    if (a[i] < highest) {
      res += highest - a[i];
      highest = highest;
    } else {
      highest = a[i];
    }
  }
  cout << res << endl;
  return 0;
}
