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