Dsa
Greedy
Easy
Postcard Collection
You are running a postcard collection project and each postcard has a value. You want to select a few postcards such that their total value is maximized, given that you can only choose a limited number of postcards. However, you need to keep in mind the minimum value threshold that needs to be maintained for the selected postcards.
Write a function that takes as input a list of integers representing the values of the postcards and an integer representing the maximum number of postcards that can be chosen. Your output should be the maximum total value of the selected postcards while adhering to the limit.
Input:
- The first line contains an integer `max_postcards` (1 <= max_postcards <= 100).
- The second line contains `n` (1 <= n <= 100), the number of postcards.
- The third line contains `n` space-separated integers representing the postcard values.
Output:
- An integer representing the maximum total value of selected postcards.
Example:
Input:
3
5
10 20 30 40 50
Output:
120
In this case, selecting the three most valuable postcards gives the maximum total value.
Key concepts
greedysortingcumulative
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now