Maximize the sum  Hackerearth practice problem solution
Problem:
You are given an array
$A$of
$N$integers. You want to choose some integers from the array subject to the condition that the number of distinct integers chosen should not exceed
$K$. Your task is to maximize the sum of chosen numbers.
You are given
$T$test cases.
Input format
 The first line contains a single integer
 The first line of each test case contains two spaceseparated integers
 The second line of each test case contains
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
$K$. If you cannot choose any element, output
$0$.
Constraints
$1\le T\le 1000\phantom{\rule{0ex}{0ex}}1\le K\le N\le 5\times {10}^{5}\phantom{\rule{0ex}{0ex}}{10}^{9}\le {A}_{i}\le {10}^{9}\phantom{\rule{0ex}{0ex}}\text{Sum of N over all test cases does not exceed}2\times {10}^{6}$
In the first test case, we have
$N=4,K=1$,
$A=[3,1,2,5]$. Since we can choose atmost 1 distinct integer, we choose
$5$. The sum is also
$5$and we output it.
In the second test case, we have
$N=4,K=2$,
$A=[2,1,2,5]$. We need to choose atmost 2 distinct integers, we choose
$2,2,5$. Note that the condition is choosing atmost
$K$distinct integers. So we can choose repeated number as many times as we want. The sum is
$2+2+5=9$and we output it.
$10$
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
 AntiPalindrome 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: 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
Data structure:
 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
Key points:
MCQs: