#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a[200000], mx = 0, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    int amt = mx - a[i];
    if (amt > 0) ans += amt;
    mx = max(mx, a[i]);
  }
  cout << ans << "\n";
}
