#include <bits/stdc++.h>
using namespace std;
long long ans;
int main() {
  long long i, s, last, n, cha = 0;
  scanf("%lld %lld", &n, &s);
  last = s;
  for (i = 1; i < n; i++) {
    scanf("%lld", &s);
    cha = max(cha, last - s);
    ans += cha;
    last = s;
  }
  printf("%lld", ans);
  return 0;
}
