Language: C++

#include<bits/stdc++.h>

#define int long long
using namespace std;
#define rep(i, n) for(int i=0;i<(n);++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;

int N;
int A[20], B[20];

signed main() {
    cin >> N;
    if (N % 2 == 1) {
        bool noop = true;//既にソート済み?
        rep(i, N) {
            cin >> A[i];
            if (i && A[i] < A[i - 1])noop = false;
        }
        if (noop) {
            cout << 0 << endl;
            return 0;
        }
        rep(i, N) {
            cin >> B[i];
        }
        bool blue[20];
        int ans = INF;
        rep(i, (1 << N)) {
            int cnt = 0;
            rep(j, N) {
                if ((i >> j) & 1) {
                    cnt++;
                    blue[j] = true;
                } else {
                    blue[j] = false;
                }
            }
            if (cnt % 2 == 1)continue;//青は偶数個
            //cout << "a" << endl;
            vector<int> val(N);
            rep(j, N) {
                val[j] = blue[j] ? B[j] : A[j];
            }
            vector<int> A, B;
            vector<int> Aval, Bval;

            rep(j, N) {
                if ((j % 2 == 0 && !blue[j]) || (j % 2 == 1 && blue[j])) {
                    A.push_back(j);
                    Aval.push_back(val[j]);
                } else {
                    B.push_back(j);
                    Bval.push_back(val[j]);
                }
            }
            if (!(A.size() == B.size() || A.size() == B.size() + 1))continue;
            //cout << "b" << endl;
            int res = 0;
            vector<int> cur(N);
            rep(j, N) {
                cur[j] = j;
            }
            sort(Aval.begin(), Aval.end());
            sort(Bval.begin(), Bval.end());
            rep(j, B.size()) {
                if (Aval[j] > Bval[j])goto next;
                if (A.size() >= j + 1 + 1) {
                    if (Aval[j + 1] < Bval[j])goto next;
                }
            }
            //cout << "c" << endl;
            rep(j, N) {
                //cout << j << endl;
                if (j % 2 == 0) {
                    //Aval[j/2]を探す
                    rep(k, A.size()) {
                        if (val[A[k]] == Aval[j / 2]) {
                            //これをj番目に動かす
                            res += cur[A[k]] - j;
                            //cerr << "A" << A[k] << " " << cur[A[k]] - j << endl;
                            val[A[k]] = -1;
                            //既に前方に移動させたもの以外は前後関係は変わっていない
                            rep(l, A[k]) {
                                cur[l]++;
                            }
                        }
                    }
                } else {
                    //Bval[j/2]を探す
                    rep(k, B.size()) {
                        if (val[B[k]] == Bval[j / 2]) {
                            res += cur[B[k]] - j;
                            //cerr << "B" << B[k] << " " << cur[B[k]] - j << endl;
                            val[B[k]] = -1;
                            rep(l, B[k]) {
                                cur[l]++;
                            }
                        }
                    }
                }
            }
            ans = min(ans, res);
            //cout << "!-1" << endl;
            next:;
        }
        if (ans == INF) {
            cout << -1 << endl;
            return 0;
        }
        cout << ans << endl;
        return 0;
    } else {
        bool noop = true;//既にソート済み?
        rep(i, N) {
            cin >> A[i];
            if (i && A[i] < A[i - 1])noop = false;
        }
        if (noop) {
            cout << 0 << endl;
            return 0;
        }
        rep(i, N) {
            cin >> B[i];
        }
        bool blue[20];
        int ans = INF;
        rep(i, (1 << N)) {
            int cnt = 0;
            rep(j, N) {
                if ((i >> j) & 1) {
                    cnt++;
                    blue[j] = true;
                } else {
                    blue[j] = false;
                }
            }
            if (cnt % 2 == 1)continue;//青は偶数個
            //cout << "a" << endl;
            vector<int> val(N);
            rep(j, N) {
                val[j] = blue[j] ? B[j] : A[j];
            }
            vector<int> A, B;
            vector<int> Aval, Bval;

            rep(j, N) {
                if ((j % 2 == 0 && !blue[j]) || (j % 2 == 1 && blue[j])) {
                    A.push_back(j);
                    Aval.push_back(val[j]);
                } else {
                    B.push_back(j);
                    Bval.push_back(val[j]);
                }
            }
            if (!(A.size() == B.size()))continue;
            //cout << "b" << endl;
            int res = 0;
            vector<int> cur(N);
            rep(j, N) {
                cur[j] = j;
            }
            sort(Aval.begin(), Aval.end());
            sort(Bval.begin(), Bval.end());
            rep(j, B.size()) {
                if (Aval[j] > Bval[j])goto next;
                if (j != B.size() - 1) {
                    if (Aval[j + 1] < Bval[j])goto next;
                }
            }
            //cout << "c" << endl;
            rep(j, N) {
                //cout << j << endl;
                if (j % 2 == 0) {
                    //Aval[j/2]を探す
                    rep(k, A.size()) {
                        if (val[A[k]] == Aval[j / 2]) {
                            //これをj番目に動かす
                            res += cur[A[k]] - j;
                            //cerr << "A" << A[k] << " " << cur[A[k]] - j << endl;
                            val[A[k]] = -1;
                            //既に前方に移動させたもの以外は前後関係は変わっていない
                            rep(l, A[k]) {
                                cur[l]++;
                            }
                        }
                    }
                } else {
                    //Bval[j/2]を探す
                    rep(k, B.size()) {
                        if (val[B[k]] == Bval[j / 2]) {
                            res += cur[B[k]] - j;
                            //cerr << "B" << B[k] << " " << cur[B[k]] - j << endl;
                            val[B[k]] = -1;
                            rep(l, B[k]) {
                                cur[l]++;
                            }
                        }
                    }
                }
            }
            ans = min(ans, res);
            //cout << "!-1" << endl;
            next:;
        }
        if (ans == INF) {
            cout << -1 << endl;
            return 0;
        }
        cout << ans << endl;
        return 0;
    }
}


