Partial Tree UVALive - 7190 (full backpack)

For a tree with up to a degree 2n-2
for every point we believe it to a degree
so for the rest of one weight corresponding to each
run can be completely determined when the size of the backpack n-2 is made the maximum value (initial negative infinity)
the size of the remaining equivalent of 1 to n-1, the capacity can be surely filled with n-2

#include<bits/stdc++.h>

#define inf 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
#define sd(x) scanf("%d",&(x))
#define rep(i,a,b) for(int i=a;i<=b;i++)

using namespace std;

int dp[2050],w[2050];

int main()
{
    int t;
    sd(t);
    while(t--)
    {
        int n;
        sd(n);
        rep(i,1,n) dp[i]=-inf;
        dp[0]=0;
        rep(i,1,n-1) sd(w[i]);
        int sum=n*w[1];
        rep(i,2,n) rep(j,i-1,n-2)
        {
            if(dp[j]<dp[j-i+1]+w[i]-w[1])
            {
                dp[j]=dp[j-i+1]+w[i]-w[1];
            }
        }
        cout<<sum+dp[n-2]<<"\n";
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/minun/p/11502594.html