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