This page is still under construction.

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

Balloons

Time limit1sMemory limit128 MB

Summary
Assign each team's balloons from rooms A and B, respecting supply limits, to minimize total travel distance.
Level

Medium5 of 10

Topics
Greedy, Sorting
Solved
No attempts yet

Problem

Teams that solve a problem at a programming contest receive balloons. Because the balloons must be attached by hand, volunteers are needed.

The balloons are stored in room AA and room BB. A total of NN teams take part in the contest, and every team sits in a different place. Some teams are closer to room AA, others are closer to room BB.

For each team you are given the number of balloons it must receive and its distance from room AA and from room BB. Compute the minimum total travel distance required to attach all balloons. Assume there are plenty of volunteers, and that a team may receive several balloons of the same color. The distance to attach one balloon equals the distance from the room the balloon was taken from to that team. A volunteer can carry only one balloon at a time.

Input

The input consists of several test cases. The first line of each test case contains the number of teams NN (1≤N≤10001 \le N \le 1000) and the numbers of balloons stored in room AA and room BB, namely AA and BB (0≤A,B≤100000 \le A, B \le 10000).

Each of the next NN lines contains the number of balloons KK a team must receive, its distance DAD_A from room AA, and its distance DBD_B from room BB (0≤DA,DB≤10000 \le D_A, D_B \le 1000). Balloons are never insufficient; that is, ∑Ki≤A+B\sum K_i \le A + B in every test case.

The last line of the input contains three zeros and is not processed.

Output

For each test case, print on its own line the minimum total travel distance needed to attach balloons to all teams. Do not include the distance for returning to room AA or BB after attaching a balloon; count only the distance from a room to a team.

Examples2

  1. Example 1

    Input
    3 15 35
    10 20 10
    10 10 30
    10 40 10
    0 0 0
    
    Expected output
    300
    
  2. Example 2

    Input
    1 5 0
    5 3 7
    0 0 0
    
    Expected output
    15