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