#include <bits/stdc++.h>
using namespace std;
void keisan(vector<int> &heights, int &i, int &sum, int &maximum) {
  if (maximum > heights.at(i)) {
    sum += maximum - heights.at(i);
  } else if (maximum < heights.at(i)) {
    maximum = heights.at(i);
  }
}
int main() {
  int n, sum = 0, maximum;
  cin >> n;
  vector<int> heights(n);
  for (int i = 0; i < n; i++) {
    cin >> heights.at(i);
    if (i == 0) {
      maximum = heights.at(0);
    } else {
      keisan(heights, i, sum, maximum);
    }
  }
  cout << sum << endl;
}
