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