#include <bits/stdc++.h>
int main() {
  int n;
  scanf("%d", &n);
  long long int a[n + 1], ans = 0;
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
  }
  long long int tall;
  tall == a[0];
  for (int i = 1; i < n; i++) {
    if (tall > a[i]) {
      ans += tall - a[i];
    } else {
      tall == a[i];
    }
  }
  printf("%lld", ans);
  return 0;
}
