Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
typedef pair<int,int> PI;

int n,m;
int b[15];
int p[15];
int to[2][15];	//完成系のビット列(2通りしかない！！)
int dp[1<<15];

queue<PI> que;
int bfs(){
	int i,x,tesu;
	PI now;
	
	x = 0;
	for( i = 0; i < n; i++ ){
		x += (1<<i)*b[i];
	}
	que.push( PI(x,0) );
	
	while(!que.empty() ){
		now = que.front();
		x = now.first;
		tesu = now.second;
		que.pop();
		
		if( dp[x] <= tesu )
			continue;
		dp[x] = tesu;
		//cout << x << " " << dp[x] << endl;
		//xの下位iビット目と下位i-1ビット目を交換
		for( i = 1; i < n; i++ ){
			int bi = (x>>i)%2;
			int bi_1 = (x>>(i-1))%2;
			int diff;
			if( bi == bi_1 )
				continue;
			else if( bi == 1 ){
				diff = ( ( 1<<(i-1) ) - (1<<i) );
			}
			else if( bi_1 == 1 ){
				diff =( -( 1<<(i-1) ) + (1<<i) );
			}
			que.push( PI( x + diff, tesu+1 ) );
		}
	}
	
	int t,u;
	t = 0;
	u = 0;
	for( i = 0; i < n; i++ ){
		t += to[0][i]<<i;
		u += to[1][i]<<i;
	}
	return min( dp[t], dp[u] );
}

int main(){
	int i,j;
	cin >> n >> m;
	
	for( i = 0; i < (1<<15); i++ )
		dp[i] = 1000000;
		
	for( i = 0; i < n; i++ )
		cin >> b[i];
	for( i = 0; i < m; i++ )
		cin >> p[i];
	
	int cor = 0;
	for( i = 0; i < m; i++ ){
		for( j = 0; j < p[i]; j++ ){
			to[0][cor] = i%2;
			to[1][cor++] = (i+1)%2;
		}
	}
	cout << bfs() << endl;
	return 0;
}