#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, plus = 0;
  cin >> N;
  if (N == 1) cout << 0 << endl;
  else {
    vector<int> vec(N);
    for (int i = 0; i < N; i++) {
      cin >> vec.at(i);
    }
    for (int i = 0; i < N - 1; i++) {
      if (vec.at(i) > vec.at(i + 1)) {
        plus += vec.at(i) - vec.at(i + 1);
        vec.at(i + 1) = vec.at(i);
      }
    }
    cout << plus << endl;
}
