Chef and easy problem Codechef Solution: 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 A1, A2, …, 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 Solution 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 Solution 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 Solution 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)
Disclaimer: The above Problem (Chef and easy problem) is generated by CodeChef but the solution is provided by Chase2learn.This tutorial is only for Educational and Learning purpose.