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