B. Party(思维)

A club plans to hold a party and will invite some of its nn members. The nn members are identified by the numbers 1,2,…,n1,2,…,n. If member ii is not invited, the party will gain an unhappiness value of aiai.

There are mm pairs of friends among the nn members. As per tradition, if both people from a friend pair are invited, they will share a cake at the party. The total number of cakes eaten will be equal to the number of pairs of friends such that both members have been invited.

However, the club's oven can only cook two cakes at a time. So, the club demands that the total number of cakes eaten is an even number.

What is the minimum possible total unhappiness value of the party, respecting the constraint that the total number of cakes eaten is even?

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041≤t≤104). The description of the test cases follows.

The first line of each test case contains two integers nn and mm (1≤n≤1051≤n≤105, 0≤m≤min(105,n(n−1)2)0≤m≤min(105,n(n−1)2)) — the number of club members and pairs of friends.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1040≤ai≤104) — the unhappiness value array.

Each of the next mm lines contains two integers xx and yy (1≤x,y≤n1≤x,y≤n, x≠yx≠y) indicating that xx and yy are friends. Each unordered pair (x,y)(x,y) appears at most once in each test case.

It is guaranteed that both the sum of nn and the sum of mm over all test cases do not exceed 105105.

Output

For each test case, print a line containing a single integer – the minimum possible unhappiness value of a valid party.

Example

input

Copy

4
1 0
1
3 1
2 1 3
1 3
5 5
1 2 3 4 5
1 2
1 3
1 4
1 5
2 3
5 5
1 1 1 1 1
1 2
2 3
3 4
4 5
5 1

output

Copy

0
2
3
2

Note

In the first test case, all members can be invited. So the unhappiness value is 00.

In the second test case, the following options are possible:

  • invite 11 and 22 (00 cakes eaten, unhappiness value equal to 33);
  • invite 22 and 33 (00 cakes eaten, unhappiness value equal to 22);
  • invite only 11 (00 cakes eaten, unhappiness value equal to 44);
  • invite only 22 (00 cakes eaten, unhappiness value equal to 55);
  • invite only 33 (00 cakes eaten, unhappiness value equal to 33);
  • invite nobody (00 cakes eaten, unhappiness value equal to 66).

The minimum unhappiness value is achieved by inviting 22 and 33.

In the third test case, inviting members 3,4,53,4,5 generates a valid party with the minimum possible unhappiness value.

思路:

1,m为偶数时,全选,没有不高兴的

2,m为奇数时,一种情况,出现为奇数的单个的可以不要,(相当于要偶数对),另一种情况是,出现为偶数的一对整体不要,(相当于要偶数对),取最小

3,偶数对是这个题的突破口

代码:

int a[maxj],x[maxj],y[maxj];
void solve(){
    int n,m;cin>>n>>m;
    for(int i=1;i<=n;++i)cin>>a[i];
    map<int,int>b;
    for(int i=1;i<=m;++i){
        cin>>x[i]>>y[i];
        b[x[i]]++,b[y[i]]++;
    }
    if(m%2==0){
        cout<<0<<'\n';
    }else{
        int ans=inf;
        for(int i=1;i<=n;++i){
            if(b[i]&1)
            ans=min(a[i],ans);
        }
        for(int i=1;i<=m;++i){
            if(b[x[i]]%2==0&&b[y[i]]%2==0)ans=min(ans,a[x[i]]+a[y[i]]);
        }
        cout<<ans<<'\n';
    }
}

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值