#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n;
  cin >> n;
  vector<long long> a(n);
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long ans = 0;
  for (long long i = 1; i < n; i++) {
    long long tmp = max(ll(0), a[i] - a[i - 1]);
    ans += tmp;
    a[i] += tmp;
  }
  cout << ans << "\n";
  return 0;
}
