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