K Friends
Practice
3.3 (20 votes)
Algorithms
Easy
Greedy algorithms
Sorting
Approved
Hiring
Problem
54% Success 3800 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Monk has N friends. They are invited to his birthday party. Each friend has a satisfying factor which is equal to the number of gifts which they are expecting. Monk wants to satisfy at-least K friends but he is unaware of their satisfying factors. So Monk starts distribution of gifts. As soon as a friend is satisfied he won't take more gifts.

Monk will follow a distribution strategy so as to minimize the number of gifts needed to satisfy atleast K of his friends. Find the minimum number of gifts which Monk should carry with himself in the worst case.

Input Format

First line contains an integer T (the number of testcases).

Then first line of each test case contains an integer N (the number of friends).

Then N space separated integers follow which are the satisfying factor(\(S[i]\)).

Last line of each test case consists of an integer K.

Output Format

For each case print in a new line the minimum number of gifts that Monk should carry.

Constraints

\(1 \le T \le 10 \)

\(1 \le N,S[i] \le 10^5 \)

\(1 \le K \le N \)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
63 votes
Tags:
AlgorithmsBasics of Greedy AlgorithmsC++Greedy Algorithms
Points:20
9 votes
Tags:
Greedy AlgorithmsAlgorithmsBasics of Greedy AlgorithmsGreedy algorithm
Points:20
90 votes
Tags:
AlgorithmsGreedy Algorithms