#include <bits/stdc++.h>
int main() {
  int sum = 0;
  int *A;
  int N = 0;
  int i;
  scanf("%d", &N);
  A = (int *)malloc(N * sizeof(int));
  for (i = 1; i <= N; i++) {
    scanf("%d", &A[i]);
  }
  for (i = 1; i < N; i++) {
    if (A[i] > A[i + 1]) {
      sum = sum + (A[i] - A[i + 1]);
      A[i + 1] = A[i + 1] + (A[i] - A[i + 1]);
    }
  }
  printf("%d", sum);
  free(A);
  return 0;
}
