Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include <bits/stdc++.h>
using namespace std;
const double eps=1e-9;
typedef vector<int> vi;
typedef long long ll;
typedef pair<string,string> pss;
vector<int> fi;
vector<int> ope;
int b;
int p[16];
vector<int> cands[2];
//cands[0] 0 start
//cands[1] 1 start
bool canuse[2];
int ans=1e7;
int tmp=0;
int N,M,onenum=0,zeronum=0;
int main(void)
{
  memset(canuse,false,sizeof(canuse));
  cin >> N >> M;
  for(int i=0;i<N;i++)
    {
      int b;
      cin >> b;
      fi.push_back(b);
      if(fi[i]==0) zeronum++;
      else onenum++;
    }
  for(int i=0;i<M;i++)
    {
      cin >> p[i];
    }
  for(int k=0;k<2;k++)
    {
      int cnt=k;
      for(int j=0;j<M;j++)
	{
	  for(int i=0;i<p[j];i++)
	    {
	      cands[k].push_back(cnt%2);
	    }
	  cnt++;
	}
      if(count(cands[k].begin(),cands[k].end(),1)==onenum) canuse[k]=true;
    }
  for(int pat=0;pat<2;pat++)
    {
      /*cout << pat << ' ' << canuse[pat] << endl;
      for(int check=0;check<N;check++)
	{
	  cout << cands[pat][check] << ' ';
	}
	cout << endl;*/
      if(canuse[pat])
	{
	  ope=fi;
	  int now=0;
	  tmp=0;
	  for(int j=0;j<M;j++)
	    {
	      //cout << "now " << now << endl;
	      if(ope==cands[pat]) break;
	      for(int i=now;i<now+p[j];i++)
		{
		  int target=-1;
		  if(ope[i]!=(pat+j)%2)
		    {
		      target=i;
		      while(ope[target]!=(pat+j)%2)
			{
			  target++;
			}
		    }
		  //cout << target << endl;
		  if(target!=-1)
		    {
		      swap(ope[i],ope[target]);
		      tmp+=(target-i);
		    }
		  // cout << i << ' ' << target << endl;
		  /*for(int check=0;check<N;check++)
		    {
		      cout << ope[check] << ' ';
		    }
		    cout << endl;*/
		}
	      now+=p[j];
	    }
	  ans=min(ans,tmp);
	}
      else continue;
    }
  cout << ans << endl;
  return 0;
}

