TEAMSEL c++

The link to the problem is…

my approach is maybe too simple …first i have sorted the array using the simplest bubble sort for now and then i have divided them into the two teams teama and teamb 2 at a time where everytime the player with the higher skill goes to the team with the lower skilled team at that point of time.This works for the first 3 cases but i guess for the 4th case it doesnt because straightaway 100 and 87 gets into opposite teams whereas for the solution it has to be “100 87 41 1” in one of the teams to get 229 So is my approach totally incorrect??..Plz suggest a new approach if possible…My code is…

int main()
{

int x;

cin>>x;

for(int i=0;i<x;i++)
{       
        int y,teama=0,teamb=0,flagged=0;
        cin>>y;
        int a[y];
        for(int j=0;j<y;j++)
        {
                cin>>a[j];
        }
        for(int c=y; c>0; c--)
        {
                for(int k=0; k<c-1; k++)
	            {
		                if(a[k]>a[k+1])
		                {
                                               flagged=1;
                                               int temp = a[k+1];
                                               a[k+1] = a[k];
			                                   a[k] = temp;
                        }
                 }
                 if(flagged==0)
                               break;
                               
        }
        teama=a[y-1];
        teamb=a[y-2];
        for(int l=y-3;l>=0;l--)
        {
                if (teama>teamb)
                {
                   teamb=teamb + a[l];
                   if(l!=0)
                   teama=teama + a[--l];   
                }                     
                else
                {
                   teama=teama + a[l];
                   if(l!=0)
                   teamb=teamb + a[--l];    
                }                                                       
        }
        cout<<teamb<<" "<<teama<<endl;
        if(i!=(x-1))
                   cout<<endl;


}       

}

Please provide a link to your problem so that people can easily skim though it. Your question should be in the question itself, not as comment.

Well i have updated my question…so any sggestions???

Are you sure you want to solve hard problems first? This is knapsack problem, isn’t it?

@betlista
Maybe i am trying something hard initially but now that i have started atleast some idea as to how one should go about this question would be welcome…nd yeah i’ll try smthing easier the next time…:stuck_out_tongue: