Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include <cstdio>
#include <algorithm>
using namespace std;

int judge(int i)
{
	int sum=0;
	while(i)
	{
		if(i&1)sum++;
		i>>=1;
	}
	return sum;
}

int res(int a,int b,int c)
{
	int x=(a&(1<<b)),y=(a&(1<<c));
	if((x&&y)||((!x)&&(!y)))return a;
	if(x)
	{
		a^=1<<b;
		a|=1<<c;
	}
	else
	{
		a^=1<<c;
		a|=1<<b;
	}
	return a;
}

int f[70000];

int n,m;

void print(int deep,int x)
{
	if(deep==n+1)return;
	print(deep+1,x>>1);
	printf("%d",x&1);
}

int a1[20],a2[20]; 

int main(void)
{
	int x=0,count;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		int y;scanf("%d",&y);
		x=(x|y)<<1;
	}
	x>>=1;	
	count=judge(x);
	int a=0,b=0;
	for(int i=1;i<=m;i++)
	{
		int y;
		scanf("%d",&y);
		for(int j=0;j<y;j++)
		if(i&1)
		{
			a=(a+1)*2;
			b*=2;
		}
		else
		{
			b=(b+1)*2;
			a*=2;
		}
	}

	a>>=1;b>>=1;
	int y,top1,top2,ans1=0xffffff,ans2=0xffffff;
	if(judge(x)==judge(a))
	{	
		y=a;top1=0,top2=0;
		for(int i=0;i<n;i++)
		if((x^y)&(1<<i))
		{
			if(y&(1<<i))a1[top1++]=i;
			else a2[top2++]=i;
		}
		ans1=0;
		for(int i=0;i<top1;i++)
		{
			ans1+=abs(a1[i]-a2[i]);
		}
	}
	if(judge(x)==judge(b))
	{
		y=b;top1=0,top2=0;
		for(int i=0;i<n;i++)
		if((x^y)&(1<<i))
		{
			if(y&(1<<i))a1[top1++]=i;
			else a2[top2++]=i;
		}
		ans2=0;
		for(int i=0;i<top1;i++)
		{
			ans2+=abs(a1[i]-a2[i]);
		}
	}
	printf("%d\n",min(ans1,ans2));
	/*
	int ans=0,flag=0;
	f[x]=2;
	while(true)
	{
		flag=0;
		for(int i=0;i<((1<<n)-1);i++)
		if(f[i]==2)
		for(int j=0;j<n;j++)
		for(int k=j+1;k<n;k++)
		if(f[res(i,j,k)]==0)
		{
			flag=1;
			int x;
			f[x=res(i,j,k)]=1;
			print(1,x);printf("\n");
		}
		printf("\n");
		ans++;
		for(int i=0;i<0xffff;i++)if(f[i]==1)f[i]=2;
		if(f[a]||f[b]){break;}
		if(flag==0){ans=-1;break;}
	}*/
	//	printf("%d\n",ans);
	return 0;
}

/*
6 3
1 0 0 1 0 1
1 3 2


*/