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