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