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