Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

int x[65536];

int inversions(int p, int n, int q, int r) {
	int x3[15], y[15], z;
	memset(x, 127, sizeof(x));
	x[p] = 0;
	for (int i = 0; i < n * n; i++){
		for (int j = 0; j < (1 << n); j++) {
			if (x[j] < 1000) {
				for (int k = 0; k < n; k++) {
					x3[k] = (j / (1 << k)) % 2;
				}
				for (int k = 0; k < n - 1; k++) {
					z = 0;
					if (x3[k] + x3[k + 1] == 1) {
						for (int l = 0; l < n; l++) {
							y[l] = x3[l];
						}
						y[k] = x3[k + 1];
						y[k + 1] = x3[k];
						for (int l = 0; l < n; l++) {
							z += y[l] * (1 << l);
						}
						x[z] = min(x[z], x[j] + 1);
					}
				}
			}
		}
	}
	return min(x[q], x[r]);
}

int n, m, a, s;
int x1[15], x2[15], cnt, r1, r2;

int main() {
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> a; s += (1 << i) * a;
	}
	for (int i = 0; i < m; i++) {
		cin >> a;
		for (int j = 0; j < a; j++) {
			x1[j + cnt] = i % 2;
			x2[j + cnt] = (i + 1) % 2;
		}
		cnt += a;
	}
	for (int i = 0; i < n; i++) {
		r1 += x1[i] * (1 << i);
		r2 += x2[i] * (1 << i);
	}
	cout << inversions(s, n, r1, r2) << endl;
	return 0;
}