Problem:-
You are given an array of integers. You want to choose some integers from the array subject to the condition that the number of distinct integers chosen should not exceed . Your task is to maximize the sum of chosen numbers.
You are given test cases.
Input format
- The first line contains a single integer denoting the number of test cases.
- The first line of each test case contains two space-separated integers and denoting the length of the array and the maximum number of distinct integers you can choose.
- The second line of each test case contains space-separated integers denoting the integer array .
Output format
For each test case(in a separate line), print the maximum sum you can obtain by choosing some elements such that the number of distinct integers chosen is at most . If you cannot choose any element, output .
Constraints
In the first test case, we have , . Since we can choose atmost 1 distinct integer, we choose . The sum is also and we output it.
In the second test case, we have , . We need to choose atmost 2 distinct integers, we choose . Note that the condition is choosing atmost distinct integers. So we can choose repeated number as many times as we want. The sum is and we output it.
Code:-
In this solution first three lines of the main function is only for the decreasing the time of execution of the program..
This is your choice that you want to use this or not but in some cases the code may take more time in execution and that time we need it .
Recommended Post:-
- Hackerearth Problems:-
- Very Cool numbers | Hacker earth solution
- Vowel Recognition | Hackerearth practice problem solution
- Birthday party | Hacker earth solution
- Most frequent | hacker earth problem solution
- program to find symetric difference of two sets
- cost of balloons | Hacker earth problem solution
- Chacha o chacha | hacker earth problem solution
- jadu and dna | hacker earth solution
- Bricks game | hacker earth problem
- Anti-Palindrome strings | hacker earth solution
- connected components in the graph | hacker earth data structure
- odd one out || hacker earth problem solution
- Minimum addition | Hackerearth Practice problem
- The magical mountain | Hackerearth Practice problem
- The first overtake | Hackerearth Practice problem
Hackerrank Problems:-Data structure:-- Playing With Characters | Hackerrank practice problem solution
- Sum and Difference of Two Numbers | hackerrank practice problem solution
- Functions in C | hackerrank practice problem solution
- Pointers in C | hackerrank practice problem solution
- Conditional Statements in C | Hackerrank practice problem solution
- For Loop in C | hackerrank practice problem solution
- Program to find cycle in the graph
- Implementation of singly link list
- Implementation of queue by using link list
- Algorithm of quick sort
- stack by using link list
- program to find preorder post order and inorder of the binary search tree
- Minimum weight of spanning tree
- Preorder, inorder and post order traversal of the tree
0 Comments