Taking Out the Trash
InterviewTime limit3sMemory limit2048 MB
Given n trash bags with weights, find the fewest trips needed when each trip carries one bag or two bags with total weight at most m.
- Level
Medium4 of 10
- Topics
- Greedy, Sorting, Two pointers
- Solved
- No attempts yet
Problem
Peter has far too much trash and needs to take all of it out. There are bags of trash, each with a fixed weight. In one trip, Peter can carry either one bag or two bags, and the total weight of a single trip can be at most milligrams. What is the minimum number of trips Peter needs to take all the trash out?
Input
The first line contains two integers and , the number of bags and the maximum weight Peter can carry in one trip. The next line contains integers , the weight of each bag in milligrams.
Output
Output the minimum number of trips Peter needs to take all the trash out.