#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<long long> s(n);
  for (int(i) = (int)(0); (i) < (int)(n); ++(i)) cin >> s[i];
  long long add = 0, pre = 0;
  for (int(i) = (int)(0); (i) < (int)(n - 1); ++(i)) {
    long long d = (s[i + 1] + add) - (s[i] + pre);
    if (d < 0) add += -d;
    pre = add;
  }
  cout << add << endl;
  return 0;
}
