Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include <cstdio>
#include <algorithm>
using namespace std;

bool make_code(int n, int m, int *b, int *p, int *a, int t)
{
    for (int i = 0; i < n; ++i)
        a[i] = -1;
    int s = 0;
    for (int i = 0; i < m; ++i) {
        int k = 0;
        for (int j = 0; j < p[i]; ++j) {
            for (; k < n && !(a[k] == -1 && b[k] == t); ++k) {}
            a[k] = s++;
        }
        t = !t;
    }
    for (int i = 0; i < n; ++i)
        if (a[i] == -1)
            return false;
    return true;
}

int bsort(int n, int *a)
{
    int ans = 0;
    for (int i = n; i >= 0; --i) {
        for (int j = 1; j < i; ++j) {
            if (a[j - 1] > a[j]) {
                swap(a[j - 1], a[j]);
                ++ans;
            }
        }
    }
    return ans;
}

int main()
{
    int n, m;
    scanf("%d %d", &n, &m);

    int b[15];
    for (int i = 0; i < n; ++i)
        scanf("%d", &b[i]);

    int p[15];
    for (int i = 0; i < m; ++i)
        scanf("%d", &p[i]);

    int a[15];
    int ans = 1000000;
        //for (int i = 0; i < n; ++i) printf(" %2d", a[i]); puts("");
    if (make_code(n, m, b, p, a, 0)) {
        ans = min(ans, bsort(n, a));
    }
    if (make_code(n, m, b, p, a, 1)) {
        ans = min(ans, bsort(n, a));
    }
    printf("%d\n", ans);

    return 0;
}