#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  for (long long i = 0; i < (int)(n); i++) cin >> a[i];
  vector<ll> dp(n, 0);
  for (long long i = 1; i < n; i++) {
    if (a[i - 1] > a[i]) {
      dp[i] += dp[i - 1] + (a[i - 1] - a[i]);
    }
  }
  cout << fixed << accumulate(dp.begin(), dp.end(), 0LL) << endl;
}
