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