#include <bits/stdc++.h>
static inline int intmax(int a, int b) { return (a < b) ? b : a; }
static inline int nonneg(int a) { return (a < 0) ? 0 : a; }
int main(void) {
  int n = 0;
  scanf("%d", &n);
  int max = 0;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int ai = 0;
    scanf("%d", &ai);
    max = intmax(max, ai);
    ans += nonneg(max - ai);
  }
  printf("%d\n", ans);
  return 0;
}
