#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve() {
  ll n, a, la, ans = 0;
  cin >> n;
  cin >> la;
  for (int i = 1; i < n; i++) {
    cin >> a;
    if (a < la) ans += la - a;
    la = a;
  }
  cout << ans << endl;
}
int main() {
  solve();
  return 0;
}
