Language: C++


#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (ll (i) = 0 ; (i) < (ll)(n) ; ++(i))
#define REPN(i, m, n) for (ll (i) = m ; (i) < (ll)(n) ; ++(i))
#define REP_REV(i, n) for (ll (i) = (ll)(n) - 1 ; (i) >= 0 ; --(i))
#define REPN_REV(i, m, n) for (ll (i) = (ll)(n) - 1 ; (i) >= m ; --(i))

#define INF             2e9
#define INF_LL          1LL<<60
#define ll              long long
#define Ceil(x, n)      (((((x))+((n)-1))/n))
#define CeilN(x, n)     (((((x))+((n)-1))/n)*n)
#define FloorN(x, n)    ((x)-(x)%(n))
#define ALL(x)          (x).begin(),(x).end()
typedef pair<ll, ll>    P;

int main() {
    int n;
    cin >> n;
    vector<int> a(n);
    REP(i, n) cin >> a[i];
    sort(ALL(a));

    ll idx = 1, idx2 = n-2;
    ll ans = abs(a[0] - a[n-1]);
    ll hidari = a[0], migi = a[n-1];
    REP(i, n-2) {
        if(i % 2 == 0) {
            if(abs(a[idx] - hidari) < abs(a[idx] - migi)) {
                ans += abs(a[idx] - migi);
                migi = a[idx];
            } else {
                ans += abs(a[idx] - hidari);
                hidari = a[idx];
            }
            idx++;
        } else {
            if(abs(a[idx2] - hidari) < abs(a[idx2] - migi)) {
                ans += abs(a[idx2] - migi);
                migi = a[idx2];
            } else {
                ans += abs(a[idx2] - hidari);
                hidari = a[idx2];
            }
            idx2--;
        }
        //cout << hidari << " " << migi << " " << ans << endl;
    }
    idx = n-2, idx2 = 1;    
    ll tmp = abs(a[0] - a[n-1]);
    hidari = a[n-1], migi = a[0];
    REP(i, n-2) {
        if(i % 2 == 0) {
            if(abs(a[idx] - hidari) < abs(a[idx] - migi)) {
                tmp += abs(a[idx] - migi);
                migi = a[idx];
            } else {
                tmp += abs(a[idx] - hidari);
                hidari = a[idx];
            }
            idx--;
        } else {
            if(abs(a[idx2] - hidari) < abs(a[idx2] - migi)) {
                tmp += abs(a[idx2] - migi);
                migi = a[idx2];
            } else {
                tmp += abs(a[idx2] - hidari);
                hidari = a[idx2];
            }
            idx2++;
        }
        //cout << hidari << " " << migi << " " << ans << endl;
    }

    ans = max(tmp, ans);

    idx = n-2, idx2 = 1;    
    ll tmp = abs(a[0] - a[n-1]);
    hidari = a[0], migi = a[n-1];
    REP(i, n-2) {
        if(i % 2 == 0) {
            if(abs(a[idx] - hidari) < abs(a[idx] - migi)) {
                tmp += abs(a[idx] - migi);
                migi = a[idx];
            } else {
                tmp += abs(a[idx] - hidari);
                hidari = a[idx];
            }
            idx--;
        } else {
            if(abs(a[idx2] - hidari) < abs(a[idx2] - migi)) {
                tmp += abs(a[idx2] - migi);
                migi = a[idx2];
            } else {
                tmp += abs(a[idx2] - hidari);
                hidari = a[idx2];
            }
            idx2++;
        }
        //cout << hidari << " " << migi << " " << ans << endl;
    }

    ans = max(tmp, ans);

    cout << ans << endl;

    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:80:8: error: redeclaration of 'long long int tmp'
   80 |     ll tmp = abs(a[0] - a[n-1]);
      |        ^~~
a.cc:52:8: note: 'long long int tmp' previously declared here
   52 |     ll tmp = abs(a[0] - a[n-1]);
      |        ^~~