#include <bits/stdc++.h>
long long int max(long long int a, long long int b) {
  if (a > b)
    return a;
  else
    return b;
}
int main(void) {
  int i, N, count;
  long long int sum, A[200001];
  char S[4], prev;
  scanf("%d", &N);
  for (i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  sum = 0;
  for (i = 1; i < N; i++) {
    sum += max(0, A[i - 1] - A[i]);
    A[i] = max(A[i - 1], A[i]);
  }
  printf("%lldd", sum);
  return 0;
}
