题目:
https://www.vijos.org/p/1048
dfs+套路剪枝;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct hh {int num,cost;}a[1001];
struct sh {int from,to;}ma[10001];
int vis[10001];
bool cmp(hh a,hh b) {return a.cost > b.cost;}
int max(int a,int b) {return a > b ? a : b;}
int tot,ans,n;
int sum[10001],pos[10001],nxt[10001],fst[10001],h[10001];
void build(int f,int t)
{
ma[++tot]=(sh){f,t};
nxt[tot]=fst[f];
fst[f]=tot;
return;
}
void dfs(int x,int T)
{
if(T + sum[n] - sum[x-1] < ans) return;
ans=max(ans,T);
if(x>n) return;
if(!vis[x])
{
for(int i=fst[x];i;i=nxt[i]) vis[ma[i].to]++;
dfs(x+1,T+a[x].cost);
for(int i=fst[x];i;i=nxt[i]) vis[ma[i].to]--;
}
dfs(x+1,T);
return;
}
void solve()
{
int x,y;
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i].cost),a[i].num=i;
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++) pos[a[i].num]=i;
for(int i=1;i<=n;i++) sum[i]=sum[i-1]+a[i].cost;
while(~scanf("%d%d",&x,&y))
{
build(pos[x],pos[y]);
build(pos[y],pos[x]);
}
dfs(1,0);
cout<<ans<<'\n';
return;
}
int main()
{
solve();
return 0;
}