Chef and easy problem Codechef Solution

Hello Programmers In this post, you will know how to solve the Chef and easy problem Codechef Solution.

Chef and easy problem Codechef Solution
Chef and easy problem Codechef Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Codechef by yourself. If you find any difficulty after trying several times, then you can look for solutions.

Ezoicreport this adProblem

Chef and Roma are playing a game. Rules of the game are quite simple. Initially there are N piles of stones on the table. In each turn, a player can choose one pile and remove it from the table. Each player want to maximize the total number of stones removed by him. Chef takes the first turn.

Please tell Chef the maximum number of stones he can remove assuming that both players play optimally.

Input

The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.

The first line of each test case contains a single integer N denoting the number of piles.

The second line contains N space separated integers A1A2, …, AN denoting the number of stones in each pile.

Output

For each test case, output a single line containg the maximum number of stones that Chef can remove.

Constraints

  • 1 ≤ Ai ≤ 109
  • Subtask 1 (35 points): T = 10, 1 ≤ N ≤ 1000
  • Subtask 2 (65 points): T = 10, 1 ≤ N ≤ 105

Example

Input:
2
3
1 2 3
3
1 2 1
Output:
4
3

Chef and easy problem CodeChef Solutions in JAVA

import java.util.*;
class Chef_and_easy_problem {
    public static void main (String[]args) {
        Scanner input = new Scanner(System.in);
        int t = input.nextInt();
        while(t-- > 0) {
            ArrayList<Long> list = new ArrayList<Long>();
            int n = input.nextInt();
            int temp = n;
            while(n-- > 0) {
                list.add(input.nextLong());
            }
            Collections.sort(list, Collections.reverseOrder());
            long sum = 0;
            for (int i = 0 ; i < temp ; i+=2) {
                 sum += list.get(i);
            }
            System.out.println(sum);
        }
        input.close();
    }
}

Chef and easy problem CodeChef Solutions in CPP

#include <bits/stdc++.h>
using namespace std;
int main () {
	int b;
	cin >> b;
	for (int j = 1; j <= b; j++) {
		int n;
	    cin >> n;
	    int a[100001];
	    for (int i = 1; i <= n; i++) {
		    cin >> a[i];
		    }
		long long sum = 0;
	    sort(a + 1, a + n + 1, greater<long long>());
	    for (int i = 1; i <= n; i += 2) {
	    		sum += a[i];
	    		}
	    	    cout << sum << endl;
	    	    }
	    return 0;
	    }

Chef and easy problem CodeChef Solutions in Python

t=int(input())
for i in range(t):
    n=int(input())
    lis=list(map(int,input().split()))
    lis.sort(reverse=True)
    j=0
    sumi=0
    while j<n:
        if j%2==0:
            sumi+=lis[j]
        j+=1
    print(sumi)    
Ezoicreport this ad

Disclaimer: The above Problem (Chef and easy problem) is generated by CodeChef but the solution is provided by BrokenProgrammers. This tutorial is only for Educational and Learning purpose.

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Next: Maximum Candies Codechef Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad