#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int& ele : a) cin >> ele;
  int ans = 0;
  for (int i = 1; i < n; ++i) {
    if (a[i - 1] > a[i]) {
      ans += (a[i - 1] - a[i]);
      a[i] = (a[i - 1] - a[i]) + a[i];
    }
  }
  cout << ans;
  return 0;
}
