题意:给出N个点,让你构造出一棵树,让价值尽量大,价值的计算方式为 Σ f[ Ai ] 其中Ai是每个点的度。
范围:T,N<=2015
解法:共2*N-2个度,先在每个点放一个度,然后剩下N-2个度做一下背包,注意下预处理。
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<iostream>
#include<stdlib.h>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<bitset>
#pragma comment(linker, "/STACK:1024000000,1024000000")
template <class T>
bool scanff(T &ret){ //Faster Input
char c; int sgn; T bit=0.1;
if(c=getchar(),c==EOF) return 0;
while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();
sgn=(c=='-')?-1:1;
ret=(c=='-')?0:(c-'0');
while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
if(c==' '||c=='\n'){ ret*=sgn; return 1; }
while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;
ret*=sgn;
return 1;
}
#define inf 1073741823
#define llinf 4611686018427387903LL
#define PI acos(-1.0)
#define lth (th<<1)
#define rth (th<<1|1)
#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)
#define drep(i,a,b) for(int i=int(a);i>=int(b);i--)
#define gson(i,root) for(int i=ptx[root];~i;i=ed[i].next)
#define tdata int testnum;scanff(testnum);for(int cas=1;cas<=testnum;cas++)
#define mem(x,val) memset(x,val,sizeof(x))
#define mkp(a,b) make_pair(a,b)
#define findx(x) lower_bound(b+1,b+1+bn,x)-b
#define pb(x) push_back(x)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
int n;
int a[6666];
int dp[6666];
int main(){
tdata{
scanff(n);
rep(i,1,n-1)scanff(a[i]);
int ans=a[1]*n;
rep(i,2,n-1)a[i]-=a[1];
rep(i,1,n-2)dp[i]=-inf;
dp[0]=0;
rep(i,2,n-1){
rep(j,i-1,n-2){
dp[j]=max(dp[j],dp[j-i+1]+a[i]);
}
}
printf("%d\n",ans+dp[n-2]);
}
return 0;
}