#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  int ar[n], i, m, ans = 0;
  for (i = 0; i < n; i++) {
    cin >> v.at(i);
  }
  int l = n;
  vector<int>::iterator it;
  it = v.end() - 1;
  while (l--) {
    m = max_element(v.begin(), it) - v.begin();
    if (*it < v[m]) {
      ans = ans + (v[m] - *it);
    }
    it--;
  }
  cout << ans;
  return 0;
}
