Language: C++

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef long double ld;
const int inf=1e9+7;
const ll longinf=1LL<<60;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
#define F first
#define S second

const int mx=1000010;
const ll mod=1e9+7;


int main(){
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i,n){
    cin >> a[i];
  }
  sort(a.begin(),a.end());
  int st = 0;
  int ed = n-1;
  int l = a[ed];
  int r = a[ed];
  ed--;
  ll ans = 0;
  while(st!=ed){
    if(abs(l-a[st])>abs(r-a[st])){
      ans += abs(l-a[st]);
      l = a[st];
      st++;
    } else {
      ans += abs(r-a[st]);
      r = a[st];
      st++;
    }
    if(st==ed) break;

    if(abs(l-a[st])>abs(r-a[st])){
      ans += abs(l-a[st]);
      l = a[st];
      st++;
    } else {
      ans += abs(r-a[st]);
      r = a[st];
      st++;
    }
    if(st==ed) break;

    if(abs(l-a[ed])>abs(r-a[ed])){
      ans += abs(l-a[ed]);
      l = a[ed];
      ed--;
    } else {
      ans += abs(r-a[ed]);
      r = a[ed];
      ed--;
    }
    if(st==ed) break;
    if(abs(l-a[ed])>abs(r-a[ed])){
      ans += abs(l-a[ed]);
      l = a[ed];
      ed--;
    } else {
      ans += abs(r-a[ed]);
      r = a[ed];
      ed--;
    }     
  }

    if(abs(l-a[ed])>abs(r-a[ed])){
      ans += abs(l-a[ed]);
    } else {
      ans += abs(r-a[ed]);
    }    
  
  ll tmp = ans;

  sort(a.rbegin(),a.rend());
  int st = 0;
  int ed = n-1;
  int l = a[ed];
  int r = a[ed];
  ed--;
  ll ans = 0;
  while(st!=ed){
    if(abs(l-a[st])>abs(r-a[st])){
      ans += abs(l-a[st]);
      l = a[st];
      st++;
    } else {
      ans += abs(r-a[st]);
      r = a[st];
      st++;
    }
    if(st==ed) break;

    if(abs(l-a[st])>abs(r-a[st])){
      ans += abs(l-a[st]);
      l = a[st];
      st++;
    } else {
      ans += abs(r-a[st]);
      r = a[st];
      st++;
    }
    if(st==ed) break;

    if(abs(l-a[ed])>abs(r-a[ed])){
      ans += abs(l-a[ed]);
      l = a[ed];
      ed--;
    } else {
      ans += abs(r-a[ed]);
      r = a[ed];
      ed--;
    }
    if(st==ed) break;
    if(abs(l-a[ed])>abs(r-a[ed])){
      ans += abs(l-a[ed]);
      l = a[ed];
      ed--;
    } else {
      ans += abs(r-a[ed]);
      r = a[ed];
      ed--;
    }     
  }

    if(abs(l-a[ed])>abs(r-a[ed])){
      ans += abs(l-a[ed]);
    } else {
      ans += abs(r-a[ed]);
    }    

  ans = max(ans, tmp);
  cout << ans << endl;
  return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:99:7: error: redeclaration of 'int st'
   99 |   int st = 0;
      |       ^~
a.cc:40:7: note: 'int st' previously declared here
   40 |   int st = 0;
      |       ^~
a.cc:100:7: error: redeclaration of 'int ed'
  100 |   int ed = n-1;
      |       ^~
a.cc:41:7: note: 'int ed' previously declared here
   41 |   int ed = n-1;
      |       ^~
a.cc:101:7: error: redeclaration of 'int l'
  101 |   int l = a[ed];
      |       ^
a.cc:42:7: note: 'int l' previously declared here
   42 |   int l = a[ed];
      |       ^
a.cc:102:7: error: redeclaration of 'int r'
  102 |   int r = a[ed];
      |       ^
a.cc:43:7: note: 'int r' previously declared here
   43 |   int r = a[ed];
      |       ^
a.cc:104:6: error: redeclaration of 'll ans'
  104 |   ll ans = 0;
      |      ^~~
a.cc:45:6: note: 'll ans' previously declared here
   45 |   ll ans = 0;
      |      ^~~