Compiler output:
a.cc: In function 'int main()':
a.cc:105:13: error: jump to label 'next'
  105 |             next:;
      |             ^~~~
a.cc:166:44: note:   from here
  166 |                 if (Aval[j] > Bval[j])goto next;
      |                                            ^~~~
a.cc:60:25: note:   skips initialization of 'std::vector<long long int> cur'
   60 |             vector<int> cur(N);
      |                         ^~~
a.cc:59:17: note:   skips initialization of 'long long int res'
   59 |             int res = 0;
      |                 ^~~
a.cc:46:31: note:   skips initialization of 'std::vector<long long int> Bval'
   46 |             vector<int> Aval, Bval;
      |                               ^~~~
a.cc:46:25: note:   skips initialization of 'std::vector<long long int> Aval'
   46 |             vector<int> Aval, Bval;
      |                         ^~~~
a.cc:45:28: note:   skips initialization of 'std::vector<long long int> B'
   45 |             vector<int> A, B;
      |                            ^
a.cc:45:25: note:   skips initialization of 'std::vector<long long int> A'
   45 |             vector<int> A, B;
      |                         ^
a.cc:41:25: note:   skips initialization of 'std::vector<long long int> val'
   41 |             vector<int> val(N);
      |                         ^~~
a.cc:30:17: note:   skips initialization of 'long long int cnt'
   30 |             int cnt = 0;
      |                 ^~~
a.cc:29:13: note:   skips initialization of 'long long int i'
   29 |         rep(i, (1 << N)) {
      |             ^
a.cc:5:27: note: in definition of macro 'rep'
    5 | #define rep(i, n) for(int i=0;i<(n);++i)
      |                           ^
a.cc:28:13: note:   skips initialization of 'long long int ans'
   28 |         int ans = INF;
      |             ^~~
a.cc:15:14: note:   skips initialization of 'bool noop'
   15 |         bool noop = true;//既にソート済み?
      |              ^~~~
a.cc:105:13: error: jump to label 'next'
  105 |             next:;
      |             ^~~~
a.cc:168:52: note:   from here
  168 |                     if (Aval[j + 1] < Bval[j])goto next;
      |                                                    ^~~~
a.cc:60:25: note:   skips initialization of 'std::vector<long long int> cur'
   60 |             vector<int> cur(N);
      |                         ^~~
a.cc:59:17: note:   skips initialization of 'long long int res'
   59 |             int res = 0;
      |                 ^~~
a.cc:46:31: note:   skips initialization of 'std::vector<long long int> Bval'
   46 |             vector<int> Aval, Bval;
      |                               ^~~~
a.cc:46:25: note:   skips initialization of 'std::vector<long long int> Aval'
   46 |             vector<int> Aval, Bval;
      |                         ^~~~
a.cc:45:28: note:   skips initialization of 'std::vector<long long int> B'
   45 |             vector<int> A, B;
      |                            ^
a.cc:45:25: note:   skips initialization of 'std::vector<long long int> A'
   45 |             vector<int> A, B;
      |                         ^
a.cc:41:25: note:   skips initialization of 'std::vector<long long int> val'
   41 |             vector<int> val(N);
      |                         ^~~
a.cc:30:17: note:   skips initialization of 'long long int cnt'
   30 |             int cnt = 0;
      |                 ^~~
a.cc:29:13: note:   skips initialization of 'long long int i'
   29 |         rep(i, (1 << N)) {
      |             ^
a.cc:5:27: note: in definition of macro 'rep'
    5 | #define rep(i, n) for(int i=0;i<(n);++i)
      |                           ^
a.cc:28:13: note:   skips initialization of 'long long int ans'
   28 |         int ans = INF;
      |             ^~~
a.cc:15:14: note:   skips initialization of 'bool noop'
   15 |         bool noop = true;//既にソート済み?
      |              ^~~~
a.cc:204:13: error: duplicate label 'next'
  204 |             next:;
      |             ^~~~