#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const long long MOD = 1e9 + 7;
const long long int INF = 1LL << 60;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long int ans = 0;
  for (int i = 1; i < n; i++) {
    if (a[i - 1] > a[i])
      ;
    {
      ans += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
  cout << ans << endl;
}
