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