https://i-blog.csdnimg.cn/blog_migrate/ad6ec671aaebeafbf5d6d3e4366a52a0.png
Input
Output
Sample Input
3 1 2 3 1 2 2 3 3 1 100 1 1 2 2 3
Sample Output
3 2
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
struct p
{
int num,val;
friend bool operator<(p a,p b)
{
return a.val<b.val;
}
}df[100005];
vector<int>edge[100005];
int main()
{
int n,x,y,ans;
int vis[100005];
while(~scanf("%d",&n))
{
for(int i=0;i<=n;i++)
edge[i].clear();
priority_queue<p>q;
memset(vis,0,sizeof vis);
for(int i=1;i<=n;i++)
{
df[i].num=i;
scanf("%d",&df[i].val);
q.push(df[i]);
}
for(int i=1;i<=n-1;i++)
{
scanf("%d %d",&x,&y);
edge[x].push_back(y);
edge[y].push_back(x);
}
ans=0;
while(!q.empty())
{
int v=q.top().num;
q.pop();
for(int i=0;i<edge[v].size();i++)
{
if(vis[edge[v][i]]==0)
ans+=df[edge[v][i]].val;
}
vis[v]=1;
}
printf("%d\n",ans);
}
}