#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int i;
  int cnt = 0;
  vector<int> a(n);
  long long max = 0;
  for (i = 0; i < n; i++) cin >> a[i];
  for (i = 0; i < n; i++) {
    if (max <= a[i])
      max = a[i];
    else
      cnt += max - a[i];
  }
  cout << cnt << "\n";
  return 0;
}
