#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n;
  cin >> n;
  long long a[n];
  for (long long i = 0; i < n; i++) cin >> a[i];
  long long cnt = 0, second = a[0];
  for (long long i = 1; i < n; i++) {
    if (a[i] < second) cnt += second - a[i];
    second = a[i];
  }
  cout << cnt;
  cout << '\n';
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long _ = 1;
  for (long long i = 1; i <= _; i++) {
    solve();
  }
  return 0;
}
