#C2355. Minimum Delivery Trips

    ID: 45662 Type: Default 1000ms 256MiB

Minimum Delivery Trips

Minimum Delivery Trips

You are given a set of n orders, where each order has a weight. A delivery vehicle can carry orders with a total weight of at most W in a single trip. To optimize the number of trips needed, you can pair the lightest and the heaviest orders together as long as their combined weight does not exceed W.

Your task is to determine the minimum number of trips required to deliver all orders.

The pairing strategy is based on the greedy algorithm. First, sort the weights. Then, use two pointers: one at the start (lightest) and one at the end (heaviest). If the sum of weights pointed by both pointers is at most W, pair them and move both pointers; otherwise, send the heaviest order alone and move the end pointer. Repeat this process until all orders are processed.

Formally, if you let \(w_1, w_2, \dots, w_n\) be the sorted weights such that \(w_1 \le w_2 \le \dots \le w_n\), then you need to compute the minimum number of steps required where in each step you can remove either one or two elements \(w_i\) and \(w_j\) (with \(i \le j\)) provided that \(w_i + w_j \le W\).

inputFormat

The first line of input contains two integers n and W where n is the number of orders and W is the maximum weight capacity per trip. The second line contains n space-separated integers representing the weights of the orders.

outputFormat

Output a single integer representing the minimum number of trips required to deliver all orders.

## sample
5 10
2 3 5 7 1
3

</p>