#include <bits/stdc++.h>
int main() {
  int N, i;
  long int ans, in, high;
  scanf("%d\n", &N);
  scanf("%ld", &high);
  for (i = 0; i < N - 1; i++) {
    scanf("%ld", &in);
    if (high > in) {
      ans += (high - in);
    } else {
      high = in;
    }
  }
  printf("%ld", ans);
  return 0;
}
