PROBLEM LINK:Author: Vamsi Kavala DIFFICULTY:CAKEWALK PREREQUISITES:Greedy algorithm, Sorting algorithms PROBLEM:You are given an array W[1], W[2], ..., W[N]. Choose K numbers among them such that the absolute difference between the sum of chosen numbers and the sum of remaining numbers is as large as possible. QUICK EXPLANATION:There are two possibilities to try  K largest numbers and K smallest numbers (see below why). So the solution could be like this:
EXPLANATION:Consider the following subproblem: choose K numbers with largest possible sum. Then the solution obviously is K largest numbers. So that here greedy algorithm works  at each step we choose the largest possible number until we get all K numbers. In our problem we should divide the set of N numbers into two groups of K and N − K numbers respectively. Consider two cases:
This reasoning justify the solution in the QUICK EXPLANATION. Regarding implementation details. Since N and T are small, then every simple O(N^{2}) sorting algorithm from here will work. Other steps of the solution seems trivial to implement. ALTERNATIVE SOLUTION:Let's go further and ask our self which of two above cases actually gives the answer. After short thinking it became clear that larger difference would be when more numbers are included to the group of largest numbers. Hence we could set M = max(K, N − K), find the sum of M largest numbers (let it be S1) and then the answer is S1 − (S − S1), where S is the sum of all numbers. AUTHOR'S AND TESTER'S SOLUTIONS:Author's solution will be provided soon. RELATED PROBLEMS:Will be provided soon. All readers are welcomed to provide related problems in comments.
This question is marked "community wiki".
asked 15 Apr '13, 15:00

https://www.codechef.com/viewsolution/9766474 why is this wrong answered 29 Mar '16, 22:46
Hey karbish98 here is the solution to your question https://discuss.codechef.com/questions/80572/maxdiffquestionproblem More over I have upvoted your question so that you can now ask question on your own and upvote and participate in codechef discuss and community rather that posting questions in answer column of editorials.
(31 Mar '16, 11:30)

for which case my this submission got WA ? submission: http://www.codechef.com/viewsolution/6355868 answered 27 Feb '15, 10:43

why does it give WA? https://www.codechef.com/viewsolution/11273738 answered 28 Aug '16, 21:57

@lucifer007 you just forget that if k>(nk) then k will be replaced with (nk). answered 28 Aug '16, 23:31

For which case is the solution giving wa https://www.codechef.com/viewsolution/11583814 pls someone explain ? answered 21 Sep '16, 16:43

include <cstdio>using namespace std; include <algorithm>int main() { int t; scanf("%d",&t); while(t) { int n,k; scanf("%d%d",&n,&k); int arr[n]; for(int i=0 ; i<n ;="" i++)="" scanf("%d",&arr[i]);="" sort(arr,arr+n);="" int="" s1,s2;="" s1="s2=0;" for(int="" i="0;i<n;i++)" {="" if((i+1)<="k)" s1+="arr[i];" else="" s2+="arr[i];" }="" int="" ans;="" if(s1="">s2) ans=s1s2; else ans=s2s1; printf("%d\n",ans); } return 0; } answered 05 Jan, 18:47

https://www.codechef.com/viewsolution/13185103 please tell me what is wrong? answered 30 Mar, 14:41

testcase=int(input()) while(testcase>0): child=0 chef=0 lst=[int(i) for i in input().split()] weights=[int(i) for i in input().split()] weights.sort() for i in range(0,len(weights)): if(i<lst[1]): child+=weights[i] else: chef+=weights[i] print(chefchild) testcase=1 Can anybody will tell me why am i getting wrong answer? answered 17 May, 19:04

Can someone tell me why am I getting the wrong answer for this code while(t>0) { int n=sc.nextInt(); int k=sc.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); Arrays.sort(a);
answered 29 Jun, 16:27

https://www.codechef.com/viewsolution/15576532 why is this getting WA answered 01 Oct, 18:50

https://www.codechef.com/viewsolution/15820847 Why am I getting WA? answered 14 Oct, 10:13
