All problems

Problem 247

Boats to Save People

Medium · 20 pts · Array, Two Pointers, Greedy, Sorting

Problem

People need to be rescued by boat. Each boat carries at most two people at a time, and their total weight must be at most L. Every person weighs at most L, so each person fits in a boat alone. Find the minimum number of boats needed to carry everyone. Sort the weights and use two pointers. The heaviest remaining person must go in some boat; if the lightest remaining person fits with them, send both, otherwise send the heaviest alone. Each step uses one boat.

Input

First line: N and L. Second line: N weights.

Output

Print the minimum number of boats.

Constraints

1 <= N <= 50000; 1 <= weight <= L <= 30000

Example input

4 3
3 2 2 1

Example output

3

Explanation

With limit 3 and weights 3 2 2 1: the person weighing 3 goes alone. A person weighing 2 shares a boat with the person weighing 1 (total 3). The other person weighing 2 goes alone. That is 3 boats.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Minimum Arrows to Burst Balloons Next: Candy Distribution