#include <bits/stdc++.h>
using namespace std;
using namespace std;
const long long mn = 2e5 + 10;
long long a[mn], b[mn];
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  for (long long i = 0; i < (n); i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(b, b + n);
  long long res = 0;
  for (long long i = 0; i < (n); i++) {
    long long ai = a[i], bi = b[i];
    if (ai > bi) continue;
    res += bi - ai;
  }
  cout << res << '\n';
}
