#include <bits/stdc++.h>
using namespace std;
using VI = vector<int>;
using VB = vector<bool>;
using VS = vector<string>;
using graph = vector<vector<int>>;
int main() {
  int n, ans = 0;
  cin >> n;
  VI a(n + 1);
  a.at(0) = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a.at(i);
    if (a.at(i) < a.at(i - 1)) {
      ans = ans + a.at(i - 1) - a.at(i);
      a.at(i) = a.at(i - 1);
    }
  }
  cout << ans << endl;
  return 0;
}
