用dp[i][j][k][l]表示1,2,3,4张牌各用i,j,k,l张时所走可以获得的最大分数,
然后对于每次将要到达的位置有
然后加上
#include<cstdio> #include<algorithm> using namespace std; const int N = 350, M = 41; int a[N], b[5] = { 0 }, maxx[M][M][M][M] = { 0 }; int main() { int n, m, temp; scanf("%d%d", &n, &m); for (int i = 0; i<n; i++) { scanf("%d", &a[i]); } for (int i = 0; i<m; i++) { scanf("%d", &temp); b[temp]++; } for (int b1 = 0; b1 <= b[1]; b1++) { for (int b2 = 0; b2 <= b[2]; b2++) { for (int b3 = 0; b3 <= b[3]; b3++) { for (int b4 = 0; b4 <= b[4]; b4++) { temp = 0; if (b1) temp = maxx[b1 - 1][b2][b3][b4]; if (b2) temp = max(temp, maxx[b1][b2 - 1][b3][b4]); if (b3) temp = max(temp, maxx[b1][b2][b3 - 1][b4]); if (b4) temp = max(temp, maxx[b1][b2][b3][b4 - 1]); maxx[b1][b2][b3][b4] = temp + a[b1 + 2 * b2 + 3 * b3 + 4 * b4]; } } } } printf("%d\n", maxx[b[1]][b[2]][b[3]][b[4]]); return 0; }