#include <bits/stdc++.h>
using namespace std;
long long a[200005];
int main(void) {
  long long n, mx = 0, sum = 0;
  scanf("%lld", &n);
  for (long long i = 1; i <= n; i++) scanf("%lld", &a[i]);
  mx = a[1];
  for (long long i = 2; i <= n; i++) {
    if (mx < a[i]) mx = a[i];
    sum += mx - a[i];
  }
  printf("%d", sum);
  return 0;
}
