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