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