Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include <iostream>
using namespace std;

const int INF = 1 << 29;

int N, M, b[15], p[15], bb[15];

int calc(int c) {
    for (int j = 0; j < N; ++j) b[j] = bb[j];

    int swaps = 0;

    int idx = 0;
    for (int j = 0; j < M; ++j) {
        for (int k = 0; k < p[j]; ++k) {
            int kk = idx+k;
            while (kk < N && b[kk] != c) ++kk;
            if (kk == N) return INF;

            swaps += kk-(idx+k);
            for (int m = kk; m > idx+k; --m) {
                swap(b[m], b[m-1]);
            }
        }
        c = 1-c;
        idx += p[j];
    }

    return swaps;
}

int main() {
    cin >> N >> M;
    for (int j = 0; j < N; ++j) { cin >> b[j]; bb[j] = b[j]; }
    for (int j = 0; j < M; ++j) { cin >> p[j]; }

    cout << min(calc(0), calc(1)) << endl;
    return 0;
}