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