Language: C++

onst int inf = 1e8;

int A[MAX_N],B[MAX_N];
int dp[(1<<MAX_N)][MAX_val];

void chmin(int &a, int b ){ if( a > b ){ swap(a,b);} return;}

signed main(void){
  
  int N; cin >> N;
  for(int i = 0; i < N; i++){ cin >> A[i];}
  for(int i = 0; i < N; i++){ cin >> B[i];}
  
  for(int i = 0; i < (1<<MAX_N); i++){
    for(int j = 0; j < MAX_val; j++){ 
      dp[i][j] = inf;
    }
  }
  
  dp[0][0] = 0; //まだ１枚も揃っていない状態を0とする
  
  for(int bit = 0; bit < (1<<N); bit++){
    //次に何を後ろに持ってくるかが問題
    int cnt = 0;
    for(int k = 0; k < MAX_N; k++){ 
      if( bit>>k&1 ){ cnt++;}
    } //今まで揃えたカードの枚数を記録しておく
    
    for(int last = 0; last < MAX_val; last++){
      if( dp[bit][last] == inf ){ continue;} //ここから遷移を伸ばす必要はない
      
      int dist = 0;
      
      for(int i = 0; i < N; i++){ 
        
       
        if( !(bit & (1<<i)) ){ 
          //次にこれを後ろに持ってくることを考える
          //今、このカードはcnt+dist番目にいる
          
          int origin = i; //元々はi番目にいた　今はcnt+distにいる
                          //これをcntまで持っていきたい
          
          int next;
          int parity = abs(cnt-origin)%2;
          
          
          if(parity){ next = B[i];}
          else{ next = A[i];}
         
          
          if( next < last ){ continue;}
          
          chmin(dp[bit|(1<<i)][next], dp[bit][last]+dist );
          dist++;
          
        }//if-bit
      } //for-i
    }//for-last
  }//for-bit
  
  int ans = inf;
  
  for(int i = 1; i < MAX_val; i++){
    chmin(ans, dp[(1<<N)-1][i]);
  }
   
  if( ans == inf ){ ans = -1;}
  
  cout << ans << endl;
  
  return 0;
}

Compiler output:
a.cc:1:1: error: 'onst' does not name a type; did you mean 'const'?
    1 | onst int inf = 1e8;
      | ^~~~
      | const
a.cc:3:7: error: 'MAX_N' was not declared in this scope
    3 | int A[MAX_N],B[MAX_N];
      |       ^~~~~
a.cc:3:16: error: 'MAX_N' was not declared in this scope
    3 | int A[MAX_N],B[MAX_N];
      |                ^~~~~
a.cc:4:12: error: 'MAX_N' was not declared in this scope
    4 | int dp[(1<<MAX_N)][MAX_val];
      |            ^~~~~
a.cc:4:20: error: 'MAX_val' was not declared in this scope
    4 | int dp[(1<<MAX_N)][MAX_val];
      |                    ^~~~~~~
a.cc: In function 'void chmin(int&, int)':
a.cc:6:42: error: 'swap' was not declared in this scope
    6 | void chmin(int &a, int b ){ if( a > b ){ swap(a,b);} return;}
      |                                          ^~~~
a.cc: In function 'int main()':
a.cc:10:10: error: 'cin' was not declared in this scope
   10 |   int N; cin >> N;
      |          ^~~
a.cc:11:38: error: 'A' was not declared in this scope
   11 |   for(int i = 0; i < N; i++){ cin >> A[i];}
      |                                      ^
a.cc:12:38: error: 'B' was not declared in this scope
   12 |   for(int i = 0; i < N; i++){ cin >> B[i];}
      |                                      ^
a.cc:14:26: error: 'MAX_N' was not declared in this scope
   14 |   for(int i = 0; i < (1<<MAX_N); i++){
      |                          ^~~~~
a.cc:15:24: error: 'MAX_val' was not declared in this scope
   15 |     for(int j = 0; j < MAX_val; j++){
      |                        ^~~~~~~
a.cc:16:7: error: 'dp' was not declared in this scope
   16 |       dp[i][j] = inf;
      |       ^~
a.cc:16:18: error: 'inf' was not declared in this scope; did you mean 'int'?
   16 |       dp[i][j] = inf;
      |                  ^~~
      |                  int
a.cc:20:3: error: 'dp' was not declared in this scope
   20 |   dp[0][0] = 0; //まだ１枚も揃っていない状態を0とする
      |   ^~
a.cc:25:24: error: 'MAX_N' was not declared in this scope
   25 |     for(int k = 0; k < MAX_N; k++){
      |                        ^~~~~
a.cc:29:30: error: 'MAX_val' was not declared in this scope
   29 |     for(int last = 0; last < MAX_val; last++){
      |                              ^~~~~~~
a.cc:30:28: error: 'inf' was not declared in this scope; did you mean 'int'?
   30 |       if( dp[bit][last] == inf ){ continue;} //ここから遷移を伸ばす必要はない
      |                            ^~~
      |                            int
a.cc:45:24: error: 'abs' was not declared in this scope
   45 |           int parity = abs(cnt-origin)%2;
      |                        ^~~
a.cc:48:30: error: 'B' was not declared in this scope
   48 |           if(parity){ next = B[i];}
      |                              ^
a.cc:49:24: error: 'A' was not declared in this scope
   49 |           else{ next = A[i];}
      |                        ^
a.cc:62:13: error: 'inf' was not declared in this scope; did you mean 'int'?
   62 |   int ans = inf;
      |             ^~~
      |             int
a.cc:64:22: error: 'MAX_val' was not declared in this scope
   64 |   for(int i = 1; i < MAX_val; i++){
      |                      ^~~~~~~
a.cc:70:3: error: 'cout' was not declared in this scope
   70 |   cout << ans << endl;
      |   ^~~~
a.cc:70:18: error: 'endl' was not declared in this scope
   70 |   cout << ans << endl;
      |                  ^~~~