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