dp[a][b][c][d]表示走了a+b2+c3+d*4步的当前的最大值,状态转移方程就出来了。
点击查看代码
#include <iostream>
#include <stack>
#include <cmath>
#include <algorithm>
#include <set>
#include <vector>
#include <climits>
#include <string.h>
#include <map>
#include <queue>
#include <list>
#include <cmath>
#include <iomanip>
#define int long long
#define ios ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define lc u<<1
#define rc u<<1|1
#define gcd __gcd
#define double long double
#define endl "\n"
#define INF LLONG_MAX
#define mod 1000000007
#define N 40
const double PI = 3.14159265358979323846;
using namespace std;
int n, m, num[355], g[N],dp[N][N][N][N];
signed main()
{
ios;
cin >> n >> m;
for (int i = 1; i <= n; i++)cin >> num[i];
for (int j = 1; j <= m; j++)
{
int x; cin >> x;
g[x]++;
}
dp[0][0][0][0] = num[1];
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 != 0)dp[a][b][c][d] = max(dp[a][b][c][d], dp[a - 1][b][c][d] + num[r]);
if (b != 0)dp[a][b][c][d] = max(dp[a][b][c][d], dp[a][b - 1][c][d] + num[r]);
if (c != 0)dp[a][b][c][d] = max(dp[a][b][c][d], dp[a][b][c - 1][d] + num[r]);
if (d != 0)dp[a][b][c][d] = max(dp[a][b][c][d], dp[a][b][c][d - 1] + num[r]);
}
}
}
}
cout << dp[g[1]][g[2]][g[3]][g[4]] << endl;
return 0;
}