#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int n, sofar, curr = 0, ans = 0;
  cin >> n;
  vector<long long int> v(n);
  for (long long int i = 0; i < n; ++i) cin >> v[i];
  sofar = v[0];
  for (long long int i = 1; i <= n - 1; ++i)
    if (v[i - 1] > v[i]) ans += v[i - 1] - v[i];
  cout << ans;
}
