P1541 [NOIP 2010 提高组] 乌龟棋

点击查看代码
#include<bits/stdc++.h>
using namespace std;

const int N=360;
int scores[N];
int n,m;
int g[5];
int dp[41][41][41][41];

int main()
{
    ios::sync_with_stdio(0),cin.tie(0);

    cin>>n>>m;

    for(int i=1;i<=n;i++) cin>>scores[i];

    for(int i=1,x;i<=m;i++){
        cin>>x;
        g[x]++;
    }

    dp[0][0][0][0]=scores[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++){
                    if(a==0&&b==0&&c==0&&d==0) continue;
                    int pre=0;

                    if(a>0) pre=max(pre,dp[a-1][b][c][d]);
                    if(b>0) pre=max(pre,dp[a][b-1][c][d]);
                    if(c>0) pre=max(pre,dp[a][b][c-1][d]);
                    if(d>0) pre=max(pre,dp[a][b][c][d-1]);

                    int pos=1+a*1+b*2+c*3+d*4;

                    dp[a][b][c][d]=scores[pos]+pre;
                }
            }
        }
    }

    cout<<dp[g[1]][g[2]][g[3]][g[4]]<<"\n";

    return 0;
}

posted @ 2026-03-11 09:43  AnoSky  阅读(5)  评论(0)    收藏  举报