#K78252. Minimum Crystals Required
Minimum Crystals Required
Minimum Crystals Required
You are given several test cases, each describing a set of crystals with specific energy values. Your task is to determine the minimum number of crystals needed so that their total energy is at least T
, the target energy value. You can choose the crystals in any order, but once a crystal is used its energy is added to the running total.
Note: If it is impossible to reach the target energy with the provided crystals, output -1
.
Greedy Strategy: The optimal approach is to select crystals in descending order of energy until the total meets or exceeds the target T
(i.e. \( \sum_{i=1}^{k} E_i \ge T \)).
inputFormat
The first line contains an integer h
representing the number of test cases.
For each test case, the input is structured as follows:
- A line with two integers
N
andT
whereN
is the number of crystals andT
is the target energy. - A line with
N
space-separated integers representing the energy values \(E_1, E_2, \dots, E_N\) of the crystals.
You must process all test cases provided from standard input.
outputFormat
For each test case, output a single line with an integer representing the minimum number of crystals needed to achieve at least T
energy, or -1
if it is not possible.
2
4 15
2 3 5 7
5 8
1 2 3 4 5
3
2
</p>