Language: Java

import java.io.*;
import java.util.*;

public class s749703010 {

    static class Pair {
        int l, r;

        Pair(int a, int b) {
            l = a;
            r = b;
        }

        @Override
        public String toString() {
            return l + " " + r;
        }
    }

    static int[][][] memo;
    static int[][] a;
    static int n;

    static int dp(int fliped, int idx, int msk) {
        if (msk == (1 << n) - 1) return 0;
        if (memo[fliped][idx][msk] != -1) return memo[fliped][idx][msk];
        int x = Integer.bitCount(msk);
        int ff = 0;
        int ans = (int) 1e9;
        for (int i = 0; i < n; i++) {
            if ((msk & (1 << i)) == 0) {
                int nf = x % 2;
                if (idx == n || a[fliped][idx] <= a[(nf + (ff % 2)) % 2][i]) {
                    ans = Math.min(ans, ff + dp((nf + (ff % 2)) % 2, i, msk | 1 << i));
                }
                ff++;
            } else x--;
        }
        return memo[fliped][idx][msk] = ans;
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
        n = sc.nextInt();
        a = new int[2][n];
        for (int i = 0; i < n; i++) a[0][i] = sc.nextInt();
        for (int i = 0; i < n; i++) a[1][i] = sc.nextInt();
        memo = new int[2][n + 1][1 << n];
        for (int[][] z : memo) for (int[] x : z) Arrays.fill(x, -1);
        int ans = dp(0, n, 0);
        pw.println(ans >= 1e8 ? -1 : ans);
        pw.flush();
    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(FileReader r) {
            br = new BufferedReader(r);
        }

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public String nextLine() throws IOException {
            return br.readLine();
        }

        public double nextDouble() throws IOException {
            String x = next();
            StringBuilder sb = new StringBuilder("0");
            double res = 0, f = 1;
            boolean dec = false, neg = false;
            int start = 0;
            if (x.charAt(0) == '-') {
                neg = true;
                start++;
            }
            for (int i = start; i < x.length(); i++)
                if (x.charAt(i) == '.') {
                    res = Long.parseLong(sb.toString());
                    sb = new StringBuilder("0");
                    dec = true;
                } else {
                    sb.append(x.charAt(i));
                    if (dec) f *= 10;
                }
            res += Long.parseLong(sb.toString()) / f;
            return res * (neg ? -1 : 1);
        }

        public boolean ready() throws IOException {
            return br.ready();
        }
    }
}


Example test case(s):
input: 3
3 4 3
3 2 3

output: 1