#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9;
const long long inf = 1e16;
void solve() {
  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 x = max(0ll, a[i] - a[i - 1]);
    ans += x;
    a[i] += x;
  }
  cout << ans << endl;
}
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  solve();
}
