Language: C++

#include <bits/stdc++.h>
//#include <boost/multiprecision/cpp_int.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("O3")
#define REP(i,n) for(int i=0;i<n;i++)
#define REPP(i,n) for(int i=1;i<=n;i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define EPS (1e-9)
#define INF (1e17)
#define PI (acos(-1))
//const double PI = acos(-1);
//const double EPS = 1e-15;
//long long INF=(long long)1E17;
#define i_7 (long long)(1e9+7)
//#define i_7 998'244'353
long mod(long a){
    long long c=a%i_7;
    if(c>=0)return c;
    return c+i_7;
}
long long po(long a, long b){
    if(b==0){
        return 1;
    }
    
    long long z = po(a,b/2);
    z = mod(z*z);
    if(b%2!=0){
        z = mod(a*z);
    }
    return z;
}

bool prime_(int n){
  if(n==1){
    return false;
  }else if(n==2){
    return true;
  }else{
    for(int i=2;i<=std::sqrt(n);i++){
      if(n%i==0){
        return false;
      }
    }
    return true;
  }
}

long long gcd_(long long a, long long b){
  if(a<b){
    std::swap(a,b);
  }
  if(a%b==0){
    return b;
  }else{
    return gcd_(b,a%b);
  }
}
 
long long lcm_(long long x, long long y){
  return (x/gcd_(x,y))*y;
}

//using namespace std;
//using namespace boost::multiprecision;
//using namespace __gnu_pbds;

// Binary Indexed Tree (Fenwick Tree)
template<typename T> // T: type of cost
struct BIT {
    int n;
    std::vector<T> d;
    BIT(int n=0):n(n),d(n+1) {}
    //配列のインデックスiの要素d[i]に対して、xを加算する。
    void add(int i, T x=1) {
        for (i++; i <= n; i += i&-i) {
            d[i] += x;
        }
    }
    //配列においてインデックスがi以下の要素d[j]たちの和を計算して返す。
    T sum(int i) {
        T x = 0;
        for (i++; i; i -= i&-i) {
            x += d[i];
        }
        return x;
    }
};

int main(){
  using namespace std;
  int n;
  cin>>n;
  int a[n], b[n];
  REP(i,n){
    cin>>a[i];
  }
  REP(i,n){
    cin>>b[i];
  }
  vector<vector<long long>> dp(1<<n, vector<long long>(51, INF));
  dp[0][0] = 0;
  REP(s, (1<<n)){
    //REP(v,51){
    vector<int> ni;//sにおいて立ってないビット
    int bits = 0;//sにおいて立っているビットの数
    BIT<int> tree(n);//sにおいて立っているビットの管理
    vector<int> upbits//sにおいて立っているビット
    REP(j,n){
      if(s & (1<<j)){
        bits++;
        tree.add(j);
        upbits.push_back(j);
      }else{
        ni.push_back(j);
      }
    }
    for(int now:upbits){
      int v;
      if(bits == 0){
        v = 0;
      }else{
        if(((bits - 1) - now) % 2 == 0){
          v = a[i];
        }else{
          v = b[i];
        }
      }
      for(int i:ni){
        int nv;
        if((bits - i) % 2 == 0){
          nv = a[i];
        }else{
          nv = b[i];
        }
        if(nv < v)continue;
        long long cnt = bits - tree.sum(i);
        dp[s | (1<<i)][nv] = min(dp[s | (1<<i)][nv], dp[s][v] + cnt);
      }
    }
  }
  long long res = INF;
  REP(v,51){
    res = min(res, dp[(1<<n) - 1][v]);
  }
  if(res == INF){
    cout<<-1<<endl;
  }else{
    cout<<res<<endl;
  }
  return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:6:18: error: expected initializer before 'for'
    6 | #define REP(i,n) for(int i=0;i<n;i++)
      |                  ^~~
a.cc:110:5: note: in expansion of macro 'REP'
  110 |     REP(j,n){
      |     ^~~
a.cc:110:9: error: 'j' was not declared in this scope
  110 |     REP(j,n){
      |         ^
a.cc:6:30: note: in definition of macro 'REP'
    6 | #define REP(i,n) for(int i=0;i<n;i++)
      |                              ^
a.cc:119:17: error: 'upbits' was not declared in this scope; did you mean 'bits'?
  119 |     for(int now:upbits){
      |                 ^~~~~~
      |                 bits
a.cc:125:17: error: 'i' was not declared in this scope
  125 |           v = a[i];
      |                 ^
a.cc:127:17: error: 'i' was not declared in this scope
  127 |           v = b[i];
      |                 ^