Ice Cream

Time limit1sMemory limit128 MB

Summary
Find the minimum cost to buy single, double, and triple scoops from two flavors so every customer's vanilla and chocolate counts are met without contaminating one-flavor orders.
Level

Medium7 of 10

Topics
Greedy, Dynamic programming, Implementation, Math
Solved
No attempts yet

Problem

Some overqualified, underpaid famous computer scientists are heading home after a long day of hard work when they decide to stop for ice cream. They immediately notice that the price per scoop gets cheaper as the order grows: a triple scoop is cheaper per scoop than a double, and a double is cheaper per scoop than a single. But not everyone wants three scoops, so they decide to combine their requests into cost-efficient orders and then split the scoops among themselves. For example, if three people each want a single scoop, they can order one triple scoop and divide it into three single scoops, satisfying everyone at the lowest cost.

The store sells only two flavors: vanilla and chocolate. Everyone uses cups instead of cones, and cups are free and unlimited. Each person requests some number of vanilla scoops and some number of chocolate scoops. There is one complication: if a single purchased unit (a single, double, or triple) contains at least one vanilla scoop and at least one chocolate scoop, the scoops melt into one another and every scoop in that unit becomes contaminated. For instance, a triple with a chocolate scoop on the bottom and two vanilla scoops on top still leaves all three scoops contaminated. People who ordered at least one scoop of each flavor do not mind, since they wanted both anyway, but people who ordered only one flavor will not accept any cross-contamination. What is the lowest total cost that satisfies everyone's requests?

Input

The first line contains the number KK of data sets, followed by the KK data sets, each of the following form.

The first line of each data set contains four integers nn, ss, dd, and tt, where 1≤n≤1001 \le n \le 100 is the number of computer scientists and ss, dd, and tt are the prices in cents of a single, double, and triple scoop, respectively. The prices satisfy 1≤s<d<t≤10001 \le s < d < t \le 1000 and s>12d>13ts > \frac{1}{2}d > \frac{1}{3}t. The next nn lines each contain two integers vv and cc: the number of vanilla scoops and the number of chocolate scoops that customer wants, where 0≤v,c≤100000 \le v, c \le 10000.

Output

For each data set, first print Data Set x: on its own line, where xx is the data-set number starting from 1. On the next line, print the minimum total cost in cents so that everyone receives all requested scoops. Print a single blank line between consecutive data sets.

Examples1

  1. Example 1

    Input
    3
    1 30 40 50
    1 1
    2 60 80 90
    1 0
    0 2
    3 12 16 21
    2 0
    1 3
    1 1
    
    Expected output
    Data Set 1:
    40
    
    Data Set 2:
    140
    
    Data Set 3:
    58