Language: C++

#include <bits/stdc++.h>
#define for(i, a, b) for(int i=(int)(a); i!=(int)(b); i++)
#define rfor(i, a, b) for(int i=(int)(a); i!=(int)(b); i--)
#define rep(i, n) for(i, 0, n)
using namespace std;
using ll = long long;

ll calc(vector<ll> a, vector<ll> b, int n){
  if (n % 2 == 0){
    vector<ll> c(n);
    for(int i=0, j=0, k=0; i<n/2; i++){
      c[n-2*i-2] = a[j++];
      c[n-2*i-1] = b[k++];
    }

    ll ans = 0;
    rep(i, n-1) ans += abs(c[i+1]-c[i]);

    return ans;
  }
  else{
    vector<ll> c(n);
    if(a.size() < b.size()) swap(a, b);

    if(n == 5){
      swap(a[0], a[1]);
    }
    else if(n > 5){
      swap(a[n/2-1], a[0]);
    }
    
    for(int i=0, j=0, k=0; i<n; i++){
      if(i % 2){
        c[i] = b[j++];
      }
      else{
        c[i] = a[k++];
      }
    }

    ll ans = 0;
  rep(i, n-1) ans += abs(c[i+1]-c[i]);
    
    return ans;
  }
}

int main(){
  int n; cin >> n;
  ll a[n];
  rep(i, n) cin >> a[i];

  sort(a, a+n);

  int b = n/2;

  vector<ll> x, y;
  for(int i=0; i<n; i++){
    if(i < b){
      x.push_back(a[i]);
    }
    else{
      y.push_back(a[i]);
    }
  }

  ll ans = calc(x, y, n);

  if(n % 2 == 0){
    cout << ans << endl;
    return 0;
  }

  x.clear(); y.clear();

  if(n % 2){
    for(int i=0; i<n; i++){
      if(i <= b){
        x.push_back(a[i]);
      }
      else{
        y.push_back(a[i]);
      }
    }
  }

  cout << max(calc(x, y, n), ans)<< endl;

  return 0;
}


