paysmile 2019. 8. 22. 01:40

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
const int MAX = 1001;
int t, w;
int fruit[MAX];
int cache[MAX][31][3];

int maxfruit(int i,int move, int tree) {
	if (i == t)
		return 0;

	int &answer = cache[i][move][tree];
	if (answer != -1)
		return answer;

	if (tree == fruit[i]) {
		if (move < w)
			answer = max(maxfruit(i + 1, move, tree) + 1, maxfruit(i + 1, move + 1, 3 - tree));
		else
			answer = maxfruit(i + 1, move, tree) + 1;
	}
	else {
		if (move < w)
			answer = max(maxfruit(i + 1, move, tree), maxfruit(i + 1, move + 1, 3 - tree) + 1);
		else
			answer = maxfruit(i + 1, move, tree);
	}
	return answer;
}

int main(void) {
	cin >> t >> w;

	for (int i = 0; i < t; i++)
		cin >> fruit[i];
	memset(cache, -1, sizeof(cache));
	cout << maxfruit(0,0,1) << endl;
	return 0;
}