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