#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < (int)(n); i++) cin >> a.at(i);
  long long min_val = a.at(0);
  long long max_val = 0;
  int flag = 0;
  for (int i = 0; i < (int)(n); i++) {
    min_val = max(min_val, a.at(i));
    if (min_val > a.at(i)) {
      flag += min_val - a.at(i);
    }
  }
  cout << flag;
}
