Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include <bits/stdc++.h>
using namespace std;
const int INF=1e9;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N,M; cin >> N >> M;
    vector<int> b(N),p(M),a(N);
    for (int i=0;i<N;++i) cin >> b[i];
    for (int i=0;i<M;++i) cin >> p[i];
    int now=0,one=1;
    for (int i=0;i<M;++i){
        for (int j=0;j<p[i];++j){
            a[now+j]=(one?1:0);
        }
        now+=p[i]; one^=1;
    }
    vector<int> pos;
    for (int i=0;i<N;++i) if (b[i]) pos.emplace_back(i);
    int ans=INF,cnt=0;
    now=0,one=0;
    for (int i=0;i<N;++i) if (a[i]){
        if (now==pos.size()){cnt=INF; break;}
        if (a[i]) cnt+=abs(pos[now++]-i),++one;
    }
    if (one!=pos.size()) cnt=INF;
    ans=min(ans,cnt);
    cnt=now=one=0;
    for (int i=0;i<N;++i){
        if (!a[i]){
            if (now==pos.size()){cnt=INF; break;}
            cnt+=abs(pos[now++]-i),++one;
        }
    }
    if (one!=pos.size()) cnt=INF;
    ans=min(ans,cnt);
    cout << ans << '\n';
}
