Dynamic Programming21 sections · 916 units
Open in Course

Potions - Problem Statement

IOI variant

Drink potions in order. Each gives or takes health. Never go below 00. Find the largest potions drunk. Greedy with regret: drink each potion. If health goes negative, 'un-drink' the worst positive potion. Use max-heap of potion effects. When negative, pop max positive effect and restore health. This is the regret trick again. The heap tracks regrettable decisions. Time: O(nlogn)O(n \log n).

Time complexity: O(nlogn)O(n \log n).

Space complexity: O(n)O(n). The regret trick appears again. When forced into a bad state, undo the worst previous decision.