#include <bits/stdc++.h>
using namespace std;
int f3() {
  int N;
  cin >> N;
  vector<int> v(N);
  for (int i = 0; i < N; i++) cin >> v[i];
  int sum = 0;
  int maxv = v[0];
  for (int i = 1; i < N; i++) {
    if (v[i] < maxv) {
      sum += maxv - v[i];
    } else {
      maxv = max(maxv, v[i]);
    }
  }
  cout << sum << endl;
  return 0;
}
int main() {
  f3();
  return 0;
}
