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';
}
}