Maximum Weight question

i have applied all the test cases I can think of but i am still getting wrong answer can anyonr look into my code.
Question-MAXDIFF Problem - CodeChef

here is my code

   #include <bits/stdc++.h>
using namespace std;

int main()
{
	long long int t;
	cin>>t;
	int n,k;
	while(t--)
	{
	cin>>n>>k;
	long long int weight[n+1];
    long long int sum1=0,sum2=0,sum=0,diff1,diff2;
	for(int i=0;i<n;i++)
	{
	cin>>weight[i];
	sum+=weight[i];
	}
	sort(weight,weight+n);
	for(int i=0;i<k;i++)
	sum1+=weight[i];
	for(int i=k;i<n;i++)
	sum2+=weight[i];
	diff1=abs(2*sum1-sum);
	diff2=abs(2*sum2-sum);
	if(diff1>diff2)
	cout<<diff1<<"\n";
	else
	cout<<diff2<<"\n";
	}
	return 0;
}

the line for(int i=k,i<n;i++) should be changed to for(i=n-k;i<n;i++)… correct it

Your code fails on this test case-

Input
1
5 3
100 100 5 2 10
Your Output
183
Correct Output
203

We can put the 100,100 and 10 in the bag giving weight of 210. Son takes other weight of 7. You will have to iterate for k elements from end of array to, because of possibility that putting larger weights can give bigger difference.

Hey there are two cases possible:

Case 1:

His son takes first k objects and you are doing correct for calculating this.

Case 2:

His son takes last k objects and here you are not calculating that correctly .sum2 should be sum of last k elements but you are calculating sum from kth element .sum2=(weight[n-k]+weight[n-k+1]…weight[n-1].

Here is accepted code:

https://www.codechef.com/viewsolution/14411533