Arranging Adapters
Time limit4sMemory limit1024 MB
Given n chargers with widths and a power strip with s sockets, find the maximum number of chargers that can plug in at once without overlapping.
- Level
Medium7 of 10
- Topics
- Greedy, Binary search, Sorting
- Solved
- No attempts yet
Problem
The day before the NWERC, you and your team are on a train to Delft. The trip is long and boring, but you have a good idea: "Let's do some training."
-- silence --
You take out your laptop to charge it, but the only socket is already in use. Your friends smirk: "No socket for you, no training for us." The smirks fade quickly when you pull out a power strip, unplug the charger from the socket, and plug it into the power strip. Now there is room for your charger too.

Figure A.1: Illustration of the second input. The first six chargers can be plugged in as shown. This is not the only possible arrangement. However, it is impossible to plug in all seven chargers.
As soon as more sockets become available, your friends take out more devices that need charging. You do not want them to avoid training, so you decide to make them solve a problem instead.
Your power strip has a row of sockets, and each socket is in diameter. All chargers have integer lengths. The plug of each charger is on one of the two ends, and each charger can be used in one of two orientations. Chargers cannot overlap, but they can touch. A charger can extend beyond the end of the power strip as long as its plug is in a socket. Your friends agree to write a program that finds the maximum number of chargers that can be plugged in at once.
Input
The input consists of:
- One line with two integers and (, ), the number of chargers and the number of sockets on the power strip.
- One line with integers (), the width of each charger in centimetres.
You are allowed to rotate chargers by before plugging them in.
Output
Output the maximum number of chargers you can plug into the power strip at the same time.