#include <bits/stdc++.h>
int main() {
  int n, i, tool = 0;
  scanf("%d", &n);
  long a[n], r;
  for (i = 0; i < n; i++) scanf("%ld ", &a[i]);
  for (i = 1; i < n; i++) {
    if (a[i] >= a[i - 1])
      continue;
    else {
      r = a[i - 1] - a[i];
      a[i] += r;
      tool += r;
    }
  }
  printf("%d", tool);
}
