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