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