Language: C++

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

typedef long long ll;
const int MOD = 1e9 + 7;
typedef vector<ll> vll;

static int speedUp = []() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    return 0;
}();

int main() {
    int n;
    cin >> n;
    vll nums(n);
    for (int i = 0; i < n; ++i) {
        cin >> nums[i];
    }
    sort(nums.begin(), nums.end());
    vll t1;
    vll t2;
    ll ans = 0;
    t1.push_back(nums[0]);
    t2.push_back(nums[0]);
    int l = 1;
    int r = nums.size() - 1;
    bool right = true;
    while (l <= r) {
        if (right) {
            if (t1.size() == t2.size() && l == r) {
                if (t1.back() < t2.back()) {
                    t1.push_back(nums[r--]);
                } else {
                    t2.push_back(nums[r--]);
                }
            } else {
                t1.push_back(nums[r--]);
                t2.push_back(nums[r--]);
            }
            right = false;
        } else {
            if (t1.size() == t2.size() && l == r) {
                if (t1.back() > t2.back()) {
                    t1.push_back(nums[l++]);
                } else {
                    t2.push_back(nums[l++]);
                }
            } else {
                t1.push_back(nums[l++]);
                t2.push_back(nums[l++]);
            }
            right = true;
        }
    }
    for (int i = 0; i < t1.size() - 1; ++i) {
        ans += abs(t1[i] - t1[i + 1]);
    }
    for (int i = 0; i < t2.size() - 1; ++i) {
        ans += abs(t2[i] - t2[i + 1]);
    }
    reverse(nums.begin(), nums.end());
    ll ans1 = 0;
    t1.clear();
    t2.clear();
    t1.push_back(nums[0]);
    t2.push_back(nums[0]);
    int l = 1;
    int r = nums.size() - 1;
    bool right = true;
    while (l <= r) {
        if (right) {
            if (t1.size() == t2.size() && l == r) {
                if (t1.back() > t2.back()) {
                    t1.push_back(nums[r--]);
                } else {
                    t2.push_back(nums[r--]);
                }
            } else {
                t1.push_back(nums[r--]);
                t2.push_back(nums[r--]);
            }
            right = false;
        } else {
            if (t1.size() == t2.size() && l == r) {
                if (t1.back() < t2.back()) {
                    t1.push_back(nums[l++]);
                } else {
                    t2.push_back(nums[l++]);
                }
            } else {
                t1.push_back(nums[l++]);
                t2.push_back(nums[l++]);
            }
            right = true;
        }
    }
    for (int i = 0; i < t1.size() - 1; ++i) {
        ans1 += abs(t1[i] - t1[i + 1]);
    }
    for (int i = 0; i < t2.size() - 1; ++i) {
        ans1 += abs(t2[i] - t2[i + 1]);
    }
    ans = max(ans, ans1);
    cout << ans << endl;
    return 0;

}

Compiler output:
a.cc: In function 'int main()':
a.cc:70:9: error: redeclaration of 'int l'
   70 |     int l = 1;
      |         ^
a.cc:28:9: note: 'int l' previously declared here
   28 |     int l = 1;
      |         ^
a.cc:71:9: error: redeclaration of 'int r'
   71 |     int r = nums.size() - 1;
      |         ^
a.cc:29:9: note: 'int r' previously declared here
   29 |     int r = nums.size() - 1;
      |         ^
a.cc:72:10: error: redeclaration of 'bool right'
   72 |     bool right = true;
      |          ^~~~~
a.cc:30:10: note: 'bool right' previously declared here
   30 |     bool right = true;
      |          ^~~~~