Compiler output:
a.cc:58:24: error: macro "for" requires 3 arguments, but only 1 given
   58 |   for(int i=0; i<n; i++){
      |                        ^
a.cc:2:9: note: macro "for" defined here
    2 | #define for(i, a, b) for(int i=(int)(a); i!=(int)(b); i++)
      |         ^~~
a.cc:77:26: error: macro "for" requires 3 arguments, but only 1 given
   77 |     for(int i=0; i<n; i++){
      |                          ^
a.cc:2:9: note: macro "for" defined here
    2 | #define for(i, a, b) for(int i=(int)(a); i!=(int)(b); i++)
      |         ^~~
a.cc: In function 'll calc(std::vector<long long int>, std::vector<long long int>, int)':
a.cc:2:26: error: two or more data types in declaration of 'i'
    2 | #define for(i, a, b) for(int i=(int)(a); i!=(int)(b); i++)
      |                          ^~~
a.cc:11:5: note: in expansion of macro 'for'
   11 |     for(int i=0, j=0, k=0; i<n/2; i++){
      |     ^~~
a.cc:11:18: error: 'j' was not declared in this scope
   11 |     for(int i=0, j=0, k=0; i<n/2; i++){
      |                  ^
a.cc:2:38: note: in definition of macro 'for'
    2 | #define for(i, a, b) for(int i=(int)(a); i!=(int)(b); i++)
      |                                      ^
a.cc:11:23: error: 'k' was not declared in this scope
   11 |     for(int i=0, j=0, k=0; i<n/2; i++){
      |                       ^
a.cc:2:51: note: in definition of macro 'for'
    2 | #define for(i, a, b) for(int i=(int)(a); i!=(int)(b); i++)
      |                                                   ^
a.cc:11:26: error: expected ')' before ';' token
   11 |     for(int i=0, j=0, k=0; i<n/2; i++){
      |                          ^
a.cc:2:51: note: in definition of macro 'for'
    2 | #define for(i, a, b) for(int i=(int)(a); i!=(int)(b); i++)
      |                                                   ^
a.cc:2:50: note: to match this '('
    2 | #define for(i, a, b) for(int i=(int)(a); i!=(int)(b); i++)
      |                                                  ^
a.cc:11:5: note: in expansion of macro 'for'
   11 |     for(int i=0, j=0, k=0; i<n/2; i++){
      |     ^~~
a.cc:11:33: error: expected ')' before ';' token
   11 |     for(int i=0, j=0, k=0; i<n/2; i++){
      |                                 ^
a.cc:2:51: note: in definition of macro 'for'
    2 | #define for(i, a, b) for(int i=(int)(a); i!=(int)(b); i++)
      |                                                   ^
a.cc:2:25: note: to match this '('
    2 | #define for(i, a, b) for(int i=(int)(a); i!=(int)(b); i++)
      |                         ^
a.cc:11:5: note: in expansion of macro 'for'
   11 |     for(int i=0, j=0, k=0; i<n/2; i++){
      |     ^~~
a.cc:11:35: error: 'i' was not declared in this scope
   11 |     for(int i=0, j=0, k=0; i<n/2; i++){
      |                                   ^
a.cc:2:51: note: in definition of macro 'for'
    2 | #define for(i, a, b) for(int i=(int)(a); i!=(int)(b); i++)
      |                                                   ^
a.cc:11:15: error: lvalue required as increment operand
   11 |     for(int i=0, j=0, k=0; i<n/2; i++){
      |               ^
a.cc:2:55: note: in definition of macro 'for'
    2 | #define for(i, a, b) for(int i=(int)(a); i!=(int)(b); i++)
      |                                                       ^
a.cc:17:17: error: 'ans' was not declared in this scope; did you mean 'abs'?
   17 |     rep(i, n-1) ans += abs(c[i+1]-c[i]);
      |                 ^~~
      |                 abs
a.cc:19:12: error: 'ans' was not declared in this scope; did you mean 'abs'?
   19 |     return ans;
      |            ^~~
      |            abs
a.cc:2:26: error: two or more data types in declaration of 'i'
    2 | #define for(i, a, b) for(int i=(int)(a); i!=(int)(b); i++)
      |                          ^~~
a.cc:32:5: note: in expansion of macro 'for'
   32 |     for(int i=0, j=0, k=0; i<n; i++){
      |     ^~~
a.cc:32:18: error: 'j' was not declared in this scope
   32 |     for(int i=0, j=0, k=0; i<n; i++){
      |                  ^
a.cc:2:38: note: in definition of macro 'for'
    2 | #define for(i, a, b) for(int i=(int)(a); i!=(int)(b); i++)
      |                                      ^
a.cc:32:23: error: 'k' was not declared in this scope
   32 |     for(int i=0, j=0, k=0; i<n; i++){
      |                       ^
a.cc:2:51: note: in definition of macro 'for'
    2 | #define for(i, a, b) for(int i=(int)(a); i!=(int)(b); i++)
      |                                                   ^
a.cc:32:26: error: expected ')' before ';' token
   32 |     for(int i=0, j=0, k=0; i<n; i++){
      |                          ^
a.cc:2:51: note: in definition of macro 'for'
    2 | #define for(i, a, b) for(int i=(int)(a); i!=(int)(b); i++)
      |                                                   ^
a.cc:2:50: note: to match this '('
    2 | #define for(i, a, b) for(int i=(int)(a); i!=(int)(b); i++)
      |                                                  ^
a.cc:32:5: note: in expansion of macro 'for'
   32 |     for(int i=0, j=0, k=0; i<n; i++){
      |     ^~~
a.cc:32:31: error: expected ')' before ';' token
   32 |     for(int i=0, j=0, k=0; i<n; i++){
      |                               ^
a.cc:2:51: note: in definition of macro 'for'
    2 | #define for(i, a, b) for(int i=(int)(a); i!=(int)(b); i++)
      |                                                   ^
a.cc:2:25: note: to match this '('
    2 | #define for(i, a, b) for(int i=(int)(a); i!=(int)(b); i++)
      |                         ^
a.cc:32:5: note: in expansion of macro 'for'
   32 |     for(int i=0, j=0, k=0; i<n; i++){
      |     ^~~
a.cc:32:33: error: 'i' was not declared in this scope
   32 |     for(int i=0, j=0, k=0; i<n; i++){
      |                                 ^
a.cc:2:51: note: in definition of macro 'for'
    2 | #define for(i, a, b) for(int i=(int)(a); i!=(int)(b); i++)
      |                                                   ^
a.cc:32:15: error: lvalue required as increment operand
   32 |     for(int i=0, j=0, k=0; i<n; i++){
      |               ^
a.cc:2:55: note: in definition of macro 'for'
    2 | #define for(i, a, b) for(int i=(int)(a); i!=(int)(b); i++)
      |                                                       ^
a.cc:42:15: error: 'ans' was not declared in this scope; did you mean 'abs'?
   42 |   rep(i, n-1) ans += abs(c[i+1]-c[i]);
      |               ^~~
      |               abs
a.cc:44:12: error: 'ans' was not declared in this scope; did you mean 'abs'?
   44 |     return ans;
      |            ^~~
      |            abs
a.cc: In function 'int main()':
a.cc:58:25: error: expected '(' before '{' token
   58 |   for(int i=0; i<n; i++){
      |                         ^
      |                         (
a.cc:58:25: error: expected primary-expression before '{' token
a.cc:69:3: error: expected primary-expression before 'if'
   69 |   if(n % 2 == 0){
      |   ^~
a.cc:67:26: error: expected ')' before 'if'
   67 |   ll ans = calc(x, y, n);
      |                          ^
      |                          )
   68 | 
   69 |   if(n % 2 == 0){
      |   ~~                      
a.cc:58:25: note: to match this '('
   58 |   for(int i=0; i<n; i++){
      |                         ^
a.cc:77:27: error: expected '(' before '{' token
   77 |     for(int i=0; i<n; i++){
      |                           ^
      |                           (
a.cc:77:27: error: expected primary-expression before '{' token
a.cc:85:3: error: expected primary-expression before '}' token
   85 |   }
      |   ^
a.cc:84:6: error: expected ';' before '}' token
   84 |     }
      |      ^
      |      ;
   85 |   }
      |   ~   
a.cc:85:3: error: expected primary-expression before '}' token
   85 |   }
      |   ^
a.cc:84:6: error: expected ')' before '}' token
   84 |     }
      |      ^
      |      )
   85 |   }
      |   ~   
a.cc:77:27: note: to match this '('
   77 |     for(int i=0; i<n; i++){
      |                           ^
a.cc:85:3: error: expected primary-expression before '}' token
   85 |   }
      |   ^
a.cc:87:30: error: 'ans' was not declared in this scope; did you mean 'abs'?
   87 |   cout << max(calc(x, y, n), ans)<< endl;
      |                              ^~~
      |                              abs