#include <bits/stdc++.h>
int main(void) {
  long N;
  long A[32768];
  long max = 0;
  long long sum = 0;
  int i;
  scanf("%ld", &N);
  for (i = 0; i < N; i++) scanf("%ld", &A[i]);
  for (i = 0; i < N; i++) {
    if (A[i] < max)
      sum += max - A[i];
    else
      max = A[i];
  }
  printf("%lld\n", sum);
  return 0;
}
