#include <bits/stdc++.h>
const int MAXN = 2e5 + 5;
int n;
long long ans = 0, maxn = 0, a[MAXN];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
  }
  maxn = -1;
  for (int i = 1; i <= n; i++) {
    maxn = std::max(maxn, a[i]);
    ans += fabs(maxn - a[i]);
  }
  printf("%lld", ans);
  return 0;
}
