Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include <bits/stdc++.h>

using namespace std;

int N,M;
vector<int> b,p;

int solve(int first) {
	int res=0;
	int t=0,tsum=0;
	vector<int> tmp(b);
	for(int i=0;i<N;++i) {
		int j=i;
		if(t&1)
			while(tmp[j]==first) ++j;
		else
			while(tmp[j]!=first) ++j;
		swap(tmp[i],tmp[j]);
		res+=j-i;
		++tsum;
		if(p[t]==tsum) tsum=0,++t;
	}
	return res;
}

int main() {
	cin >> N >> M;
	b.resize(N); p.resize(M);
	int num1=0,sume=0,sumo=0;
    for(int i=0;i<N;++i) {
		cin >> b[i];
		if(b[i]) ++num1;
	}
	for(int i=0;i<M;++i) {
		cin >> p[i];
		if(i&1) sumo+=p[i];
		else sume+=p[i];
	}
	int ans=1e9;
	if(sume>=sumo) ans=min(ans,solve(num1<=N-num1?0:1));
	if(sume<=sumo) ans=min(ans,solve(num1<=N-num1?1:0));
	cout << ans << endl;
	
	return 0;
}
