#include <bits/stdc++.h>
using ll = long long;
using namespace std;
void solve() {}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll n;
  vector<ll> a(n);
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    if (i == 0) continue;
    if (a[i - 1] > a[i]) {
      ans += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
  cout << ans;
  return 0;
}
