#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a[200001], sum = 0, mmax;
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  mmax = a[0];
  for (int i = 1; i < n; i++) {
    if (a[i] > mmax) {
      mmax = a[i];
    } else
      sum += mmax - a[i];
  }
  cout << sum << endl;
  return 0;
}
