#include <bits/stdc++.h>
int main(void) {
  int i = 0, a[1000] = {0}, j[1000] = {0}, tya = 0, t = 0, b = 0;
  scanf("%d", &b);
  for (i = 0; i < b; i++) {
    scanf("%d", &a[i]);
  }
  j[0] = 0;
  i = 0;
  t = 1;
  while (i != b) {
    if (a[i] <= a[i + 1]) {
      j[t] = 0;
      i++;
      t++;
    } else {
      j[t] = a[i] - a[i + 1];
      a[i + 1] = a[i + 1] + j[t];
      i++;
      t++;
    }
  }
  t = 0;
  while (t != b) {
    tya = tya + j[t];
    t++;
  }
  printf("%d\n", tya);
  return 0;
}
