#C10336. Minimum Number of Trips to Deliver Provisions

    ID: 39530 Type: Default 1000ms 256MiB

Minimum Number of Trips to Deliver Provisions

Minimum Number of Trips to Deliver Provisions

You are given a number of castles and a supply wagon with a maximum load capacity. Each castle requires a certain amount of provisions. Your task is to determine the minimum number of trips needed to deliver all the provisions to the castles.


Formally, you are given an integer NN representing the number of castles, an array a=[a1,a2,...,aN]a = [a_1,a_2,...,a_N] where aia_i is the amount of provisions required by the ii-th castle, and an integer WW representing the maximum capacity of the wagon. In each trip, you can deliver a subset of provisions such that the total does not exceed WW. You need to minimize the number of trips required to deliver all provisions.

Note: The solution uses a greedy strategy by sorting the provisions in descending order and then trying to fill each trip as much as possible.

inputFormat

The input is given via standard input (stdin).

The first line contains two integers NN and WW separated by a space, where NN is the number of castles and WW is the maximum capacity of the wagon.

The second line contains NN integers separated by spaces, representing the provisions required by each castle.

outputFormat

Output a single integer to standard output (stdout) representing the minimum number of trips required to deliver all the provisions.## sample

5 8
2 3 7 4 1
3

</p>