#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long int n;
  long long int sum = 0;
  cin >> n;
  vector<long long int> v(n);
  for (auto &x : v) cin >> x;
  for (auto i = 0; i < v.size(); i++) {
    if (v[i] > v[i + 1]) {
      sum += v[i] - v[i + 1];
      v[i + 1] = v[i];
    } else
      continue;
  }
  cout << sum;
}
int main() {
  solve();
  return 0;
}
