PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#QHD0000088. Giá mua thấp nhất (Lowest Price in Town)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Giá mua thấp nhất (Lowest Price in Town)

Lowest Price in Town

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

A store sells a single product individually and offers several bulk packages. Each package specifies a number of items and a fixed price. The customer may buy unlimited copies of any package and may combine packages with individual purchases.

For each requested quantity kik_i, find the minimum total cost of buying at least kik_i items. It is allowed to buy more than required if this is cheaper; unused extra items have no resale value. There are no discounts other than the explicitly listed packages.

For instance, if the individual price is $22.00, a two-item package costs $22.00 and a four-item package costs $40.00, then buying four items is the cheapest way to obtain at least three: it costs $40.00 instead of $44.00 for a two-item package plus one individual item.

All monetary values have exactly two decimal places. Implementations should work in integer cents to avoid floating-point rounding errors. All queries refer to the same price list.

Input

  • The first line gives the single-item price PP and the number MM of package offers.
  • Each of the next MM lines contains a package quantity NiN_i and its price PiP_i.
  • The last line contains qq positive integer queries k1,k2,…,kqk_1,k_2,\ldots,k_q, separated by spaces. There is no leading query count: all integers on the final line are queries.

This describes one dataset from the original UVa problem; multiple queries remain inside that dataset.

Output

First print exactly Case 1:. For each requested quantity kk in input order, print Buy k for $d.cc, replacing k with the requested quantity and d.cc with the minimum price in dollars, always with exactly two digits after the decimal point. Do not add blank lines.

Subtasks

  • Subtask 1 (20 points): 0≤M≤40\le M\le4, 2≤Ni≤152\le N_i\le15, 1≤ki≤201\le k_i\le20, 1≤q≤51\le q\le5; all prices satisfy 0<P,Pi<10000<P,P_i<1000 and have exactly two decimal digits.

  • Subtask 2 (30 points): 0≤M≤100\le M\le10, 2≤Ni≤502\le N_i\le50, 1≤ki≤601\le k_i\le60, 1≤q≤201\le q\le20; prices 0<P,Pi<10000<P,P_i<1000.

  • Subtask 3 (50 points): 0≤M≤200\le M\le20, 2≤Ni≤1002\le N_i\le100, 1≤ki≤1001\le k_i\le100, 1≤q≤1001\le q\le100; all prices satisfy 0<P,Pi<10000<P,P_i<1000.

Examples

Example 1

Input

22.00 2
2 22.00
4 60.00
2 4

Output

Case 1:
Buy 2 for $22.00
Buy 4 for $44.00

Explanation: For k=2k=2, one two-item package costs 22.00.For22.00. For k=4$, two two-item packages cost $44.00, less than the four-item offer at $60.00.

Example 2

Input

22.00 2
2 22.00
4 40.00
1 2 3

Output

Case 1:
Buy 1 for $22.00
Buy 2 for $22.00
Buy 3 for $40.00

Explanation: One item is bought individually, two via the two-item package, and three via the four-item package with one extra item, at $40.00.