#K85817. Maximum Distinct Messages
Maximum Distinct Messages
Maximum Distinct Messages
Problem Description
You are given a network with a maximum allowed total length, L, and a list of N distinct messages, each with a specified length. Your task is to determine the maximum number of messages that can be transmitted such that the sum of their lengths does not exceed L.
To maximize the number of messages, you should choose the messages with the smallest lengths first. In other words, if you sort the messages by length, you need to select the longest possible prefix of messages where the sum of lengths satisfies the inequality \[ \sum_{i=1}^{k} a_i \leq L \] for the maximum possible k.
Read the input from stdin
and output the result to stdout
.
inputFormat
The input begins with an integer T (\(1 \leq T \leq 100\)), representing the number of test cases. Each test case consists of three lines:
- The first line contains a single integer N (the number of messages).
- The second line contains a single integer L (the maximum allowed total length).
- The third line contains N space-separated integers denoting the lengths of the messages.
All input is read from stdin
.
outputFormat
For each test case, output a single integer on a new line indicating the maximum number of distinct messages that can be transmitted without exceeding the total allowed length L. The output should be written to stdout
.
2
3
5
1 2 3
4
10
2 3 5 7
2
3
</p>