#include <bits/stdc++.h>
int main(void) {
  int i;
  int N;
  long long int ans, front;
  int *A;
  char buf[128], *ch;
  char *great_buf;
  fgets(buf, sizeof(buf), stdin);
  ch = strtok(buf, " ");
  N = atoi(ch);
  A = (int *)calloc(N, sizeof(int));
  great_buf = (char *)calloc(N * 11, sizeof(char));
  fgets(great_buf, sizeof(buf), stdin);
  ch = strtok(great_buf, " ");
  A[0] = atoi(ch);
  for (i = 1; i < N; i += 1) {
    ch = strtok(NULL, " ");
    A[i] = atoi(ch);
  }
  free(great_buf);
  ans = 0;
  front = (long long int)A[0];
  for (i = 1; i < N; i += 1) {
    if (A[i] < front) {
      ans += front - (long long int)A[i];
    } else {
      front = (long long int)A[i];
    }
  }
  free(A);
  printf("%lld\n", ans);
  return 0;
}
