#P5391. Cirno's Infinite Knapsack

    ID: 18623 Type: Default 1000ms 256MiB

Cirno's Infinite Knapsack

Cirno's Infinite Knapsack

Cirno starts with an initially empty sequence of items and a backpack of size VV. There are qq operations, each being one of the following:

  • add x y: Append an item with volume $x$ and value $y$ to the sequence.
  • erase: Remove the last item from the sequence.

After each operation, you are required to find the maximum total value that can be obtained by filling the backpack of capacity VV, under the following condition: for every type of item present in the sequence, there are infinitely many copies available. In other words, if the current sequence contains items with volumes xix_i and values yiy_i, determine the maximum total value achievable subject to the constraint

ixikiV,\sum_{i} x_i \cdot k_i \le V,

where each ki0k_i\ge 0 is an integer. This can be computed using an unbounded knapsack formulation with the recurrence

$$\text{dp}[j] = \max_{i \text{ such that } j \ge x_i}\{\text{dp}[j-x_i] + y_i\}, \quad 0 \le j \le V, $$

with the initial condition dp[0]=0\text{dp}[0]=0.

inputFormat

The first line contains two integers VV and qq, representing the backpack capacity and the number of operations respectively. Each of the following qq lines describes an operation in one of the two formats:

  • add x y: Add an item with volume $x$ and value $y$.
  • erase: Erase the last added item.

outputFormat

After each operation, output a single integer: the maximum total value obtainable by filling the backpack of capacity VV with an unlimited supply of each item type present in the sequence. Print each answer on a separate line.

sample

5 5
add 1 2
add 2 3
erase
add 2 2
add 3 4
10

10 10 10 10

</p>