Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int INF = (int)1e9 + 7;

int main() {
    int n, m; cin >> n >> m;
    string b;
    vector<int> cnt01(2);
    for (int i = 0; i < n; i++) {
        char bi; cin >> bi; b += bi; cnt01[bi - '0']++;
    }
    vector<int> p(m);
    for (int &pi: p) cin >> pi;
    int cnt = INF;
    for (int t0: {0, 1}) {
        int t = t0;
        string nxt = "";
        vector<int> tmp01(2);
        for (int i = 0; i < m; i++) {
            nxt += string(p[i], '0' + t);
            tmp01[t] += p[i];
            t ^= 1;
        }
        if (tmp01[0] != cnt01[0] || tmp01[1] != cnt01[1]) continue;
        string cur = b;
        int tmp = 0;
        for (int i = 0; i < n; i++) if (cur[i] != nxt[i]) {
            int it = i;
            while (cur[it] != nxt[i]) it++;
            tmp += it - i;
            swap(cur[it], cur[i]);
        }
        cnt = min(cnt, tmp);
    }
    cout << cnt << endl;
    return 0;
}
