#include <bits/stdc++.h>
int main() {
  double a[2 * 10000 + 10];
  int i = 0, n, max = 0, cou = 0;
  scanf("%d", &n);
  for (i; i < n; i++) {
    scanf("%f", &a[i]);
    if (a[i] > max) {
      max = a[i];
    } else if (a[i] < max) {
      cou = cou + (int)(max - a[i]);
    }
  }
  printf("%d", cou);
}
