Language: C++

#include <stdio.h>
#include <stdlib.h>

#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <stdint.h>
#include <string.h>

#define _USE_MATH_DEFINES
#include <math.h>

#include <vector>
#include <list>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
#include <deque>
#include <string>

#include <algorithm>
#include <functional>
#include <bitset>
#include <functional>
#include <chrono>
#include <random>
#define sqr(x) (x) * (x)

typedef unsigned int u32;
typedef int i32;
typedef unsigned long long int u64;
typedef long long int i64;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint8_t u8;
typedef int8_t i8;

using namespace std;
using namespace std::chrono;
using namespace __gnu_pbds;

const i64 mod = 1000000007ll;
const i64 smod = 998244353ll;
const i64 inf = 10000000000000007ll;

const double eps = 1e-10;


const i64 MAXN = 20;

i64 n;
i64 a[MAXN];
i64 b[MAXN];

inline i64 val(pair<i64, bool> p) {
    if (!p.second) {
        return a[p.first];
    }
    else {
        return b[p.first];
    }
}

inline i64 val(vector<i64>& r) {
    //stringstream ss;
    //for (auto t : r) {
    //    ss << t << " ";
    //}
    //cerr << ss.str() << endl;

    i64 R = 0;
    tree<int, null_type, less<i64>, rb_tree_tag, tree_order_statistics_node_update> s;

    for (i64 i = 0; i < n; i++) {
        s.insert(r[i]);
        R += i - s.order_of_key(r[i]);
    }
    return R;
}

i64 solve(i64 m) {
    map<i64, map<i64, vector<i64>>> e;
    for (i64 i = 0; i < n; i += 1) {
        pair<i64, i64> p = { i, (m & (1ll << i) ? 1 : 0) };
        e[val(p)][p.second].push_back(p.first);
    }

    vector<i64> r;
    i64 ts = 0;
    for (auto& u : e) {
        auto& t = u.second;

        vector<i64> c[2];
        for (auto v : t[0]) {
            c[v % 2].push_back(v);
        }
        for (auto v : t[1]) {
            c[(v + 1) % 2].push_back(v);
        }

        i64 z[2] = { 0, 0 };
        i64 tt = (t[0].size() + t[1].size());
        z[0] = tt / 2 + tt % 2;
        z[1] = tt / 2;
        if (ts % 2) {
            swap(z[0], z[1]);
        }

        if (c[0].size() != z[0] || c[1].size() != z[1]) {
            return inf;
        }

        sort(c[0].data(), c[0].data() + c[0].size());
        sort(c[1].data(), c[1].data() + c[1].size());

        for (i64 i = 0; i < t[0].size() + t[1].size(); i++) {
            r.push_back(c[(ts + i) % 2][i / 2]);
        }


        ts += t[0].size() + t[1].size();
    }

    return val(r);
}

int main(int argc, char* argv[]) {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(15); cout.setf(ios::fixed);

    cin >> n;
    for (i64 i = 0; i < n; i++) {
        cin >> a[i];
    }

    for (i64 i = 0; i < n; i++) {
        cin >> b[i];
    }

    i64 R = inf;
    for (i64 m = 0; m < (1ll << n); m++) {
        R = min(R, solve(m));
    }
    cout << ((R == inf) ? -1 : R) << endl;

    return 0;
}

Compiler output:
a.cc:44:17: error: '__gnu_pbds' is not a namespace-name
   44 | using namespace __gnu_pbds;
      |                 ^~~~~~~~~~
a.cc: In function 'i64 val(std::vector<long long int>&)':
a.cc:76:5: error: 'tree' was not declared in this scope; did you mean 'free'?
   76 |     tree<int, null_type, less<i64>, rb_tree_tag, tree_order_statistics_node_update> s;
      |     ^~~~
      |     free
a.cc:76:10: error: expected primary-expression before 'int'
   76 |     tree<int, null_type, less<i64>, rb_tree_tag, tree_order_statistics_node_update> s;
      |          ^~~
a.cc:79:9: error: 's' was not declared in this scope
   79 |         s.insert(r[i]);
      |         ^