Problem: 2052A - Adrenaline Rush
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 4):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fr(i, a, b) for (int i = a; i < b; i++)
#define vi vector<int>
#define vc vector<char>
#define pb push_back
#define ub(x, v) upper_bound(v.begin(), v.end(), x)
#define lb(x, v) lower_bound(v.begin(), v.end(), x)
#define lbi(x, v) lower_bound(v.begin(), v.end(), x) - v.begin()
#define ubi(x, v) upper_bound(v.begin(), v.end(), x) - v.begin()
#define all(a) (a).begin(), (a).end()
int power(int a, int b)
{
    int result = 1;
    while (b)
    {
        if (b & 1)
            result = result * a;
        a = a * a;
        b >>= 1;
    }
    return result;
}
#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)
signed main()
{
    fast;
    {
        int n;
        cin >> n;
        vi v(n);
        fr(i, 0, n) { cin >> v[i]; }
        vector<pair<int, int>> p;
        map<int, vi> m;
        fr(i, 0, n - 1)
        {
            fr(j, 1 + i, n)
            {
                if (v[i] > v[j])
                {
                    m[v[i]].pb(v[j]);
                }
                else
                {
                    m[v[i]].pb(v[j]);
                    m[v[j]].pb(v[i]);
                }
            }
        }
        for (int i = n; i > 0; i--)
        {
            if (!m[i].empty())
            {
                sort(all(m[i]));
                vi temp;
                for (int j = m[i].size() - 1; j >= 0; j--)
                {
                    if (m[i][j] > i)
                    {
                        temp.pb(m[i][j]);
                    }
                    else
                    {
                        p.pb({i, m[i][j]});
                    }
                }
                if (!temp.empty())
                {
                    sort(all(temp));
                    fr(j, 0, temp.size())
                    {
                        p.pb({i, temp[j]});
                    }
                }
            }
        }
        cout << p.size() << endl;
        fr(i, 0, p.size())
        {
            cout << p[i].first << " " << p[i].second << endl;
        }
    }
}
/*
1 2 3 4 5
5 3 2 1 4
tot = 13
unordered
5 3
5 2
5 1
5 4
3 2
3 1
4 3
3 4
2 1
4 2
2 4
4 1
1 4

if we sort
5 4
5 3
5 2
5 1
5 1 2 3 4
4 3
4 2
4 1
5 4 1 2 3
3 2
3 1
3 4
5 3 4 1 2
2 1
2 4
5 3 2 4 1
1 4
5 3 2 1 4



2 1
3 1
4 1
5 1
2 3 4 5 1
4 3
4 2

1 4
*/


Submission (verdict: Runtime error on test 1):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fr(i, a, b) for (int i = a; i < b; i++)
#define vi vector<int>
#define vc vector<char>
#define pb push_back
#define ub(x, v) upper_bound(v.begin(), v.end(), x)
#define lb(x, v) lower_bound(v.begin(), v.end(), x)
#define lbi(x, v) lower_bound(v.begin(), v.end(), x) - v.begin()
#define ubi(x, v) upper_bound(v.begin(), v.end(), x) - v.begin()
#define all(a) (a).begin(), (a).end()
int power(int a, int b)
{
    int result = 1;
    while (b)
    {
        if (b & 1)
            result = result * a;
        a = a * a;
        b >>= 1;
    }
    return result;
}
#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)
signed main()
{
    fast;
    {
        int n;
        cin >> n;
        vi v(n);
        fr(i, 0, n) { cin >> v[i]; }
        vector<pair<int, int>> p;
        map<int, vi> m;
        fr(i, 0, n - 1)
        {
            fr(j, 1 + i, n)
            {
                if (v[i] > v[j])
                {
                    m[v[i]].pb(v[j]);
                }
                else
                {
                    m[v[i]].pb(v[j]);
                    m[v[j]].pb(v[i]);
                }
            }
        }
        for (int i = n; i > 1; i--)
        {
            if (!m[i].empty())
            {
                sort(all(m[i]));
                vi temp;
                for (int j = m[i].size() - 1; j >= 0; j--)
                {
                    if (m[i][j] > i)
                    {
                        temp.pb(m[i][j]);
                    }
                    else
                    {
                        p.pb({i, m[i][j]});
                    }
                }
                if (!temp.empty())
                {
                    sort(all(temp));
                    fr(j, 0, temp.size())
                    {
                        p.pb({i, temp[j]});
                    }
                }
            }
        }
        sort(all(m[1]));
        for(int i = m[1].size()-1;i>=0;i--){
            p.pb({1,m[1][i]});
        }
        cout << p.size() << endl;
        fr(i, 0, p.size())
        {
            cout << p[i].first << " " << p[i].second << endl;
        }
    }
}
/*
1 2 3 4 5
5 3 2 1 4
tot = 13
unordered
5 3
5 2
5 1
5 4
3 2
3 1
4 3
3 4
2 1
4 2
2 4
4 1
1 4

if we sort
5 4
5 3
5 2
5 1
5 1 2 3 4
4 3
4 2
4 1
5 4 1 2 3
3 2
3 1
3 4
5 3 4 1 2
2 1
2 4
5 3 2 4 1
1 4
5 3 2 1 4

3 
1 2 3
unordered
1 2
2 1
1 3
3 1
2 3
3 2
 ordered
3 2
3 1
3 1 2
2 1
2 3
2 3 1
1 3
1 2

4 1 2 3
4 1
4 2
4 3
2 1
// 1 2
3 1
// 1 3
3 2
// 2 3

4 3
4 2
4 1
4 1 2 3
3 2
2 3

4 3 1 2
2 1
2 3
4 2 3 1

*/