#include using namespace std; using ll = long long; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector y(n); for (int i = 0; i < n; i++) cin >> y[i]; sort(y.begin(), y.end()); int cnt = 0; ll tmp = 0; ll ans = 0; for (int i = 0; i < n; i++) { if (cnt == 0) { cnt = 1; tmp = y[i]; } else { ll v1 = abs(y[i] - tmp); if (cnt == 1) { ans += v1; tmp = y[i]; cnt++; continue; } if (i == n - 1) { ans += v1; cnt++; continue; } ll v2 = abs(y[i + 1] - y[i]); if (v1 < v2) { ans += v1; cnt++; } else { cnt = 1; tmp = y[i]; } } } cout << ans << endl; return 0; }