Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

#define rep(i,x,y) for(int i=(x);i<(y);i++)

typedef long long int ll;
typedef pair<int, int> pil;
template<typename T> using  vec = std::vector<T>;

const int inf = 1 << 30;
const long long int infll = 1LL << 62;
const double eps = 1e-9;
const int dx[] = { 1,0,-1,0 };
const int dy[] = { 0,1,0,-1 };

int main()
{
	std::cin.tie(0);
	std::ios::sync_with_stdio(false);
	cout.setf(ios::fixed);
	cout.precision(10);

	int n, m;

	cin >> n >> m;

	bool zeroflag = true;
	bool oneflag = true;

	vec<int> bits(n);
	vec<int> p(m);
	int zerocount = 0;
	int onecount = 0;

	rep(i, 0, n) {
		cin >> bits[i];

		if (bits[i] == 0) {
			zerocount++;
		}
		else {
			onecount++;
		}
	}

	rep(i, 0, m) {
		cin >> p[i];
	}

	vec<int> ans(n);
	int cur = 0;
	int zc = 0;
	int oc = 0;

	rep(i, 0, m) {
		rep(j, 0, p[i]) {
			if (i % 2 == 0) {
				ans[cur] = 0;
				cur++;
				zc++;
			}
			else {
				ans[cur] = 1;
				cur++;
				oc++;
			}
		}
	}

	int answer = inf;

	if (zc == zerocount&&oc == onecount) {
		int tempans = 0;
		vec<int> tempbits = bits;

		rep(i, 0, n) {
			if (tempbits[i] == ans[i]) {
				continue;
			}

			rep(j, i + 1, n) {
				if (tempbits[j] == ans[i]) {
					int temp = tempbits[j];

					for (int k = j; k > i; k--) {
						tempbits[k] = tempbits[k - 1];
					}

					tempbits[i] = temp;
					tempans += j - i;
					break;
				}
			}
		}

		answer = min(answer, tempans);
	}

	cur = 0;
	zc = 0;
	oc = 0;

	rep(i, 0, m) {
		rep(j, 0, p[i]) {
			if (i % 2 == 0) {
				ans[cur] = 1;
				cur++;
				oc++;
			}
			else {
				ans[cur] = 0;
				cur++;
				zc++;
			}
		}
	}

	if (zc == zerocount&&oc == onecount) {
		int tempans = 0;
		vec<int> tempbits = bits;

		rep(i, 0, n) {
			if (tempbits[i] == ans[i]) {
				continue;
			}

			rep(j, i + 1, n) {
				if (tempbits[j] == ans[i]) {
					int temp = tempbits[j];

					for (int k = j; k > i; k--) {
						tempbits[k] = tempbits[k - 1];
					}

					tempbits[i] = temp;
					tempans += j - i;
					break;
				}
			}
		}

		answer = min(answer, tempans);
	}

	cout << answer << endl;

	return 0;
}