#P2792. [JSOI2008] 小店购物

[JSOI2008] 小店购物

Description

The shop’s promotions are simple and fun:

During a single shopping session, if you buy refined oil at this shop, then when you buy soap you can enjoy a discounted price of 2.002.00 yuan per bar; if you buy soap, then when you buy cola you can enjoy a discounted price of 1.501.50 yuan per can; and so on. In summary: if you buy item AA, then you may buy item BB at a discounted price of PP yuan per unit (with no limit on the quantity).

Interestingly, you need to buy some fixed items, and because different purchase orders may lead the shopkeeper to charge you different totals. For example, you need one bar of soap (original price 2.502.50 yuan), one bottle of refined oil (original price 10.0010.00 yuan), and one can of cola (original price 1.801.80 yuan). If you purchase in the order cola → refined oil → soap, the shopkeeper will charge you 13.8013.80 yuan; but if you purchase in the order refined oil → soap → cola, you only need to pay 13.5013.50 yuan.

The local residents know that JSOI team members are good at programming, so they ask you to write a program: given the original prices of all items in the shop, all promotion rules, and the items you need, compute the minimum amount of money you must spend (you are not allowed to buy any unnecessary items, even if doing so could reduce the total cost).

Input Format

The first line contains an integer n (1n50)n\ (1 \le n \le 50), the total number of item types in the shop.

The next nn lines follow. The (i+1)(i+1)-th line contains a real number ci (0<ci1000)c_i\ (0 < c_i \le 1000) and an integer mi (0mi100)m_i\ (0 \le m_i \le 100) separated by a space, denoting the original price of item ii and the required quantity. The (n+2)(n+2)-th line contains an integer k (1k500)k\ (1 \le k \le 500), the total number of promotion rules.

Then kk lines follow. Each line contains two integers A,B (1A,Bn)A, B\ (1 \le A, B \le n) and a real number P (0P<1000)P\ (0 \le P < 1000), describing one promotion: if you buy item AA, then you may buy item BB at a discounted price of PP yuan per unit. Here PP is less than the original price of item BB. All pairs (A,B)(A, B) in the promotions are distinct. For convenience, the shopkeeper does not use one-cent units, so all prices are multiples of 0.100.10 yuan.

Output Format

Output a single real number: the minimum total cost. Print the number with exactly two decimal places.

4
10.00 1
1.80 1
3.00 0
2.50 2
2
1 4 2.00
4 2 1.50
15.50

Hint

Constraints can be found in the Input Format.

Translated by ChatGPT 5