#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[n];
  int maxv = 0;
  for (int i = 0; i < n; i++) cin >> a[i];
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (maxv <= a[i]) {
      maxv = max(maxv, a[i]);
    } else {
      ans += maxv - a[i];
    }
  }
  cout << ans << endl;
  return 0;
}
