#C5664. Maximum Sum of K Items Within Budget
Maximum Sum of K Items Within Budget
Maximum Sum of K Items Within Budget
You are given n items with associated prices. Your task is to select exactly K items such that the total price does not exceed a given budget B and the sum is maximized.
In other words, find a subset of exactly K items with prices \(p_1, p_2, \dots, p_K\) satisfying \[ p_1+p_2+\cdots+p_K \le B, \] such that the sum \(p_1+p_2+\cdots+p_K\) is as large as possible. If no valid combination exists, output 0.
Note: The input is read from standard input and the output should be printed to standard output.
inputFormat
The input consists of two lines:
- The first line contains three integers n, K, and B separated by spaces, where \(n\) is the number of items, \(K\) is the number of items to choose, and \(B\) is the budget.</p>
- The second line contains \(n\) integers separated by spaces representing the prices of the items.</p>
</ul>
outputFormat
Output a single integer—the maximum possible sum of the prices of exactly K selected items that does not exceed \(B\). If no valid combination exists, output 0.
## sample5 3 100 20 30 50 10 40
100