题解
2023-06-04 13:29:25
发布于:上海
3阅读
0回复
0点赞
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int f[41][41][41][41], num[351], g[5], n, m, x;
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> num[i];
f[0][0][0][0] = num[1];
for (int i = 1; i <= m; i++)
{
cin >> x;
g[x]++;
}
for (int a = 0; a <= g[1]; a++)
for (int b = 0; b <= g[2]; b++)
for (int c = 0; c <= g[3]; c++)
for (int d = 0; d <= g[4]; d++)
{
int r = 1 + a + b * 2 + c * 3 + d * 4;
if (a)
f[a][b][c][d] = max(f[a][b][c][d], num[r] + f[a - 1][b][c][d]);
if (b)
f[a][b][c][d] = max(f[a][b][c][d], num[r] + f[a][b - 1][c][d]);
if (c)
f[a][b][c][d] = max(f[a][b][c][d], num[r] + f[a][b][c - 1][d]);
if (d)
f[a][b][c][d] = max(f[a][b][c][d], num[r] + f[a][b][c][d - 1]);
}
cout << f[g[1]][g[2]][g[3]][g[4]];
return 0;
}
这里空空如也
有帮助,赞一个