Language: C++

#include <stdio.h>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;
int cnt = 0;

int main()
{
    int n, x, mi, pi, sum;
    cin >> n;
    int r[n];
    vector<int> mem[INT16_MAX]; // 保存用の配列
    for (int i = 0; i < n; i++) // repという入れ方もあるらしい
    {                           // O(N)
        cin >> r[i];
    }
    sort(r, r + n);

    if (n == 2)
    {
        mem[cnt].push_back(r[1]);
        mem[cnt++].push_back(r[0]);
        sum = r[1] - r[0];
    }
    else
    {
        int flag = 0;
        // 全部プラスマイナスでないかを判定
        if (r[0] >= 0)
            flag = 1;
        else if (r[n] <= 0)
            flag = -1;

        if (flag == 1)
        { //全部正の数
            for (int i = 2; i < n; i++)
            {
                mem[cnt].push_back(r[0]);
                mem[cnt++].push_back(r[i]);
                r[0] = r[0] - r[i];
            }
            mem[cnt].push_back(r[1]);
            mem[cnt++].push_back(r[0]);
            sum = r[1] - r[0];
        }
        else if (flag == -1)
        { // 全部負の数
            for (int i = 0; i < n - 1; i++)
            {
                mem[cnt].push_back(r[n - 1]);
                mem[cnt++].push_back(r[i]);
                r[n - 1] = r[n - 1] - r[i];
            }
            sum = r[n - 1];
        }
        else
        { // プラマイ混合
            for (int i = 0; i < n; i++)
            {
                if (r[i] > 0)
                {
                    pi++;
                }
                else
                {
                    mi++;
                }
            }
            if (mi <= pi)
            {
                for (int i = n - 1; i > mi; i--)
                { //プラス部分をマイナスにする
                    mem[cnt].push_back(r[0]);
                    mem[cnt++].push_back(r[i]);
                    r[0] = r[0] - r[i];
                }

                for (int i = 0; i < mi; i++)
                { //マイナス部分の計算をする
                    mem[cnt].push_back(r[mi]);
                    mem[cnt++].push_back(r[i]);
                    r[mi] = r[mi] - r[i];
                }
                sum = r[mi];
            }
            else
            {//変更する
                for (int i = n - 1; i > mi; i--)
                { //プラス部分をマイナスにする
                    mem[cnt].push_back(r[0]);
                    mem[cnt++].push_back(r[i]);
                    r[0] = r[0] - r[i];
                }

                for (int i = 0; i < mi; i++)
                { //マイナス部分の計算をする
                    mem[cnt].push_back(r[mi]);
                    mem[cnt++].push_back(r[i]);
                    r[mi] = r[mi] - r[i];
                }
                sum = r[mi];
            }
        }
    }

    printf("%d\n", sum);
    for (int i = 0; i < n - 1; i++)
    {
        printf("%d %d\n", mem[i][0], mem[i][1]);
    }
}


Compiler output:
a.cc: In function 'int main()':
a.cc:14:21: error: 'INT16_MAX' was not declared in this scope
   14 |     vector<int> mem[INT16_MAX]; // 保存用の配列
      |                     ^~~~~~~~~
a.cc:6:1: note: 'INT16_MAX' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
    5 | #include <vector>
  +++ |+#include <cstdint>
    6 | using namespace std;
a.cc:19:5: error: 'sort' was not declared in this scope; did you mean 'sqrt'?
   19 |     sort(r, r + n);
      |     ^~~~
      |     sqrt
a.cc:23:9: error: 'mem' was not declared in this scope
   23 |         mem[cnt].push_back(r[1]);
      |         ^~~
a.cc:40:17: error: 'mem' was not declared in this scope
   40 |                 mem[cnt].push_back(r[0]);
      |                 ^~~
a.cc:44:13: error: 'mem' was not declared in this scope
   44 |             mem[cnt].push_back(r[1]);
      |             ^~~
a.cc:52:17: error: 'mem' was not declared in this scope
   52 |                 mem[cnt].push_back(r[n - 1]);
      |                 ^~~
a.cc:75:21: error: 'mem' was not declared in this scope
   75 |                     mem[cnt].push_back(r[0]);
      |                     ^~~
a.cc:82:21: error: 'mem' was not declared in this scope
   82 |                     mem[cnt].push_back(r[mi]);
      |                     ^~~
a.cc:92:21: error: 'mem' was not declared in this scope
   92 |                     mem[cnt].push_back(r[0]);
      |                     ^~~
a.cc:99:21: error: 'mem' was not declared in this scope
   99 |                     mem[cnt].push_back(r[mi]);
      |                     ^~~
a.cc:111:27: error: 'mem' was not declared in this scope
  111 |         printf("%d %d\n", mem[i][0], mem[i][1]);
      |                           ^~~