#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (auto& x : a) {
    cin >> x;
  }
  int mx = a[0], ans = 0;
  for (int i = 1; i < n; i++) {
    if (a[i] < mx)
      ans += (mx - a[i]);
    else
      mx = a[i];
  }
  cout << ans << '\n';
  return 0;
}
