#include <bits/stdc++.h>
int main(void) {
  int N, i, s = 0, max = 0;
  long int A[200001];
  setbuf(stdout, NULL);
  scanf("%d", &N);
  for (i = 0; i < N; i++) {
    scanf("%ld", &A[i]);
    if (A[i] < max)
      s += max - A[i];
    else
      max = A[i];
  }
  printf("%d", s);
  return 0;
}
