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