CF 437C The Child and Toy

http://codeforces.com/problemset/problem/437/C
time limit per test
1 second memory limit per test
256 megabytes
input: standard input
output: standard output

Description

On Children’s Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can’t wait to destroy the toy.

The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let’s define an energy value of part i as vi. The child spend vf1 + vf2 + … + vfk energy for removing part i where f1, f2, …, fk are the parts that are directly connected to the i-th and haven’t been removed.

Help the child to find out, what is the minimum total energy he should spend to remove all n parts.

Input

The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v1, v2, …, vn (0 ≤ vi ≤ 105). Then followed m lines, each line contains two integers xi and yi, representing a rope from part xi to part yi (1 ≤ xi, yi ≤ n; xi ≠ yi).
Consider all the parts are numbered from 1 to n.

Output

Output the minimum total energy the child should spend to remove all n parts of the toy.

Examples

Input

4 3
10 20 30 40
1 4
1 2
2 3

Output

40

Input

4 4
100 100 100 100
1 2
2 3
2 4
3 4

Output

400

Input

7 10
40 10 20 10 20 80 40
1 5
4 7
4 5
5 2
5 7
6 4
1 6
1 3
4 3
1 4

Output

160

Solution

本题愿意是想删除每一个点,删除的时候有代价,这个代价是与这个点相连的所有点的点权和(必须是还没有被删除掉的点)。乍一看这道题,就按照题目意思去做了,结果发现如果删点的话,我们还还把相应的链接的点的权值减掉当前这个被删除点的权值。然后发现每次都要找最小的点,会发现顺序很乱,虽然我们可以O(log N)用堆把最小的点找出,但还要去对于他相连的点处理,且堆排后顺序会乱,我们就不能这样做。
正解是在输入的时候,我们把输入的边当作要删除的边,每次取与他相连的两个点的最小点权的那个点作为累加的答案,这样就避免了很多繁琐的操作,变得可行。

程序代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int n,m,sum,a[2000];
inline int read(){
    int x=0,w=1;char ch;
    while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
    if(ch=='-') w=-1,ch=getchar();
    while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch-48),ch=getchar();
    return x*w;
}
int main(){
    while(~scanf("%d%d",&n,&m)){
        sum=0;
        for(register int i=1;i<=n;++i) a[i]=read();
        for(register int i=1;i<=m;++i) {
            int u=read(),v=read();
            sum+=min(a[u],a[v]);
        }
        cout<<sum<<endl;
    }
}

解后反思

对于有些图论的题(虽然这题显然是贪心)当我们不好处理点时,可以想想处理边时;反之依然。有时后我们也可以采取时光倒流或是离线或是在线等多种操作。

猜你喜欢

转载自blog.csdn.net/tangzhide123yy/article/details/81169453
今日推荐