xtu oj 1252Defense Tower

Defense Tower

Accepted : 98 Submit : 191
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、

贪心

#include 
#include 
#include 
#include 
using namespace std;

int cmp(int a,int b)
{
    return a>b;
}

int a[100010];
int main()
{
    int n;

    //int num[100010];
    //int map[100][100];

    while(~scanf("%d",&n))
    {
        int sum=0;
        //memset(map,0,sizeof(map));
        //int k=1;
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
            //num[k]=a[i];//cout<
            //k++;
        }
        for(int i=0; i1; i++)
        {
            int x,y;
            scanf("%d %d",&x,&y);
            //map[x][y]=map[y][x]=1;
            sum+=min(a[x],a[y]);
            //sum[x]+=num[x];
        }
        cout<//sort(a,a+n,cmp);

    }

    return 0;
}

你可能感兴趣的:(湘潭赛)