#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 5;
const int MXN = 2e5 + 5;
const int MOD = 1e9 + 7;
void solve() {
  long long n;
  cin >> n;
  long long a[n];
  for (long long i = 0; i < (n); i++) cin >> a[i];
  long long m = 0, ans = 0;
  for (long long i = (1); i <= (n - 1); i++) {
    if (a[i - 1] >= a[i]) {
      ans += abs(a[m] - a[i]);
    }
    if (a[i] > a[m]) m = i;
  }
  cout << ans << "\n";
}
int main() {
  long long t;
  t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
