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