Official

E - Product Development Editorial by en_translator


Trying all combinations of development plans in a \(\mathrm{O}(2^N N K)\) time does not fit in the time limit. Instead, notice that parameter exceeding \(P\) can be clamped, so there are essentially only \(\mathrm{O}((P+1)^K)\) combinations of intermediate parameters. Then we come up with the following dynamic programming (DP):

  • \(\mathrm{dp}[i][a] =\) minimum total cost of the development plans chosen from the first \(i\) plans, so that the array of parameters becomes \(a=(a_1,a_2,\dots,a_K)\) (or \(\infty\) if it is impossible to do so).

By clamping by \(P\) the elements of \(a\) exceeding \(P\), the number of states can be bounded by \(\mathrm{O}(N(P+1)^K)\). The transition has only two ways, whether or not to adopt the \((i+1)\)-th plan, each costing \(\mathrm{O}(K)\) time, so the complexity of this solution is \(\mathrm{O}(NK(P+1)^K)\), which is fast enough.

The implementation becomes simple by treating \(a\) as a \(K\)-digit \((P+1)\)-ary integer. In fast languages like C++, one can also manage \(a\) as an array and use map for the DP.

posted:
last update: