Language: C++

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<bits/stdc++.h>
#include<cmath>
#include<bitset>
using namespace std;

int main(){
  int N,ans=0,pm=0; //pm＝負の数
  cin>>N;
  vector<int> A;
  vector<int> B;
  for(int i=0; i<N; ++i){
    int a;
    cin>>a;
    if(a<0) pm++;
    if(a>=0) A.push_back(a);
    else B.push_back(abs(a));
  }
  
  sort(A.begin(),A.end());
  reverse(A.begin(),A.end());
  
  sort(B.begin(),B.end());
  reverse(B.begin(),B.end());
  
  vector<int> C;
  
  if(pm<(N/2)){
    vector<int> D;
    for(int i=0; i<A.size(); ++i){
      D.push_back(A.at(A.size()-i-1));
    }
    
    for(int i=0; i<B.size(); ++i){
      C.push_back(A.at(i));
      C.push_back(-1*B.at(i));
      ans+=A.at(i);
      ans+=B.at(i);
    }
    
    int P=N-2*B.size();
    for(int i=0; i<(P/2); ++i){
      C.push_back(A.at(B.size()+i));
      C.push_back(D.at(i));
      ans+=A.at(B.size()+i);
      ans-=D.at(i);
    }
    if(P%2==1){
      C.push_back(A.at(B.size()+(P/2)));
      ans+=A.at(B.size()+(P/2));
    }
    
    cout<<ans<<endl;
    
    vector<int> Q;
    for(int i=N-1; i>=0; --i){
      Q.push_back(C.at(i));
    }
    
    int t=0;
    for(int i=0; i<N-1; ++i){
      if(i==0){
        cout<<Q.at(0)<<Q.at(1)<<endl;
        if(N%2==1) t=Q.at(1)-Q.at(0);
        else t=Q.at(0)-Q.at(1);
      }
      else{
        t=Q.at(i)-t;
        cout<<Q.at(i)<<t<<endl;
      }
    }
    
  }
  
  else{
    vector<int> D;
    for(int i=0; i<B.size(); ++i){
      D.push_back(B.at(B.size()-i-1));
    }
    
    for(int i=0; i<A.size(); ++i){
      C.push_back(-1*B.at(i));
      C.push_back(A.at(i));
      ans+=A.at(i);
      ans+=B.at(i);
    }
    
    vector<int> Q;
    for(int i=C.size()-1; i>=0; --i){
      Q.push_back(C.at(i));
    }
    
    int t=0;
    for(int i=0; i<C.size()-1; ++i){
      if(i==0){
        cout<<Q.at(0)<<Q.at(1)<<endl;
        else t=Q.at(0)-Q.at(1);
      }
      else{
        t=Q.at(i)-t;
        cout<<Q.at(i)<<t<<endl;
      }
    }
    
    
    int P=N-2*A.size();
    for(int i=0; i<P; ++i){
      Q.push_back(B.at(A.size()+i));
      ans+=B.at(A.size()+i);
    }
    
    cout<<ans<<endl;
    
    for(int i=2*A.size(); i<N-1; ++i){
      if(i==0){
        cout<<Q.at(0)<<Q.at(1)<<endl;
        else t=Q.at(0)-Q.at(1);
      }
      else{
        t=Q.at(i)-t;
        cout<<Q.at(i)<<t<<endl;
      }
    }
    
  }
  
}


Compiler output:
a.cc: In function 'int main()':
a.cc:100:9: error: expected '}' before 'else'
  100 |         else t=Q.at(0)-Q.at(1);
      |         ^~~~
a.cc:98:15: note: to match this '{'
   98 |       if(i==0){
      |               ^
a.cc:102:7: error: 'else' without a previous 'if'
  102 |       else{
      |       ^~~~
a.cc:103:16: error: 'i' was not declared in this scope
  103 |         t=Q.at(i)-t;
      |                ^
a.cc:111:7: error: 'Q' was not declared in this scope
  111 |       Q.push_back(B.at(A.size()+i));
      |       ^
a.cc:119:15: error: 'Q' was not declared in this scope
  119 |         cout<<Q.at(0)<<Q.at(1)<<endl;
      |               ^
a.cc:120:9: error: expected '}' before 'else'
  120 |         else t=Q.at(0)-Q.at(1);
      |         ^~~~
a.cc:118:15: note: to match this '{'
  118 |       if(i==0){
      |               ^
a.cc:120:14: error: 't' was not declared in this scope
  120 |         else t=Q.at(0)-Q.at(1);
      |              ^
a.cc:120:16: error: 'Q' was not declared in this scope
  120 |         else t=Q.at(0)-Q.at(1);
      |                ^
a.cc:122:7: error: 'else' without a previous 'if'
  122 |       else{
      |       ^~~~
a.cc:123:9: error: 't' was not declared in this scope
  123 |         t=Q.at(i)-t;
      |         ^
a.cc:123:11: error: 'Q' was not declared in this scope
  123 |         t=Q.at(i)-t;
      |           ^
a.cc:123:16: error: 'i' was not declared in this scope
  123 |         t=Q.at(i)-t;
      |                ^
a.cc: At global scope:
a.cc:128:3: error: expected declaration before '}' token
  128 |   }
      |   ^
a.cc:130:1: error: expected declaration before '}' token
  130 | }
      | ^