#include <bits/stdc++.h>
using namespace std;
vector<long long> v;
long long sum = 0;
long long bc(long long ed) {
  long long mx = INT_MIN;
  long long id;
  for (long long i = 0; i <= ed; i++) {
    if (v[i] > mx) {
      mx = v[i];
      id = i;
    }
  }
  return id;
}
void mc(long long ed) {
  long long ct = v[ed];
  for (long long i = ed; i < v.size(); i++) {
    if (v[i] > ct) break;
    sum += ct - v[i];
    v[i] = ct;
  }
}
int main() {
  long long n;
  cin >> n;
  v.resize(n);
  for (long long i = 0; i < n; i++) cin >> v[i];
  long long id = n;
  while (id != 0) {
    id = bc(id - 1);
    mc(id);
  }
  mc(id);
  cout << sum << endl;
}
