#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e16;
const ll mod = 1000000007;
int main() {
  ll n;
  cin >> n;
  vector<ll> v(n);
  for (ll i = 0; i < (ll)(n); i++) cin >> v.at(i);
  ll res = 0;
  for (ll i = 0; i < (ll)(n); i++) {
    if (i == 0) continue;
    if (v.at(i - 1) < v.at(i)) {
      res += v.at(i) - v.at(i - 1);
      v.at(i) = v.at(i - 1);
    }
  }
  cout << res << endl;
}
