eolymp
bolt
Try our new interface for solving problems
Problems

Spellcasting

Spellcasting

The casting of a spell is a continuous process. One begins with a certain amount of energy (measured in mana), which can be used to summon elements into the spell. Each element can be summoned in any quantity instantaneously by consuming energy, at which point it immediately becomes a part of the spell, providing power (measured in mana per second) from now on. This power causes energy to accumulate over time, which can in turn be used to summon additional elements. We continue this process until the total power output of our spell reaches a required level. There is one complication in this process, which experienced spellcasters will exploit to cast spells more e ectively. Each element can have up to one parent element, which supports its summoning, making it cost half as much energy as it usually does if the parent element is already present. For example, if element \textbf{A} supports element \textbf{B} and element \textbf{C}, we could summon \textbf{1} unit of element \textbf{A} first at full cost, and then summon \textbf{0.5} unit of element \textbf{B} and \textbf{0.5} unit of element \textbf{C} at half cost. If we were to then summon \textbf{0.5} more unit of element \textbf{C}, that portion would be at full energy cost again, since the \textbf{1} unit of element \textbf{A} is already supporting other elements. Note that all \textbf{3} elements contribute their full power output to the spell; supporting does not interfere with power output in any way, nor does it consume an element. Given an initial amount of energy, a target amount of power, and a description of the spell elements available for summoning, gure out how to cast a spell that reaches the target amount of power in a minimum amount of time. \InputFile Input will consist of multiple test cases. Each test case begins with a line with \textbf{3} space-separated integers \textbf{N}, \textbf{E}, and \textbf{P}, denoting the number of elements, the starting energy (in mana), and the target power (in mana per second) respectively. Following this line are \textbf{N} lines, the \textbf{i}^th of which describes element \textbf{i} by the three space-separated integers \textbf{e_i}, \textbf{p_i}, and \textbf{parent_i} denoting the energy cost to summon, the power output, and the index of the parent element (\textbf{1}-indexed; \textbf{parent_i = 0} if element \textbf{i} has no parent element). Constraints include: \begin{itemize} \item \textbf{1} ≤ \textbf{N} ≤ \textbf{1000}, \textbf{1} ≤ \textbf{E} ≤ \textbf{10^9}, \textbf{1} ≤ \textbf{P} ≤ \textbf{10^9}. \item \textbf{1} ≤ \textbf{e_i} ≤ \textbf{10^9} for all \textbf{i}, \textbf{0} ≤ \textbf{p_i} ≤ \textbf{10^9} for all \textbf{i}, \textbf{0} ≤ \textbf{parent_i} ≤ \textbf{N} for all \textbf{i}. \item At least one pi will be positive. \item No element is its own ancestor; in other words, no element is capable of supporting itself, whether directly or indirectly. \end{itemize} Input will be terminated with a case where \textbf{N = E = P = 0}, which should not be processed. \OutputFile For each test case, print out a single line with a single integer equal to the minimum number of seconds required to reach the target power (rounded up to the nearest integer).
Time limit 5 seconds
Memory limit 64 MiB
Input example #1
1 1 1000000
200 100 0
2 1 1000000
200 100 0
2 1 1
2 1 1000000
200 100 2
2 1 0
0 0 0
Output example #1
30
29
14