#K75602. Maximum Subset Sum Without Exceeding Target
Maximum Subset Sum Without Exceeding Target
Maximum Subset Sum Without Exceeding Target
You are given a list of n integers and an integer target T. Your task is to find a subset from the list whose sum is as large as possible without exceeding T. In other words, you need to choose a subset S such that
$$\max \left\{ \sum_{i\in S} a_i : \sum_{i\in S} a_i \le T \right\}$$
If there are multiple subsets with the same sum, choose the one with the fewest elements. If there is still a tie, choose the lexicographically smallest subset (when each subset is sorted in ascending order). If no non-empty subset satisfies the condition, output an empty result.
Note: The subset should be output in ascending order.
inputFormat
The input is read from standard input.
- The first line contains two integers
n
andT
, wheren
is the number of elements andT
is the target sum. - The second line contains
n
integers separated by spaces representing the list of numbers.
outputFormat
Output the chosen subset in a single line as space-separated integers in ascending order. If no valid non-empty subset exists, output an empty line.
## sample4 10
1 2 3 4
1 2 3 4
</p>