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