#include <bits/stdc++.h>
int main(void) {
  int n, a, i, s = 0, max = 0;
  setbuf(stdout, NULL);
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &a);
    if (a < max)
      s += max - a;
    else
      max = a;
  }
  printf("%d", s);
  return 0;
}
