#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
  int am = 0;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (am > a[i]) {
      ans += (am - a[i]);
    } else {
      am = a[i];
    }
  }
  cout << ans << endl;
  return 0;
}
