Help me in solving MAXDIFF problem

My issue

My code

#include <iostream>
#include<algorithm>
using namespace std;

int main() {
	// your code goes here
	int t,n,k;
	cin>>t;
	while(t--){
	    cin>>n>>k;
	  long  long int a[n];
	    for(int i=0;i<n;i++){
	        cin>>a[i];
	    }
	   // long int b[k],d[n-k];
	  long  int sum1=0,sum2=0,j=k;
	    sort(a,a+n);
	    for(int i=0;i<k;i++){
	      
	        sum1+=a[i];
	       
	    }
	       for(;j<n;j++){
	      
	      
	        sum2+=a[j];
	    }
	    
	    cout<<sum2-sum1<<endl;
	}
	return 0;
}

Problem Link: MAXDIFF Problem - CodeChef

@deepakjdh31
Suppose n=7 and k=6
and array is 1 2 3 4 5 6 7
according to your logic sum1=21 and sum2=7;
thus answer is 7-21= -14;
but if i allow father to carry k items and rest to son then
sum2= 2+3+4+5+6+7=27;
and sum1=1;
thus answer would be 26 which is the correct answer for this case .
Its not given that only son has to pick k items ,any one of them has to pick exactly k items.

1 Like