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