Official

F - Damage over Time Editorial by en_translator


Suppose that you stop once the monster’s health goes below \(0\). If you cast spell \(i\) in the \(j\)-th last turn, that spell gives a damage of \(\min (j, t_i) \times d_i\) until the last turn. Every turn you should choose the spell that maximizes that maximizes this value.
Let \(t'_1,\lt\ldots,\lt,t'_{N'}\) be the values appearing as \(t_i\), and consider \((N'+1)\) segments \([0, t'_1), [t'_1, t'_2), \ldots,[t'_{N'-1}, t'_{N'}), [t'_{N'}, \infty)\). Let \(X\) be the set of indices of spells such that \(j \lt t_i\), and \(Y\) be the set of other indices. Then, \(X\) and \(Y\) are dependent on which segment \(j\) belongs. This problem can be solved by considering the maximum damage you can make for each segment and simulating the monster’s health in an \(\mathrm{O}(1)\) or \(\mathrm{O}(\log H)\) time each.

For some \(X\) and \(Y\), the maximum damage that can be made is \(\max ( j \times \max_{i \in X} d_i, \max_{i \in Y}t_i \times d_i)\). The sum within this segment can be found in an \(\mathrm{O}(1)\) time by finding the boundary of \(j\) where the former argument becomes larger than the latter in the \(\max\), and by using the formula of sum of arithmetic progression. If the health persists, advance to the next segment; otherwise, find the turn that makes the health \(0\) or less for the first time. Beware of overflows.

posted:
last update: