#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  int A[200000] = {0};
  int sum = 0;
  int tmp = 0;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  tmp = A[0];
  for (int i = 0; i < N; i++) {
    if (tmp > A[i]) {
      sum += tmp - A[i];
    } else if (tmp < A[i]) {
      tmp = A[i];
    }
  }
  cout << sum << "\n";
  return 0;
}
