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