#K79887. Minimum Shipments

    ID: 35408 Type: Default 1000ms 256MiB

Minimum Shipments

Minimum Shipments

You are given n packages with weights given as a list, and a maximum allowed weight W for each shipment. Your task is to determine the minimum number of shipments required to send all the packages.

The problem can be interpreted as a variant of bin packing. In each shipment, you can add any packages as long as the total weight does not exceed \(W\). Formally, for each shipment, if you choose a subset of packages with weights \(w_1, w_2, \ldots, w_k\), it must satisfy:

[ w_1 + w_2 + \cdots + w_k \leq W ]

Your goal is to find the minimum number of shipments (bins) required such that each package is sent in some shipment.

Note: Although the problem resembles the NP-hard bin packing problem, you can use a greedy strategy (e.g., sorting the packages in descending order and then iteratively packing them) to obtain a solution that works for the given input constraints.

inputFormat

The input is given in two lines:

  • The first line contains two integers n and W, where n is the number of packages and W is the maximum allowed weight per shipment.
  • The second line contains n space-separated integers representing the weights of the packages.

Example:

5 10
2 2 2 2 10

outputFormat

Output a single integer representing the minimum number of shipments required to ship all packages.

Example:

2
## sample
5 10
2 2 2 2 10
2

</p>