#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;
}
'백준 알고리즘 > 다이나믹 프로그래밍' 카테고리의 다른 글
백준 2352번 C++ (0) | 2019.08.23 |
---|---|
백준 5582번 C++ (0) | 2019.08.22 |
백준 11052번 C++ (0) | 2019.08.21 |
백준 11054번 C++ (0) | 2019.08.06 |
백준 1038번 C++ (0) | 2019.08.06 |