Discounts

Time limit1sMemory limit128 MB

Summary
For each product, given buy-B-get-F-free offers and query amounts, find the maximum saving in dollars for each quantity.
Level

Medium4 of 10

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

Problem

Peter's shop is doing poorly, so he is looking for ways to improve sales. His latest idea is a "buy three, get one free"-style promotion, and he hopes that special offers like this will bring more customers into his shop. Your task is to write a program that helps Peter's customers easily see how much they can save.

Input

The input contains several product scenarios.

Each scenario begins with the product's name on a line by itself. A name is made up of one or more words separated by spaces. A line containing only a single # marks the end of the input; do not process that entry.

The next line contains two integers PDPD and PCPC (0≤PD≤500 \le PD \le 50, 0≤PC≤990 \le PC \le 99) separated by a space: the price of one item, in dollars and cents respectively. PDPD and PCPC are never both zero.

The next line contains one integer DD (0<D≤100 < D \le 10): the number of offers available for this product. It is followed by DD lines, each containing two integers BB and FF (0<B,F≤1000 < B, F \le 100) separated by a space. BB is the number of items that must be bought, and FF is the number of items that may then be taken for free.

The next line contains one integer EE (0<E≤300 < E \le 30): the number of queries that follow. Each of the next EE lines contains a single positive integer less than 500500, the quantity of items the customer wants. Using the available offers, determine the greatest saving the customer can make. Remember that a customer does not have to take every free item an offer allows.

Output

Print one section per product. Each section starts with the product's name on its own line, followed by EE lines -- one per query, in the same order as the input. Every line has the form

Buy N, save $D

where N is the requested quantity and D is the amount saved compared with taking no free items. D is written in the form

d.dd

that is, at least one digit for the dollars, a decimal point, and exactly two digits for the cents.

Separate consecutive product sections with a blank line.

Examples2

  1. Example 1

    Input
    Baked Beans
    0 95
    3
    12 1
    36 5
    100 12
    6
    10
    26
    40
    41
    54
    153
    #
    
    Expected output
    Baked Beans
    Buy 10, save $0.00
    Buy 26, save $1.90
    Buy 40, save $3.80
    Buy 41, save $4.75
    Buy 54, save $5.70
    Buy 153, save $16.15
    
  2. Example 2

    Input
    Milk
    1 50
    1
    2 1
    3
    1
    3
    7
    Big Widget
    12 0
    1
    3 2
    2
    4
    5
    #
    
    Expected output
    Milk
    Buy 1, save $0.00
    Buy 3, save $1.50
    Buy 7, save $3.00
    
    Big Widget
    Buy 4, save $12.00
    Buy 5, save $24.00