#C3569. Minimum Items to Reach Billing Target
Minimum Items to Reach Billing Target
Minimum Items to Reach Billing Target
You are given a set of items with their respective billing values and a target billing amount. Your task is to determine the minimum number of items that need to be purchased such that their total billing amount is at least the target value.
If it is not possible to achieve the target by purchasing all available items, output -1
.
Hint: To minimize the number of items, choose the items with the highest billing values first. This greedy strategy is optimal for this problem.
The problem can be mathematically formulated as:
$$\min \{ k \mid \sum_{i=1}^{k} a_{i} \ge T \}.$$
inputFormat
The input is given via stdin and consists of three lines:
- An integer
n
representing the number of items. - A line with
n
space-separated integers representing the billing value of each item. - An integer
T
representing the target billing amount.
outputFormat
Output a single integer to stdout representing the minimum number of items required to reach or exceed the target billing amount. If it is not possible, output -1
.
5
100 200 300 400 500
700
2
</p>