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