#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  scanf("%d", &n);
  vector<int> v(n), p(n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &v[i]);
    p[i] = v[i];
    if (i > 0) p[i] = max(p[i - 1], p[i]);
  }
  long long ans = 0;
  for (int i = n - 1; i > 0; i--) {
    ans += p[i] - v[i];
  }
  printf("%I64\n", ans);
  return 0;
}
