#P1060. [NOIP 2006 普及组] 开心的金明

[NOIP 2006 普及组] 开心的金明

Description

Jinming is very happy today because his family is about to receive the keys to their new home, which has a spacious room just for him. What makes him even happier is that his mom told him yesterday: “You decide which items to buy and how to furnish your room, as long as the total cost does not exceed NN yuan.” Early this morning, Jinming started making a budget, but there are too many things he wants to buy, so the total will definitely exceed the limit NN. Therefore, he assigns an importance level to each item, divided into 5 levels, represented by integers 1–5, with level 5 being the most important. He also found the price of each item on the Internet (all in integer yuan). He hopes to maximize the sum of price times importance for the selected items, under the constraint that the total cost does not exceed NN yuan (it may be equal to NN yuan).

Let the price of item jj be vjv_j and its importance be wjw_j. If kk items are selected with indices j1,j2,,jkj_1, j_2, \dots, j_k, then the desired total is:

$$v_{j_1} \times w_{j_1} + v_{j_2} \times w_{j_2} + \cdots + v_{j_k} \times w_{j_k}.$$

Please help Jinming design a shopping list that meets the requirements.

Input Format

The first line contains 22 positive integers separated by a space: n,mn, m (n<30000,m<25n < 30000, m < 25), where nn is the total amount of money and mm is the number of items.

From line 22 to line m+1m+1, the ii-th of these lines (corresponding to item ii) contains two non-negative integers v,pv, p, where vv is the price (v10000v \le 10000) and pp is the importance (1p51 \le p \le 5).

Output Format

Output 11 positive integer: the maximum possible value of the sum of price times importance for the selected items without exceeding the total amount of money (this value is <100000000< 100000000).

1000 5
800 2
400 5
300 5
400 3
200 2

3900

Hint

NOIP 2006 Junior, Problem 2.

Translated by ChatGPT 5