#include <bits/stdc++.h>
int main(void) {
  int N;
  scanf("%d", &N);
  int shun[N];
  for (int i = 0; i < N; i++) {
    scanf("%d", &shun[i]);
  }
  int sum = 0;
  int max = shun[0];
  for (int i = 0; i < N; i++) {
    if (shun[i] >= max) {
      sum += 0;
      max = shun[i];
    } else {
      sum += max - shun[i];
    }
  }
  printf("%d", sum);
}
