#include <bits/stdc++.h>
using namespace std;
int main() {
  long int n, a, b, sum = 0;
  cin >> n;
  cin >> b;
  for (int i = 1; i < n; i++) {
    cin >> a;
    if (b > a) {
      sum += b - a;
    } else {
      b = a;
    }
  }
  printf("%d\n", sum);
  return 0;
}
