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