#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (auto& e : a) cin >> e;
  int r = 0;
  for (int i = 1; i < n; i++) {
    int d = max(0, a[i - 1] - a[i]);
    a[i] += d;
    r += d;
  }
  cout << r << endl;
}
