#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int n, ans = 0, i, t;
  cin >> n;
  vector<long long> a(n);
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (i = 1; i < n; i++) {
    if (a[i - 1] >= a[i]) {
      t = a[i - 1] - a[i];
      ans += t;
      a[i] += t;
    }
  }
  cout << ans << endl;
  return 0;
}
