#K68112. Maximum Number of Cakes
Maximum Number of Cakes
Maximum Number of Cakes
You are given a selection of N cake types, each with a preparation time Ti. Your goal is to choose as many different types as possible such that the total preparation time does not exceed M minutes. Formally, given the list T where Ti represents the preparation time of the i-th cake, you need to maximize the number of cakes chosen subject to the constraint:
$$ \sum_{i=1}^{k} T_{i} \le M $$
The best strategy is to sort the preparation times in increasing order and select cakes one by one until adding the next cake would exceed the time limit M.
inputFormat
The input consists of two lines.
- The first line contains two integers
N
andM
, whereN
is the number of cake types andM
is the maximum total preparation time allowed. - The second line contains
N
space-separated integers, representing the preparation timesT1, T2, ..., TN
for each cake type.
outputFormat
Output a single integer indicating the maximum number of different cake types that can be chosen without exceeding the total time M
.
5 100
30 20 50 10 40
4