#include <bits/stdc++.h>
using namespace std;
constexpr long long mod = 1000000007;
constexpr long long INF = (1ll << 60);
constexpr double pi = 3.14159265358979323846;
template <typename T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T>
inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int main() {
  long long n;
  cin >> n;
  vector<long long> a(n);
  for (long long i = 0; i < n; ++i) cin >> a[i];
  long long sum = 0;
  for (long long i = 0; i < n - 1; i++) {
    if (a[i + 1] > a[i]) {
      sum += (a[i + 1] - a[i]);
      a[i] = a[i + 1];
    }
  }
  cout << sum << endl;
  return 0;
}
