Defense Tower
Accepted : 92 | Submit : 176 | |
Time Limit : 3000 MS | Memory Limit : 65536 KB |
Defense Tower
In ICPCCamp, there are n cities and (n−1) (bidirectional) roads between cities. The i -th road is between the ai -th and bi -th cities. It is guaranteed that cities are connected.
In the i -th city, there is a defense tower with power pi . The tower protects all cities with a road directly connected to city i . However, the tower in city i does not protect city i itself.
Bobo would like to destroy all defense towers. When he tries to destroy the tower in city i , any not-destroyed tower protecting city i will deal damage whose value equals to its power to Bobo.
Find out the minimum total damage Bobo will receive if he chooses the order to destroy the towers optimally.
Input
The input contains at most 30 sets. For each set:
The first line contains an integer n ( 1≤n≤105 ).
The second line contains n integers p1,p2,…,pn ( 1≤pi≤104 ).
The i -th of the last (n−1) lines contains 2 integers ai,bi ( 1≤ai,bi≤n ).
Output
For each set, an integer denotes the minimum total damage.
Sample Input
3 1 2 3 1 2 2 3 3 1 100 1 1 2 2 3
Sample Output
3 2
题解:
特例:
5
10 10 10 10 8
1 5
2 5
3 5
4 5
5
5 5 5 5 10
1 5
2 5
3 5
4 5
两组特例中,都是先摧毁攻击力大的那座塔,故可以猜想使用贪心
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define MAXN 100005
int a[MAXN];
int main()
{
int n;
//freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
int u,v;
int ans=0;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<n;i++){
scanf("%d%d",&u,&v);
ans+=min(a[u],a[v]);
}
printf("%d\n",ans);
}
return 0;
}