#include <bits/stdc++.h>
int main() {
  int N;
  scanf("%d%*c", &N);
  char *s = (char *)malloc(11 * N * sizeof(char));
  fgets(s, 11 * N, stdin);
  unsigned long int max = 0;
  unsigned long int sum = 0;
  for (int i = 0; i < N; i++) {
    int n;
    unsigned long int a;
    sscanf(s, "%ld %n", &a, &n);
    if (max > a) {
      sum += max - a;
    } else {
      max = a;
    }
    s = &s[n];
  }
  printf("%ld\n", sum);
  return 0;
}
