#include #include #include using namespace std; const int N = 100000; int y[N],n; long long int cv = 0,ans=-1; int bfr = 1, nxt; int main() { cin >> n; nxt = n - 1; for (int i = 0; i < n; i++)cin >> y[i]; sort(y, y + n); for (int i = 0; i < n; i++)cv += y[i] - y[0]; ans = cv; for (int i = 1; i < n; i++) { cv = cv + bfr * (y[i] - y[i - 1]) - nxt * (y[i] - y[i - 1]); ans = min(ans, cv); bfr++; nxt--; } cout << ans << endl; return 0; }