#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  if (n == 1) {
    cout << 0;
    return 0;
  }
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int tam = a[0], rs = 0;
  for (int i = 1; i < n; i++) {
    if (a[i] < tam)
      rs += (tam - a[i]);
    else
      tam = a[i];
  }
  cout << rs;
  return 0;
}
