Language: C++

#include <iostream>
#include<string>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<queue>
#include<deque>
#include<regex>
#include<stack>
#include<stdio.h>
#include<vector>
#include<set>
#include<map>
#include<iomanip>

#define rep(i, n) for(int i=0;i<n;i++)

typedef int long long ll;
using namespace std;
typedef pair<int, int> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const ll MOD = 1e9 + 7;
static const int MAX = 100;
static const int INF = (1 << 23);



int main() {
    ll n;
    cin >> n;
    vector<ll> v(n);
    vector<ll> m;
    vector<ll> s;
    bool f = true;
    bool t = true;


    rep(i, n) {
        cin >> v[i];
        if (v[i] > 0)f = false;
        if (v[i] < 0)t = false;
        if (v[i] >= 0)s.push_back(v[i]);
        if (v[i] < 0)m.push_back(v[i]);
    }
    //vector<ll> c;
    //sort(c.begin(),c.end());
    sort(v.begin(), v.end());
    sort(s.begin(), s.end());
    sort(m.begin(), m.end());
    vector<pair<ll, ll>> ans;


    if(n==2){
        cout<<v[1]-v[0]<<endl;
        cout<<v[1]<<" "<<v[0]<<endl;
        return 0;
    }


    if (f) {//負のみ


      ll mi=v[0];
        ll ma=v[n-1];
        int p;
        int pl=0;int ne=0;
        rep(i,n){
            if(v[i]<0){
                p=i;
                ne++;
            }else{
                pl++;
            }
        }

        p++;

        if((n-ne-pl)==n){
            cout<<0<<endl;
            rep(i,n-1){
                cout<<0<<" "<<0<<endl;
            }
            return 0;
        }

        if(n==2){
            cout<<v[1]-v[0]<<endl;
            cout<<v[1]<<" "<<v[0]<<endl;
            return 0;
        }
        p=min(n-1,p);
        for(int i=1;i<p;i++){
            ans.push_back({ma,v[i]});
            ma-=v[i];
        }
        for(int i=p;i<n-1;i++){
            ans.push_back({mi,v[i]});
            mi-=v[i];
        }
        ans.push_back({ma,mi});
        cout<<ma-mi<<endl;

        rep(i,ans.size()){
            cout<<ans[i].first<<" "<<ans[i].second<<endl;
        }


    }else if(t){//正のみ

            ll lass=s[0];
            for(int i=2;i<s.size();i++){
                ans.push_back({lass,s[i]});
                lass-=s[i];
            }
           // cout<<lass<<endl;


            ans.push_back({s[1],lass});
            cout<<s[1]-lass<<endl;
            rep(i,ans.size()){
                cout<<ans[i].first<<" "<<ans[i].second<<endl;
            }
     }else{

        ll las=m[0];
        for(int i=0;i<s.size()-1;i++){
            ans.push_back({las,s[i]});
            las-=s[i];
        }

        ll las2=s[s.size()-1];
        for(int j=1;j<m.size();j++){
            ans.push_back({las2,m[j]});
            las2-=m[j];
        }


        ans.push_back({las2,las});
        cout<<las2-las<<endl;
        rep(i,ans.size()){
            cout<<ans[i].first<<" "<<ans[i].second<<endl;
        }


    }










    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:92:14: error: no matching function for call to 'min(ll, int&)'
   92 |         p=min(n-1,p);
      |           ~~~^~~~~~~
In file included from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:92:14: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   92 |         p=min(n-1,p);
      |           ~~~^~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:4:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:92:14: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   92 |         p=min(n-1,p);
      |           ~~~^~~~~~~