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