#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long n;
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  long long ans = 0;
  for (int i = 0; i < n - 1; i++) {
    long long more = 0;
    if (a[i] > a[i + 1]) {
      more = abs(a[i] - a[i + 1]);
      a[i + 1] += more;
      ans += more;
    }
    cerr << "["
         << "a[i]"
            ": "
         << (a[i])
         << "] "
            "["
         << "a[i + 1]"
            ": "
         << (a[i + 1])
         << "] "
            "["
         << "more"
            ": "
         << (more)
         << "] "
            "["
         << "ans"
            ": "
         << (ans) << "] "
         << "\n";
  }
  cout << ans << "\n";
  return 0;
}
