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