Tuesday 16 October 2012

Sale Problem

Problem Description:

In a shop each kind of product has a price. For example, the price of a flower is $2 and the price of a vase is $5. In order to attract more customers, the shop runs a promotion in which special offers are made. A special offer consists of one or more product items for a reduced price. Examples: Three flowers for $5 instead of $6, or two vases and one flower for $10 instead of $12.

Write a program that calculates the price a customer has to pay for a certain set of items, making optimal use of the special offers. That is, the price should be as low as possible. You are not allowed to add items, even if that would lower the price.

For the prices and offers given above, the lowest price for 3 flowers and 2 vases is $14: 2 vases and 1 flower for the reduced price of $10 and 2 flowers for the regular price of $4.

Input:

The first line of input.txt contains the number b of different kinds of products in the basket ( 0 <= b <= 5 ). Each of the next b lines contains three values c,k and p. The value c is the (unique) product code ( 1 <= c <= 999 ). The value k indicates how many items of this product are in the basket ( 1 <= k <= 5 ). The value p is the regular price of the item ( 1 < = p <= 999 ).

The next line contains the number s of special offers ( 0 <= s <= 99 ). Each of the next s lines describes one offer by giving its structure and its reduced price. The first number n on such a line is the number of different kinds of products that are part of the offer ( 1 <= n <= 5 ). The next n pairs of numbers (c,k) indicate that k items ( 1 <= k <= 5 ) with product code c ( 1 <= c <= 999 ) are involved in the offer. The last number p on the line stands for the reduced price ( 1 <= p <= 9999 ). The reduced price of an offer is less than the sum of the regular prices.


Output:

Write to the output file output.txt one line with the lowest possible price to be paid for the items in the basket.


Solution: