#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, max = 0, sum = 0;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < (int)(n); i++) {
    cin >> a.at(i);
  }
  for (int i = 0; i < (int)(n); i++) {
    if (a.at(i) > max) {
      max = a.at(i);
    }
    if (a.at(i) < max) {
      sum += (max - a.at(i));
    }
  }
  cout << sum << endl;
}
