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