Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include <algorithm>
#include <iostream>
#include <limits>
#include <utility>
#include <vector>

using namespace std;

int main()
{
    int n, m;
    cin >> n >> m;
    vector<int> bs;
    for (int i = 0; i < n; ++i) {
        int b;
        cin >> b;
        bs.push_back(b);
    }
    vector<int> ps;
    for (int i = 0; i < m; ++i) {
        int p;
        cin >> p;
        ps.push_back(p);
    }
    auto expandRunLength = [](const vector<int> rl) {
        vector<int> startingZero;
        vector<int> startingOne;
        bool state = false;
        for (auto&& i : rl) {
            for (int j = 0; j < i; ++j) {
                startingZero.push_back(state ? 1 : 0);
                startingOne.push_back(state ? 0 : 1);
            }
            state = !state;
        }
        return make_pair(startingZero, startingOne);
    };
    auto countSwap = [& target = bs](const vector<int>& s) {
        if (count(begin(target), end(target), 1) !=
            count(begin(s), end(s), 1)) {
            return numeric_limits<int>::max();
        }
        int counter = 0;
        auto ss = s;
        for (size_t i = 0; i < target.size(); ++i) {
            if (ss[i] == target[i]) {
                continue;
            }

            for (size_t j = i + 1; j < target.size(); ++j) {
                if (ss[j] == target[i]) {
                    counter += (j - i);
                    swap(ss[j], ss[i]);
                    break;
                }
            }
        }
        return counter;
    };
    auto ss = expandRunLength(ps);
    cout << min(countSwap(ss.first), countSwap(ss.second)) << endl;
}
