#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  int n;
  cin >> n;
  int ans = 0, p;
  cin >> p;
  for (int i = 1; i < n; i++) {
    int q;
    cin >> q;
    if (q < p)
      ans += p - q;
    else
      p = q;
  }
  cout << ans << endl;
  return 0;
}
