The Crossing
InterviewTime limit2sMemory limit128 MB
Pair at most two riders per shared boat within the weight limit or send each alone, and find the lowest total fare.
- Level
Medium5 of 10
- Topics
- Greedy, Sorting, Two pointers
- Solved
- No attempts yet
Problem
Hektor and his friends decided to take a trip to an island in the middle of a nearby lake. To cross to the island, they must hire boats from a small ferry service.
The service rents out two kinds of boats:
- A first-kind boat carries at most two passengers, and the sum of their weights must not exceed kg. Each single use of such a boat costs .
- A second-kind boat is sturdier but has only one passenger seat, so it carries exactly one person (of any weight) per trip. Each single use costs .
Given the weight of every participant, compute the minimum total cost of ferrying all of them to the island.
Input
The first line contains a natural number (), the number of test sets. The test sets follow, one after another.
The first line of each test set contains four space-separated natural numbers , , , (, ): the number of participants, the cost of one use of a first-kind boat, the cost of one use of a second-kind boat, and the maximum combined weight of the passengers in a first-kind boat, respectively.
The second line of each test set contains space-separated natural numbers (), the weights of the participants.
Output
For each test set, print on its own line the minimum total cost of ferrying all participants to the island.