## Editorial for DMOPC '19 Contest 2 P4 - A Greedy Problem

**only**when stuck, and

**not to copy-paste code from it**. Please be respectful to the problem author and editorialist.

**Submitting an official solution before solving the problem yourself is a bannable offence.**

Author:

The intended solution for subtask 1 is to run 0-1 knapsack for each pair of problems. We simply run knapsack without and and then take the number of ways to get a sum of times not greater than .

**Time complexity: **

There is no intended solution for the second subtask.

**Time complexity: **

For the third subtask, we observe that 0-1 knapsack can be thought of as a generating function: . We see that each item is just a factor of the polynomial which means that items can be removed from the knapsack. To remove an item, we loop in increasing values of time and subtract values instead of adding them.

Therefore, we can add all of the items to the knapsack before all the queries. When we answer a query, we just remove and from the knapsack and check the number of ways to get a sum of times not greater than .

**Time complexity: **

## Comments