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

void
print_bits(vector<bool>v)
{
	for (auto x:v)
		cout << x << " ";
	cout << endl;
}

	vector<bool>
bits_start(bool b, const vector<int> p)
{
	bool bit = b;
	vector<bool> bits;
	for (int i = 0; i < p.size(); i++)
	{
		for (int j = 0; j < p[i]; j++)
			bits.push_back(bit);
		bit = !bit;
	}
	return bits;
}

	vector<vector<bool> >
create_bits (const vector<int> p)
{
	vector<vector<bool> > created;
	created.push_back(bits_start(0, p));
	created.push_back(bits_start(1, p));
	return created;
}

int
count_true (const vector<bool> b)
{
	int count_true = 0;
	for (int i = 0; i < b.size(); i++)
		if (b[i])
			count_true++;
	return count_true;
}

	int
code_distance (const vector<bool> ori, const vector<bool> created)
{
	if (count_true(ori) != count_true(created))
		return 20;

	int len = ori.size();
	int distance = 0;
	for (int i = 0; i < len; i++)
		if (ori[i] != created[i])
			distance++;
	return distance;
}

int
which_shorter (const vector<bool> b, 
		const vector<vector<bool> > bits_list)
{
	if(code_distance(b, bits_list[0]) < 
			code_distance(b, bits_list[1]))
		return 0;
	else
		return 1;
}

int
same_at (vector<bool>ori, vector<bool>created)
{
	int ok = -1;
	for (int i = 0; i < ori.size(); i++)
		if (ori[i] == created[i])
			ok++;
		else
			break;
	return ok;
}

int
search_closest_true(bool target, int suffix, 
		vector<bool>ori, vector<bool> created)
{
	int len = created.size();
	int next, before;
	next = before = 100;
	int left_terminal = same_at(ori, created);
#ifdef D
	print_bits(ori);
	print_bits(created);
	cout << "same at " << left_terminal << endl;
#endif
	if (suffix != 0)
	{
		before = 0;
		int i;
		for (i = suffix; left_terminal < i ; i--)
			if (created[i] == target)
				break;
			else
				before--;
		if (left_terminal >= i)
			before = 100;
	}
	if (suffix != len-1)
	{
		next = 0;
		for (int i = suffix; i < len; i++)
			if (created[i] == target)
				break;
			else
				next++;
	}
#ifdef D
	cout << "before " << before <<endl;
	cout << "next " << next <<endl;
	cout << "closest " << min(abs(before), abs(next)) << endl;
#endif
	return min(abs(before), abs(next));
}

	int
swap (int step, int dist, int now, vector<bool>& created)
{
	int swap_count = 0;
	for (int i = now; i != dist; i+=step)
	{
		created[i] = !created[i];
		created[i+step] = !created[i+step];
		swap_count++;
#ifdef D
		cout << "created  ";
		print_bits(created);
#endif
	}
	return swap_count;
}

	int
do_swap(int dist, int now, vector<bool>& created)
{
	if (dist < now)
		return swap(-1, dist, now, created);
	else
		return swap(1 , dist, now, created);
}

	int
code_swap (const vector<bool>ori, vector<bool> created)
{
	int swap_count = 0;
	for (int i = 0; i < ori.size(); i++)
	{
		if (ori[i] != created[i])
		{
#ifdef D
			cout << "ori: i " << i << ", bool " << ori[i] <<endl;
			cout << "cre: i " << i << ", bool " << created[i] <<endl;
#endif
			swap_count += do_swap(i, i+search_closest_true(ori[i], i, ori, created), created);
		}
		if (code_distance(ori, created) == 0)
		{
#ifdef D
			cout << "ori ";
			print_bits(ori);
			cout << "cre ";
			print_bits(created);
#endif
			return swap_count;
		}
	}
#ifdef D
	cout << "ori ";
	print_bits(ori);
	cout << "cre ";
	print_bits(created);
#endif
	return swap_count;
}

	int
main (void)
{
	int n, m; cin >> n >> m;
	vector<bool> b;
	vector<int>	p;
	for (int i = 0; i < n; i++)
	{
		int in; cin >> in;
		b.push_back(in);
	}
	for (int i = 0; i < m; i++)
	{
		int in; cin >> in;
		p.push_back(in);
	}

	vector<vector<bool> > bits_list;
	bits_list = create_bits(p);
	int shorter = which_shorter(b, bits_list);
#ifdef D
	{
		cout << "original ";
		print_bits(b);
		cout << "selected ";
		print_bits(bits_list[shorter]);
		cout <<endl;
	}
#endif
	cout << code_swap(b, bits_list[shorter]) << endl;

	return 0;
}



Measured memory_bytes: 3112