#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pint = pair<int, int>;
const ull MAX = 1e19;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (ll i = (0); i < (ll)(n); i++) cin >> a[i];
  int ans = 0;
  int ma = a[0];
  for (ll i = (1); i < (ll)(n); i++) {
    if (a[i] < ma) ans += ma - a[i];
    ma = max(ma, a[i]);
  }
  cout << ans << endl;
  return 0;
}
