Sand Castle
InterviewTime limit1sMemory limit128 MB
Given current merlon heights and a multiset of target heights in any order, pair them to minimize the total cost of raising and lowering, where raising costs X and lowering costs Y.
Problem
Farmer John has built a sand castle. Like every good castle, its wall has crenellations — the alternating pattern of embrasures (the gaps) and merlons (the solid, raised blocks).
The wall has merlons (), numbered through . Merlon currently has height ().
Farmer John wants to redesign the wall. He has a list of target heights () and wants the final merlon heights to be exactly this multiset of values, in some order of his choosing (any permutation — not necessarily the order given).
To reshape the merlons he hires craftsmen who charge money per unit of height added and money per unit of height removed ().
Among all ways of assigning the target heights to the merlons, choose the one with the smallest total cost and output that minimum cost. The answer is guaranteed to fit in a signed 32-bit integer.
Input
- The first line contains three space-separated integers , , and .
- Each of the next lines contains two space-separated integers and .
Output
- Output a single integer: the minimum total cost to rebuild the wall.
Hint
In the sample, Farmer John lowers the first merlon's height by at a cost of (heights become ), then raises the second merlon's height by at a cost of (heights become ), for a total cost of .