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