#include <bits/stdc++.h>
using namespace std;
int main() {
  unsigned long int n, max = 0, min = 99999999, sum = 0;
  cin >> n;
  vector<unsigned long int> a(n);
  for (unsigned long int i = 0; i < n; i++) {
    cin >> a.at(i);
    if (a.at(i) > max) max = a.at(i);
    if (a.at(i) < min) min = a.at(i);
  }
  for (unsigned long int i = 0; i < n; i++) {
    if ((max - a.at(i)) < (a.at(i) - min)) {
      sum += max - a.at(i);
    } else {
      sum += a.at(i) - min;
    }
  }
  cout << sum << endl;
}
