#include <bits/stdc++.h>
int main(void) {
  int n, j, sum = 0;
  long long a[200007];
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
  }
  for (int i = 0; i < n - 1; i++) {
    if (a[i] > a[i + 1]) {
      j = a[i] - a[i + 1];
      a[i + 1] = a[i + 1] + j;
      sum = sum + j;
    }
  }
  printf("%d", sum);
  return 0;
}
