Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

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

#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)

using namespace std;
const int IINF = INT_MAX;
int n,m,one;
int b[20],p[20],bc[1<<16],buf[20];

bool isValid(int bitmask){
  vector<int> vec;
  rep(i,n){
    int cnt = 1;
    int color = ( bitmask >> i ) & 1;
    while( i+1 < n && ( ( bitmask >> (i+1) ) & 1 ) == color ) {
      ++cnt;
      ++i;
    }
    vec.push_back(cnt);
  }
  if( vec.size() != m ) return false;
  rep(i,m) if( vec[i] != p[i] ) return false;
  return true;
}

void compute(){
  int mini = IINF;
  rep(bitmask,(1<<n)) if( bc[bitmask] == one ) {
    if( !isValid(bitmask) ) continue;
    int cost = 0;
    rep(i,n) buf[i] = b[i];
    rep(i,n) if( buf[i] != ( ( bitmask >> i ) & 1 ) ) {
      int k=-1;
      REP(j,i+1,n) if( buf[j] == ( ( bitmask >> i ) & 1 ) ) { k = j; break; }
      for(int j=k;j>i;j--) { swap(buf[j],buf[j-1]); ++cost; }
    }
    mini = min(mini,cost);
  }
  cout << mini << endl;
}

int main(){
  rep(i,(1<<16)) bc[i] = __builtin_popcount(i);
  cin >> n >> m;
  rep(i,n) { cin >> b[i]; one += b[i]; }
  rep(i,m) cin >> p[i];

  compute();
  return 0;
}