#K40432. Minimum Drone Flights
Minimum Drone Flights
Minimum Drone Flights
You are given n packages and a drone with a weight capacity of W. Each package has a weight provided in an array. The objective is to deliver all packages using the minimum number of drone flights. In each flight, the drone can carry either one package or pair two packages together, provided the sum of their weights does not exceed W.
Use a greedy strategy to achieve the optimal solution. One common method is to sort the packages by weight and then use a two-pointer technique: one starting from the lightest package and the other from the heaviest package. If the lightest and heaviest can be paired together without exceeding the weight capacity, then they go in one flight; otherwise, the heavier package goes by itself. Continue this process until all packages are delivered.
It can be mathematically expressed as:
\( \text{flights} = \min \{ k \mid (\text{All packages can be delivered in } k\text{ flights}) \} \)
Help the team determine the minimum number of flights required.
inputFormat
The first line of input contains two integers, n (the number of packages) and W (the drone's weight capacity).
The second line contains n space-separated integers representing the weights of the packages.
outputFormat
Output a single integer representing the minimum number of drone flights required to deliver all packages.
## sample5 10
1 2 3 4 5
3
</p>