#P4095. [HEOI2013] Eden 的新背包问题

    ID: 3031 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>2013各省省选河北枚举,暴力背包进制

[HEOI2013] Eden 的新背包问题

Description

The amnesiac Eden keeps trying to recall the past, yet he can only clearly remember the feeling of longing, not her face or smile.

In his memory, she always liked giving Eden puzzles. On Valentine's Day night, while the two were strolling through a bustling street and gazing at the delicate dolls in a gift shop, she suddenly asked Eden a question: there are nn dolls. Each doll has a cost and a value, and each doll can be bought a limited number of times. With a fixed budget mm, how should one choose which dolls to buy and how many copies of each, so that the total cost does not exceed mm and the total value is maximized.

As is well known, this is a classic multiple knapsack problem. Eden solved it quickly, but she seemed a bit unhappy that her problem was answered so fast. So she made it harder: there will be multiple queries. In each query, a new budget is given and one doll is removed (i.e., that doll cannot be chosen). Then the answer to the multiple knapsack under this condition is asked (i.e., the problem described above).

Now Eden is in trouble, but he does not want to be stumped. Can you help him?.

Input Format

  • The first line contains an integer nn, the number of dolls. Dolls are indexed starting from 00.
  • The next nn lines, each line contains three integers. On line i+2i + 2, the integers ai,bi,cia_i, b_i, c_i denote the cost of one copy of doll ii, the value obtained, and the purchase limit of doll ii, respectively.
  • The next line contains an integer qq, the number of queries.
  • The next qq lines, each contains two integers di,eid_i, e_i, indicating which doll is removed (note that dolls are indexed from 00) and the new budget for this query. The removal does not persist, i.e., different queries are independent.

Output Format

Output qq lines. On the ii-th line, output the answer for the ii-th query.

5 
2 3 4 
1 2 1 
4 1 2 
2 1 1 
3 2 3 
5 
1 10 
2 7 
3 4 
4 8 
0 5
13 
11 
6 
12 
4 

Hint

Sample Explanation.

There are five types of dolls. Their costs, values, and purchase limits are (2,3,4)(2,3,4), (1,2,1)(1,2,1), (4,1,2)(4,1,2), (2,1,1)(2,1,1), (3,2,3)(3,2,3).

There are five queries. Take the first query as an example.

The first query removes the doll with index 11, and asks for the maximum value when the budget is 1010. The remaining dolls are (2,3,4)(2,3,4), (4,1,2)(4,1,2), (2,1,1)(2,1,1), (3,2,3)(3,2,3). If you buy 44 copies of doll 00 (i.e., all of them), and then buy one copy of doll 33, you spend exactly 1010 and the total value is 1313. It can be proved that there is no better solution.

Note that you do not have to buy all available copies of a doll.

Constraints.

  • For 10%10\% of the testdata, it is guaranteed that n10n \leq 10.
  • Additionally, for 20%20\% of the testdata, it is guaranteed that n100n \leq 100, ci=1c_i = 1, q100q \leq 100.
  • Additionally, for 20%20\% of the testdata, it is guaranteed that n100n \leq 100, q100q \leq 100.
  • Additionally, for 30%30\% of the testdata, it is guaranteed that ci=1c_i = 1.
  • For 100%100\% of the testdata, it is guaranteed that 1n10001 \leq n \leq 1000, 1q3×1051 \leq q \leq 3\times 10^5, 1ai,bi,ci1001 \leq a_i,b_i,c_i \leq 100, 0di<n0 \leq d_i < n, 0ei10000 \leq e_i \leq 1000.

Translated by ChatGPT 5