Two Parcels per Courier

easy two-pointers sorting greedy 1000 ms · 256 MB

Two Parcels per Courier

Dispatch at QuickVan Logistics sends out one van per delivery run. A van can carry at most two parcels on a run, and the combined weight of the parcels it carries must not exceed the van's weight limit W kilograms. Every parcel weighs at most W kilograms by itself, so a parcel can always be sent alone if it has no suitable partner.

Given the weights of n parcels waiting at the depot, find the minimum number of runs needed to deliver all of them.

Input

The first line contains two integers n and W (1 ≤ n ≤ 2·10^5, 1 ≤ W ≤ 10^9).

The second line contains n integers w_1, w_2, ..., w_n (1 ≤ w_i ≤ W) — the weight of each parcel in kilograms.

Output

Print a single integer: the minimum number of runs required to deliver all parcels.

Notes

In the first example, the 5 kg parcel has no partner light enough to share a run with it (even the lightest 1 kg parcel would push the pair to 6 kg), so it travels alone. The remaining four parcels pair up as (1, 3) and (2, 2), giving 3 runs in total.

In the second example, pairing the lightest parcel with the heaviest one first is essential: sending (1, 4) and (2, 3) finishes everyone in 2 runs. Repeatedly pairing the two heaviest parcels instead — (4, 3) together, then 2 alone, then 1 alone — would take 3 runs, which is not optimal.

Example 1

Input

5 5
1 2 2 3 5

Output

3

Example 2

Input

4 5
1 2 3 4

Output

2

Hints · reveal one at a time if you're stuck

Hint 1

Sort the weights first. Think about the heaviest parcel that is left: which other parcel is the only one worth pairing it with?

Hint 2

If the heaviest and the lightest remaining parcels fit together, pair them; otherwise the heaviest must go alone. Move two pointers inward from the ends of the sorted list and count the runs as you go.

report an issue Prefer your own editor? sooks prep 87 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.