#include <bits/stdc++.h>
int xmax(int a, int b) {
  if (a > b) {
    return a;
  } else {
    return b;
  }
}
int main(void) {
  int n;
  int sum = 0;
  scanf("%d", &n);
  int arr[n];
  int max = 0;
  int pos = 0;
  int i = 0;
  for (; i < n; i++) {
    scanf("%d", &arr[i]);
  }
  int mx = 0;
  for (i = 0; i < n; i++) {
    mx = xmax(mx, arr[i]);
    sum += mx - arr[i];
  }
  printf("%d", sum);
  return 0;
}
