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