#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, x, ans = 0, maxm = 0;
  vector<int> a;
  cin >> n;
  for (int i = 0; i < (n); i++) {
    cin >> x;
    if (maxm < x)
      maxm = x;
    else
      ans += (maxm - x);
  }
  cout << ans << endl;
}
int main() {
  solve();
  return 0;
}
