Problem 466
Maximum Units on a Truck
Easy · 10 pts · Array, Greedy, Sorting
Problem
You must load boxes onto a truck that can carry at most S boxes. There are several box types: type i has count[i] boxes available, and each of its boxes holds units[i] units. Choose which boxes to load (any boxes of any types, up to S boxes in total) to maximise the total number of units on the truck. Since every box takes the same space, greedily load the boxes with the most units first.
Input
First line: N (number of box types) and S. Next N lines: count and units for each type.
Output
Print the maximum total number of units.
Constraints
1 <= N <= 1000; 1 <= count, units <= 1000; 1 <= S <= 1000000
Example input
3 4 1 3 2 2 3 1
Example output
8
Explanation
With S = 4 and types (1 box of 3 units), (2 boxes of 2 units), (3 boxes of 1 unit), load 1·3 + 2·2 + 1·1 = 8 units.