#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
  ll n;
  cin >> n;
  ll ans = 0;
  vector<ll> A(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  ll now = A[0];
  for (int i = 1; i < n; i++) {
    if (A[i] >= A[i - 1]) {
      now = A[i];
    } else {
      ans += now - A[i];
    }
  }
  cout << ans << "\n";
}
