Language: C++

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define repA(i, a, n) for(int  i = a; i <= (n); ++i)
#define all(x) x.begin(), x.end()
#define sz(x) (int) (x).size()
#define fill(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
#define endl '\n'
#define init(n) int n;cin>>n;int a[n];rep(i,n)cin>>a[i];
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long int> vll;
typedef vector<pii> vii;

int main() {
    cin.sync_with_stdio(0); cin.tie(0); 
    cin.exceptions(cin.failbit);
    int T=1;
    // cin>>T;
    while(T--){
        int n;cin>>n;
        int a[n],b[n];
        rep(i,n)cin>>a[i];
        rep(i,n)cin>>b[i];
        int total=(1<<(n))-1;
        int ans=INT_MAX;
        for(int mask=0;mask<=total;mask++){
            pair<int,int> arr[n];
            for(int j=0;j<n;j++){
                arr[j].fst=a[j];
                if((mask&(1<<j))!=0){
                    arr[j].fst=b[j];
                }
                arr[j].snd=j;
            }
            int temp=0;
            for(int i=0;i<n;i++){
                for(int j=i+1;j<n;j++){
                    if(arr[i].fst>arr[j].fst)temp++;
                }
            }
            sort(arr,arr+n);
            int flag=1;
            int last=-1;
            for(int i=0;i<n;i++){
                if(((abs(arr[i].snd-i))%2)==0){
                    if((mask&(1<<i))!=0){
                        if(last!=-1){
                            if(arr[last].fst==a[i].fst){
                                int e=(i-last);
                                if(e%2==0){
                                    flag=0;
                                    temp=temp+(e+1)+(e/2);
                                }
                                else{
                                    break;
                                }
                            }   
                            else{
                                flag=0;
                                break;
                            }
                            last=-1;continue;
                        }
                        last=i;
                        flag=0;
                    }
                }
                else{
                    if((mask&(1<<i))==0){
                        if(last!=-1){
                            if(arr[last].fst==a[i].fst){
                                int e=(i-last);
                                if(e%2==0){
                                    flag=1;
                                    temp=temp+(e+1)+(e/2);
                                }
                                else{
                                    flag=0;
                                    break;
                                }
                            }   
                            else{
                                flag=0;
                                break;
                            }
                            last=-1;continue;
                        }
                        last=i;
                        flag=0;
                    }
                }
            }
            if(flag){
                ans=min(ans,temp);
            }
        }
        if(ans==INT_MAX)ans=-1;
        cout<<ans;
    }
    return 0;   
}

Compiler output:
a.cc: In function 'int main()':
a.cc:8:13: error: request for member 'first' in 'a[i]', which is of non-class type 'int'
    8 | #define fst first
      |             ^~~~~
a.cc:55:52: note: in expansion of macro 'fst'
   55 |                             if(arr[last].fst==a[i].fst){
      |                                                    ^~~
a.cc:8:13: error: request for member 'first' in 'a[i]', which is of non-class type 'int'
    8 | #define fst first
      |             ^~~~~
a.cc:78:52: note: in expansion of macro 'fst'
   78 |                             if(arr[last].fst==a[i].fst){
      |                                                    ^~~