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