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