#include <bits/stdc++.h>
int W_Length(char *str) {
  int length = 0;
  while (*str++ != '\0') length++;
  return length;
}
void swap(long long int *a, long long int *b) {
  int temp;
  temp = *a;
  *a = *b;
  *b = temp;
}
double deg(double theta) { return theta * (2 * 3.14159265359) / 360; }
long long int cmp(const void *p, const void *q) {
  return *(long long int *)p - *(long long int *)q;
}
int cmp_s(const void *left, const void *right) {
  char *left_char = (char *)left;
  char *right_char = (char *)right;
  return strcmp(left_char, right_char);
}
long long int GCD(long long int a, long long int b) {
  long long int L = a, S = b;
  while (1) {
    if (S > L) {
      L = S + L;
      S = L - S;
      L = L - S;
    }
    if (L % S == 0) {
      return S;
    }
    L = L % S;
  }
}
long long int LCM(long long int a, long long int b) {
  return a * b / (GCD(a, b));
}
int main(void) {
  long long int n, i, min, k, a[210000], max, j = 1, count = 0;
  scanf("%lld", &n);
  scanf("%lld", &a[0]);
  max = a[0];
  for (i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
    if (a[i] > max) {
      max = a[i];
      continue;
    }
    count += max - a[i];
  }
  printf("%lld", count);
  return 0;
}
