#include <bits/stdc++.h>
int main(void) {
  int a[200001] = {0}, b = 0, c = 0, n, i;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (a[i] <= a[i - 1]) {
      b = a[i - 1] - a[i];
      a[i] = a[i - 1];
      c += b;
    }
  }
  printf("%d", c);
  return 0;
}
