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