#include <bits/stdc++.h>
using namespace std;
int main() {
  int i, count = 0;
  long long N, max = 0;
  vector<long long> A(200000);
  cin >> N;
  for (i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  max = A.at(0);
  for (i = 1; i < N; i++) {
    if (A.at(i) > max) {
      max = A.at(i);
    } else {
      count += max - A.at(i);
    }
  }
  cout << count;
}
