#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, mx = 0, sum = 0;
  cin >> n;
  int ar[n];
  for (int i = 0; i < n; i++) {
    cin >> ar[i];
  }
  for (size_t i = 0; i < n; i++) {
    mx = max(mx, ar[i]);
    if (mx > ar[i]) sum += mx - ar[i];
  }
  cout << sum << endl;
  return 0;
}
