This page is still under construction.

Parts of this page are still being built. What you see may change.

Sand Castle

Interview

Time limit1sMemory limit128 MB

Summary
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.
Level

Medium5 of 10

Topics
Greedy, Sorting, Math, Array
Solved
No attempts yet

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 NN merlons (1≤N≤25,0001 \le N \le 25{,}000), numbered 11 through NN. Merlon ii currently has height MiM_i (1≤Mi≤100,0001 \le M_i \le 100{,}000).

Farmer John wants to redesign the wall. He has a list of NN target heights B1,…,BNB_1, \dots, B_N (1≤Bi≤100,0001 \le B_i \le 100{,}000) 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 XX money per unit of height added and YY money per unit of height removed (1≤X,Y≤1001 \le X, Y \le 100).

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 NN, XX, and YY.
  • Each of the next NN lines contains two space-separated integers MiM_i and BiB_i.

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 11 at a cost of 55 (heights become 2,1,12, 1, 1), then raises the second merlon's height by 11 at a cost of 66 (heights become 2,2,12, 2, 1), for a total cost of 1111.

Examples4

  1. Example 1

    Input
    3 6 5
    3 1
    1 2
    1 2
    
    Expected output
    11
    
  2. Example 2

    Input
    1 10 10
    5 5
    
    Expected output
    0
    
  3. Example 3

    Input
    1 3 7
    2 10
    
    Expected output
    24
    
  4. Example 4

    Input
    1 3 7
    10 2
    
    Expected output
    56