#include <bits/stdc++.h>
using namespace std;
void solver() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int &x : a) cin >> x;
  multiset<int> ms;
  for (int x : a) ms.insert(x);
  int ans = 0;
  for (int i = n - 1; i >= 0; --i) {
    int now = a[i];
    auto it = ms.rbegin();
    ans += (*it) - now;
    ms.erase(ms.find(now));
  }
  cout << ans << '\n';
}
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(NULL);
  int t = 1;
  while (t--) solver();
  return 0;
}
