Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;

const int INF = 1 << 30;

int calc_cost(vector<int>& b1, vector<int>& b2) {
    int cnt1 = 0, cnt2 = 0;
    for (int i = 0; i < b1.size(); i++) {
        if (b1[i] == 1) cnt1++;
    }
    for (int i = 0; i < b2.size(); i++) {
        if (b2[i] == 1) cnt2++;
    }
    if (cnt1 != cnt2) return INF;
    vector<int> idx1, idx2;
    for (int i = 0; i < b1.size(); i++) {
        if (b1[i] == 1) idx1.push_back(i);
    }
    for (int i = 0; i < b2.size(); i++) {
        if (b2[i] == 1) idx2.push_back(i);
    }
    int res = 0;
    for (int i = 0; i < idx1.size(); i++) {
        res += abs(idx1[i] - idx2[i]);
    }
    return res;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, M;
    cin >> N >> M;
    vector<int> B(N), P(M);
    for (int i = 0; i < N; i++) cin >> B[i];
    for (int i = 0; i < M; i++) cin >> P[i];
    vector<int> b1, b2;
    for (int i = 0; i < M; i++) {
        if (i % 2 == 0) {
            for (int j = 0; j < P[i]; j++) {
                b1.push_back(1);
            }
        }
        else {
            for (int j = 0; j < P[i]; j++) {
                b1.push_back(0);
            }
        }
    }
    for (int i = 0; i < M; i++) {
        if (i % 2) {
            for (int j = 0; j < P[i]; j++) {
                b2.push_back(1);
            }
        }
        else {
            for (int j = 0; j < P[i]; j++) {
                b2.push_back(0);
            }
        }
    }
    int ans = INF;
    ans = min(ans, calc_cost(b1, B));
    ans = min(ans, calc_cost(b2, B));
    cout << ans << endl;
    return 0;
}
