int main(void) {
  int n;
  scanf("%d", &n);
  int people[n], total = 0;
  for (int i = 0; i < n; i++) {
  	scanf("%d", &people[i]);
  }
  
  for (int i = 1; i < n; i++) {
    int diff = people[i-1] - people[i];
    if (diff > 0) {
      people[i] = people[i-1];
      total += diff;
    }
  }
  
  printf("%d", total);
  return 0;
}