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