#include <bits/stdc++.h>
#define MAXN 350
typedef unsigned int uint;
using namespace std;
struct T {
uint use[5];
T(uint cnt[]) {
memcpy(&use[1], &cnt[1], sizeof(uint) * 4);
}
bool operator<(const T &x) const {
for(uint i = 1; i <= 3; i++) {
if(use[i] != x.use[i]) {
return use[i] < x.use[i];
}
}
return use[4] < x.use[4];
}
};
uint n, m;
uint cnt[5];
uint a[MAXN + 1];
map<T, uint> dp[MAXN + 1];
uint f(uint pos, T now) {
auto find = dp[pos].find(now);
if(find != dp[pos].end()) {
return find->second;
}
uint tmp = 0, bound = min((uint)4, pos - 1);
for(uint i = 1; i <= bound; i++) {
if(now.use[i] == 0) {
continue;
}
now.use[i]--;
tmp = max(tmp, f(pos - i, now));
now.use[i]++;
}
dp[pos][now] = tmp + a[pos];
}
int main() {
cin >> n >> m;
for(uint i = 1; i <= n; i++) {
cin >> a[i];
}
for(uint i = 1; i <= m; i++) {
uint tmp;
cin >> tmp;
cnt[tmp]++;
}
uint init[5] = {0, 0, 0, 0, 0};
dp[1][T(init)] = a[1];
cout << f(n, cnt) << endl;
return 0;
}
写了个记忆化搜索,本地测试对的,提交就错了。 下载下来第一个测试数据:
13 4
32 37 75 16 64 33 79 97 22 2 99 100 41
4 2 2 4
求助。