Language: C++

#include <bits/stdc++.h>

#define fi first
#define se second

const int N = 200200;
const int mod = 1e9 + 7;

using namespace std;

long long res;
int a[N], b[N];
set < pair < int, int > > s, A, B;

void make(int i, int j)
{
        res += min(a[i], b[j]);

        s.erase({a[i], i});
        s.erase({a[j], j});
        s.erase({b[j], - j});
        A.erase({a[i], i});
        A.erase({a[j], j});
        B.erase({b[j], j});

        a[i] = a[j];
        s.insert({a[i], i});
        A.insert({a[i], i});
}

long long solve1()
{
        res = 0;
        A.clear();
        B.clear();
        s.clear();

        for(int i = 1; i <= n; i++){
                A.insert({a[i], i});
                B.insert({b[i], i});

                s.insert({a[i], i});
                s.insert({b[i], - i});
        }

        for(int i = 1; i < n; i++){
                auto p = *A.begin();
                int h = abs(p.se);

                if(p.se > 0){
                        B.erase({b[h], h});
                        int need = (--B.end())->se;
                        B.insert({b[h], h});
                        make(h, need);
                } else{
                        A.erase({a[h], h});
                        int need = (--A.end())->se;
                        A.insert({a[h], h});
                        make(need, h);
                }
        }

        res += min(A.begin()->fi, B.begin()->fi);
        return res;
}

long long solve2()
{
        res = 0;
        A.clear();
        B.clear();
        s.clear();

        for(int i = 1; i <= n; i++){
                A.insert({a[i], i});
                B.insert({b[i], i});

                s.insert({a[i], i});
                s.insert({b[i], - i});
        }

        for(int i = 1; i < n; i++){
                auto p = *s.begin();
                int h = abs(p.se);

                if(p.se > 0){
                        B.erase({b[h], h});
                        int need = (--B.end())->se;
                        B.insert({b[h], h});
                        make(h, need);
                } else{
                        A.erase({a[h], h});
                        int need = (--A.end())->se;
                        A.insert({a[h], h});
                        make(need, h);
                }
        }

        res += min(A.begin()->fi, B.begin()->fi);
        return res;
}

long long solve3()
{
        res = 0;
        A.clear();
        B.clear();
        s.clear();

        for(int i = 1; i <= n; i++){
                A.insert({a[i], i});
                B.insert({b[i], i});

                s.insert({a[i], i});
                s.insert({b[i], - i});
        }

        for(int i = 1; i < n; i++){
                auto p = *B.begin();
                int h = abs(p.se);
                p.se = -p.se;

                if(p.se > 0){
                        B.erase({b[h], h});
                        int need = (--B.end())->se;
                        B.insert({b[h], h});
                        make(h, need);
                } else{
                        A.erase({a[h], h});
                        int need = (--A.end())->se;
                        A.insert({a[h], h});
                        make(need, h);
                }
        }

        res += min(A.begin()->fi, B.begin()->fi);
        return res;
}


int main()
{
        ios_base::sync_with_stdio(0);

        //freopen("input.txt", "r", stdin);
        //freopen("output.txt", "w", stdout);

        int n;
        cin >> n;

        for(int i = 1; i <= n; i++){
                cin >> a[i] >> b[i];
        }

        long long res = 1e18;
        res = min(res, solve1());
        res = min(res, solve2());
        res = min(res, solve3());

        cout << res << "\n";
}


Compiler output:
a.cc: In function 'long long int solve1()':
a.cc:38:29: error: 'n' was not declared in this scope
   38 |         for(int i = 1; i <= n; i++){
      |                             ^
a.cc:46:28: error: 'n' was not declared in this scope
   46 |         for(int i = 1; i < n; i++){
      |                            ^
a.cc: In function 'long long int solve2()':
a.cc:74:29: error: 'n' was not declared in this scope
   74 |         for(int i = 1; i <= n; i++){
      |                             ^
a.cc:82:28: error: 'n' was not declared in this scope
   82 |         for(int i = 1; i < n; i++){
      |                            ^
a.cc: In function 'long long int solve3()':
a.cc:110:29: error: 'n' was not declared in this scope
  110 |         for(int i = 1; i <= n; i++){
      |                             ^
a.cc:118:28: error: 'n' was not declared in this scope
  118 |         for(int i = 1; i < n; i++){
      |                            ^