Wrong answer?

problem code : MAXDIFF
#include <bits/stdc++.h>
using namespace std;

int main() {
// your code goes here
int t;
cin>>t;
while(t–)
{
long long n,k,sum=0,sum1=0;
cin>>n>>k;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
sum+=a[i];
}
sort(a,a+n);
for(int i=0;i<k;i++)
sum1+=a[i];
long long h=sum-(2*sum1);
if(h<0)
h=-h;
cout<<h<<endl;
}
return 0;
}

Consider test case:

1
5 3
8 2 10 4 5

Answer : 17.

How? Ha ha ha :laughing:.
Hack it yourself.

1 Like

You are storing first k minimum no. in one bag and then calculating the answer, however you can maximize the difference by keeping k maximum no. in the first bag and rest in other bag because the heavier one will be picked by chef (it is not mentioned the child will carry the bag with k items). The answer will be the maximum of these 2 cases.

1 Like