I am getting WA verdict in 3testcases, didn’t able to find the error, can anyone help?
#include <bits/stdc++.h>
#define lovishishere ios_base::sync_with_stdio(0);cin.tie(0);
#define ll long long
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define mod 1000000007
#define N 1000005
std::vector<ll> v[N];
std::vector<ll>cost;
using namespace std;
ll mus[N];
bool vis[N];
ll cnt;
void bfs(ll a){
if(!vis[a]){
cnt++;
vis[a]=true;
}
else{
return;
}
ll sec=mus[a];
queue<ll> q;
q.push(a);
while(!q.empty()){
ll a=q.front();
q.pop();
for(auto w: v[a]){
if(!vis[w]){
vis[w]=true;
q.push(w);
sec+=mus[w];
}
}
}
cost.pb(sec);
}
int main(){
lovishishere
ll t,n,m,k,a,b;
cin>>t;
while(t--){
memset(mus,0,sizeof(mus));
memset(vis,0,sizeof(vis));
cin>>n>>m>>k;
while(m--){
cin>>a>>b;
v[a].pb(b);
v[b].pb(a);
}
for(ll i=1;i<=n;i++){
cin>>mus[i];
}
for(ll i=1;i<=n;i++){
bfs(i);
}
if(cnt<k){
cout<<-1<<endl;
continue;
}
sort(cost.begin(), cost.end());
ll ans=0;
for(ll i=0;i<k/2;i++){
ans+=cost[i];
}
reverse(cost.begin(), cost.end());
for(ll i=0;i<k-k/2;i++){
ans+=cost[i];
}
cout<<ans<<endl;
cnt=0;
cost.clear();
for(ll i=0;i<n;i++){
v[i].clear();
}
}
//code here
return 0;
}