#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll N, sum = 0;
  cin >> N;
  vector<ll> vec(N);
  for (int i = 0; i < (int)(N); i++) {
    cin >> vec.at(i);
  }
  if (N == 1) {
    cout << "0" << endl;
  } else {
    for (int i = 0; i < (int)(N - 1); i++) {
      if (vec.at(i) > vec.at(i + 1)) {
        sum += vec.at(i) - vec.at(i + 1);
        vec.at(i + 1) = vec.at(i);
      }
    }
  }
  cout << sum << endl;
}
