why the hell WA for galactik,,,,checkd nearly all tst cases

#include
#include
#include

using namespace std;
int dfs(int a);int mini=10005;int p=0;
list M[1000004];
int visited[100005]={0};

int cost[100005]={0};
int main()
{
int a,b,c,t,n,m,i,j,k,l;
scanf("%d %d",&n,&m);

for(i=1;i<=m;i++)
{
    scanf("%d %d",&a,&b);
    M[a].push_back(b);
    M[b].push_back(a);
}int ans[n];int s=1;
for(i=1;i<=n;i++)
{scanf("%d",&cost[i]);} k=1;
for(i=1;i<=n;i++)
{
    if(visited[i]==0)
    {
        dfs(i);
        if(p==0)
        {s=0;goto d;}
       
        ans[k]=mini;mini=10005;p=0;s=1;k++;
    }
}d:mini=10005;int answ=0;int y=0;
for(i=1;i<k;i++)
{
if(mini>ans[i])
{mini=ans[i];y=i;}
}
for(i=1;i<k;i++)
{if((i!=y)&&(k>2)){answ=answ+ans[i]+mini;}
else if(k<3)
{answ=0;}
 }    
if(s==1)
printf("%d",answ);
else if(s==0)
printf("%d",-1); 

return 0;
}

int dfs(int a)
{
visited[a]=1;
if(cost[a]>=0)
p=1;
if((mini>cost[a])&&(cost[a]>=0))
mini=cost[a];
list::iterator it;
for(it=M[a].begin();it!=M[a].end();++it)
{if(visited[*it]==0)dfs(*it);}
}

pls dont ask duplicate ques…!!!

@wonder. You can always add answers or comments to your original question. Someone will definitely help you out.

kk watng 4 the ans…:frowning:

see the tc on that link…new tc i have added!!!

//