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