#include <bits/stdc++.h>
int main(int argc, char const *argv[]) {
  int c = 0;
  long int N, a, p;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &a);
    i || (p = a);
    i && (p > a) && (c += p - a);
    p = a > p ? a : p;
  }
  printf("%ld\n", c);
  return 0;
}
