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