#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  int64_t a[100001], b[100001], c, d, e, f, m, n, p, x, y, z, t, i, j, k, s = 0;
  cin >> t;
  for (i = 0; i < t; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(b, b + t);
  for (i = 0; i < t; i++) {
    if (a[i] < b[i])
      s = s + b[i] - a[i];
    else if (a[i] == b[i])
      s = s + b[t - 1] - a[i];
  }
  cout << s << "\n";
}
