#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long t = 1;
  while (t--) {
    long long n;
    cin >> n;
    vector<long long> v(n);
    for (auto &x : v) cin >> x;
    long long maxx = v[0];
    long long ans = 0;
    for (long long i = 0; i < n - 1; i++) {
      if (v[i] < maxx) {
        ans += maxx - v[i];
      } else
        maxx = v[i];
    }
    cout << ans << "\n";
  }
  return 0;
}
