#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, sum;
  cin >> N;
  vector<int> A(N);
  vector<int> S(N);
  sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    S.at(i) = A.at(i);
  }
  sort(A.begin(), A.end());
  sort(S.begin(), S.end(), greater<int>());
  for (int i = 0; i < N; i++) {
    sum = sum + A.at(i) * abs(S.at(i) - A.at(i));
  }
  cout << sum;
}
