Maximize the sum
Practice
2.8 (83 votes)
Sorting
Arrays
Implementation
Data structures
Set
1 D
Problem
88% Success 38240 Attempts 20 Points 1.5s Time Limit 256MB Memory 1024 KB Max Code
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 $$T$$ denoting the number of test cases.
- The first line of each test case contains two space-separated integers $$N$$ and $$K$$ denoting the length of the array and the maximum number of distinct integers you can choose.
- The second line of each test case contains $$N$$ space-separated integers denoting the integer array $$A$$.
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 \\
1 \le K \le N \le 5 \times 10^5 \\
-10^9 \le A_i \le 10^9 \\
\text{Sum of N over all test cases does not exceed } 2 \times 10^6 $$
Submissions
Please login to view your submissions
Similar Problems
Points:20
56 votes
Tags:
Real worldData Structures1-DArrays
Points:20
131 votes
Tags:
ArraysData StructuresEasyOne-dimensional
Points:20
30 votes
Tags:
ArraysData StructuresEasyOne-dimensional
Editorial