#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  int n, x;
  vector<int> v;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    v.push_back(x);
  }
  int sum = 0;
  for (int i = 1; i < n; i++) {
    if (v[i] < v[i - 1]) {
      int dif = v[i - 1] - v[i];
      sum += dif;
      v[i] += dif;
    }
  }
  cout << sum << '\n';
  return 0;
}
