We are asked: Vova and monster each have health and attack. Vova can choose each turn: either attack monster (reduce monster health by a1) or heal himself (increase health by c1). After Vova's action, if battle is not over (i.e., both still alive), monster attacks Vova (reduce Vova's health by a2). Battle ends when either health <=0. It's possible battle ends after Vova's attack (if monster health <=0) before monster gets to attack? The description: "Then, if the battle is not over yet, the Modcrab attacks Vova" So if Vova's attack kills monster, monster doesn't attack back. Similarly, if Vova heals and his health is already 0? But he wouldn't have been dead. So careful: The sequence: start of phase, Vova chooses action (attack or heal). After that, check if battle is over? Actually: "Then, if the battle is not over yet, the Modcrab attacks Vova." So after Vova's action, if monster's health <=0 or Vova's health <=0, battle ends; otherwise monster attacks. Also, if Vova heals, his health increases; it's impossible that his health becomes <=0 after heal, so only monster health matters for end after Vova's action? Actually, Vova could be at health <=0 before his action? No, at beginning of each phase, both are alive (since battle continues). So Vova's health >0, monster health >0. After Vova's action: if he attacks, monster health may become <=0; if it does, battle ends, monster doesn't attack. If he heals, monster health unchanged >0, so monster attacks. Also, it's possible that Vova's health becomes >0 after heal (always). So after Vova's action, only check if monster health <=0; Vova's health is >0 (since he started >0 and healed only adds). But if Vova attacked, Vova's health unchanged; could be low but still >0. So only condition for battle end after Vova's action is if monster dies. Then monster doesn't attack. If monster is alive after Vova's action, then monster attacks; after monster attack, Vova's health reduces by a2; then check if Vova's health <=0, battle ends. So the battle can also end after monster attack if Vova dies. But Vova wants to win (monster health <=0) before his health <=0.
Goal: minimize total number of phases (i.e., turns) needed to win. We must output the sequence of actions (HEAL or STRIKE) of minimal length.
We have parameters up to 100, small. So we can DP or compute directly.
Let's formalize:
State: (vHealth, mHealth) at start of a phase.
Action: either Attack or Heal.
If Attack: new mHealth = mHealth - a1; if new mHealth <= 0 => battle ends after this action, no monster attack. Total phases = current phase count. Vova's health unchanged.
If new mHealth > 0, then monster attacks: vHealth = vHealth - a2; if vHealth <=0 => Vova dies, battle lost, not allowed. So we must ensure vHealth - a2 > 0 (or >=? "drops to 0 or lower" => health drops to 0 or lower, so if becomes 0, dead. So must be >0 after monster attack to continue). So after attack and monster strike, if vHealth > a2 (strictly greater) then we can continue; else if vHealth == a2, then after monster attack health becomes 0 => dead. So requirement: vHealth > a2? Actually vHealth - a2 > 0 => vHealth > a2. If vHealth > a2, then new vHealth = vHealth - a2 >0, and battle continues. So attack is safe only if after reducing monster, if monster still alive, Vova's health must be > a2.
If Heal: vHealth' = vHealth + c1; mHealth unchanged. Then monster attacks (since monster alive): vHealth'' = vHealth' - a2 = vHealth + c1 - a2. Must have vHealth'' > 0 to continue (strictly). Since c1 > a2, healing always increases net health? But careful: vHealth might be low. Actually vHealth'' = vHealth + (c1 - a2) which is > vHealth because c1 > a2. So if Vova was alive (vHealth >0) at start, after heal and monster attack, he's still alive because vHealth'' >= vHealth + 1? Since c1 - a2 >=1. Indeed given c1 > a2, so c1 - a2 >=1. So heal always results in positive health after monster attack. So heal action is always safe. But if we heal when monster's health is low, we might waste turns.
We want to minimize total phases until monster health <=0 after an attack. We need to decide sequence of actions.
Observation: Healing only serves to increase Vova's health to survive more monster attacks while we are attacking. Since we want to minimize phases, we want to attack as much as possible. But we may need to heal because Vova's health will decrease due to monster attacks after each attack (or after heal? Actually after heal, monster also attacks, so healing also reduces health? Wait: after heal, monster attacks, so vHealth becomes vHealth + c1 - a2, which is net increase of c1 - a2 >0, so healing actually increases health net. So healing gives a net health gain. So we might heal to boost health high enough to survive subsequent attacks while we finish monster.
But also note: If we attack and kill monster, we avoid monster's attack. So the optimal strategy likely involves attacking until we would die, then heal enough to survive until we can finish monster.
Given small numbers, we can BFS over states: (vH, mH, steps) and actions, but we also want minimum steps. However, there is an optimal greedy? Let's think.
We need to ensure Vova never dies. Let's denote:
We need to deliver a certain number of strikes to kill monster: let k = ceil(h2 / a1). Because each strike reduces mH by a1. Actually after each strike, if monster not dead, monster attacks. So the total number of strikes needed is exactly the number of attacks we perform that reduce monster health to <=0. The final strike may kill monster before monster attacks. So we need to plan: In the process, Vova will take damage from monster after each action that does NOT kill monster. That includes: after a heal action, monster always attacks (since heal doesn't kill). After an attack that does not kill monster, monster attacks. So Vova will receive a2 damage for each non-final action that leaves monster alive. Also, the final action (which must be an attack) will not receive damage if it kills monster. So if we have total number of attacks = k (k strikes needed to reduce monster from h2 to <=0), then among those k attacks, the last one kills, so no damage after it. The other (k-1) attacks are followed by monster attacks. Additionally, we may have some heal actions interspersed, each also followed by a monster attack. So total number of monster attacks that occur = (number of attacks before the final) + (number of heals). Let H be number of heal actions. Then total monster attacks = (k-1) + H. Each monster attack reduces Vova's health by a2.
Vova starts with health h1. He gains health from heal actions: each heal gives +c1 health (before monster attack). However, after heal, monster attacks reduces by a2, so net effect of a heal is +c1 - a2. But careful: heal action occurs at start of phase, Vova's health is before action. He heals: health becomes current + c1. Then monster attacks: subtract a2, so final health after that phase = current_health + c1 - a2. So net change: + (c1 - a2) relative to start of that phase.
When we do an attack action that does not kill, health reduces by a2 (monster attack) after the attack, so net change: -a2.
So we can model Vova's health as:
Initial health = h1.
After a sequence of actions (non-final attacks and heals), we have some health before final attack.
The final attack does not incur damage if it kills monster. So we need to ensure that before each attack (including the final), Vova's health is >0 (obviously). But more precisely, before any attack that does not kill, after that attack and subsequent monster attack, health must remain >0. So for non-final attacks, we require that after subtracting a2, health > 0. That means before that attack, health > a2. Also before a heal, no requirement except health positive initially; after heal and attack, health >0 automatically.
We also need to ensure that before final attack, health >0 (obviously). The final attack kills, so no damage; we don't need any health constraint beyond >0.
So the problem reduces to: We need to schedule exactly k strikes, with the last one being a strike. In between, we may have heal actions. The sequence is a mix of strikes (S) and heals (H), ending with S. Let the number of strikes = k, number of heals = h. The total phases = k + h.
Now, what is the health evolution? Let's simulate:
Start: health = h1.
We process actions in order. For each action:
-
If it's a heal: health = health + c1 - a2 (since after monster attack). Equivalent to health += (c1 - a2).
-
If it's a non-final strike (i.e., strike that doesn't kill): health = health - a2. (Because health unchanged by strike, then monster attacks reduces by a2.)
But careful: After a strike that does not kill, we reduce monster health by a1. So we need to keep track of monster health to know when it's the final strike. However, for health constraints, the condition for a non-final strike is that before the strike, health > a2 (since after strike and monster attack, health must be >0). But also after the strike, monster health reduces. For the final strike, no constraint beyond health >0 before strike.
So we can think: We need to schedule strikes such that before each non-final strike, Vova's current health > a2. Because if health <= a2, then after monster attack he would die. Also before a heal, no constraint.
Our goal is minimize total phases => minimize h + k. Since k is fixed (minimum number of strikes needed to kill monster: k = ceil(h2 / a1)). But can we use more strikes? If we use more strikes than needed, that would be wasteful because each strike reduces monster by a1, but if we exceed the needed, maybe we could use fewer strikes? Actually to kill monster we need at least k strikes. Using more than k strikes would be unnecessary; we could kill earlier. So minimal number of strikes is exactly k. So any optimal strategy must use exactly k strikes, because any additional strikes would increase total phases without need. But is it possible that using more strikes (i.e., not attacking with full force each time?) No, each attack does a1 fixed damage. To kill, we need enough cumulative damage >= h2. The minimal number of attacks is k. If we use exactly k attacks, we kill. If we use more, we overkill, but that would be suboptimal because we could have stopped earlier. So optimal uses exactly k attacks. However, what about the possibility that we sometimes attack but the monster health becomes <=0 after the attack, but we might have done an extra heal before the final attack? That still uses k attacks. So the total number of strikes is fixed to k. So our objective is to minimize the number of heals h, subject to the constraint that Vova never dies, i.e., health remains >0 after monster attacks, and before each non-final strike, health > a2.
Given that, we need to determine the minimum number of heals needed, and then produce any sequence achieving that minimal h, with total phases = k + h.
But is it always optimal to do all heals as early as possible? Possibly. Because heals increase health, making subsequent attacks safer. However, heals also cost a turn where monster attacks (net health gain), but also delay killing. The main tradeoff: We need to ensure before each non-final strike, health > a2. Starting from h1, after some attacks, health decreases. We can interject heals to boost health.
We need to find the minimal number of heals such that we can schedule k strikes with the constraints.
Simplify: The order of actions: Since heals give net health increase, they are beneficial to do before attacks that would be dangerous. So we likely want to do heals as early as needed to keep health above a2 before each non-final strike. But we could also do heals in between attacks. But to minimize total heals, we want to use as few as possible.
We can think of this as: We will perform a sequence of actions; let’s denote the health before each action. We'll have k strikes; among them, the last strike is final. For i from 1 to k-1, before strike i, we need health_i > a2. After strike i, health becomes health_i - a2 (since monster attacks). For heals, before a heal, health can be any positive; after heal+monster, health becomes health + (c1 - a2). So heals increase health.
We need to find if with h heals we can survive.
Another viewpoint: The total damage Vova takes from monster attacks = (k-1 + h) * a2. Starting health h1, plus total heal net gain = h * (c1 - a2). Final health after all actions (including after final strike) is not important as long as we survive each step.
But careful: The health after each step must remain >0; however the total net health change approach might overlook that health might dip below zero intermediate if we take too many attacks in a row. But if we ensure that before each non-final strike, health > a2, then intermediate health stays positive. The net sum approach might hide that the order matters: healing later could be too late if health dropped below a2 earlier. So we need to ensure the health never goes <=0 before a heal. Since heals give net increase, we can schedule heals to prevent health from dropping below threshold.
We can think of it as we have to schedule h heals at some points among the k-1 non-final strikes. Let’s denote we have a total of (k-1) attacks that incur damage. In between these attacks, we may insert heals. Essentially, we start with health = h1. We will go through a sequence of "events": each non-final strike reduces health by a2; each heal increases health by (c1 - a2). The final strike doesn't affect health. The constraints: health before each non-final strike must be > a2. Also health must remain >0 throughout; but if before a strike health > a2, then after the strike it becomes health - a2 > 0. So it's sufficient to ensure that before each non-final strike, health > a2. For heals, there's no constraint because even if health is low, heal will increase it and then monster attacks but net gain ensures it stays positive: if health is 1, heal gives +c1, monster subtracts a2, resulting in 1 + c1 - a2 >= 2? Actually c1 - a2 >=1, so result >=2 >0, so heal is always safe. So the only dangerous moments are before each non-final strike, we need health > a2.
Thus, we can plan: start with cur_h = h1. We need to perform (k-1) non-final strikes and h heals in some order, ending with a final strike (which we don't need to check health for, except that cur_h >0). The order can be any sequence where before each strike (among the first k-1) we have cur_h > a2.
Goal: minimize h.
We can think greedily: As long as cur_h > a2, we can strike (non-final) because it's safe. When cur_h <= a2, we cannot safely strike; we must heal to increase cur_h. So we can simulate: we have to deliver k strikes total. We'll do strikes as much as possible, only healing when necessary.
But is that always optimal? Probably yes, because healing costs a turn and we want to minimize heals, so we should only heal when we absolutely need to. However, there is nuance: if we heal earlier, could we reduce total heals? Unlikely, because healing earlier would increase health more, but it's unnecessary if we can still strike safely; it only adds a heal that could be avoided. So the minimal number of heals is the number of times we need to heal when following the rule: strike whenever possible (i.e., cur_h > a2), else heal.
But is it possible that striking when cur_h > a2 leads to a situation later where more heals are needed than if we had healed earlier? Consider: if we strike now, cur_h reduces by a2; later we may need to heal anyway. But if we heal now, cur_h increases by net (c1 - a2), potentially making later health higher so we might need fewer heals overall? But since we are counting heals, healing now adds one heal, so it can't reduce total number of heals; it could only increase. However, could there be a scenario where striking now, even though cur_h > a2, leads to later a point where cur_h becomes <= a2 and you need to heal, but if you had healed earlier instead of striking, maybe you would still need the same number of heals? Actually, consider we have a fixed number of non-final strikes (k-1). We need to perform them all. Each strike reduces health by a2. So total reduction from strikes is (k-1)a2. Heals increase health by (c1 - a2) each. The net change from start to before final strike is: h1 + h(c1 - a2) - (k-1)*a2. This net must be > 0 (since we need health >0 before final strike). But also before each intermediate strike, health must be > a2. So the sequence matters.
If we do strikes as long as cur_h > a2, then we may have to heal only when cur_h <= a2. This seems to minimize heals because we are postponing heals until necessary. But we need to check if there is any scenario where this greedy approach might fail to find a feasible sequence with minimal heals? Usually for such problems (like "Vova and monsters" typical problem) the optimal is to strike when health > a2 else heal. And output sequence accordingly. However, we need to be careful about the final strike: it doesn't require health > a2, just >0. So after we have done all k-1 non-final strikes, we can do final strike even if health is low, as long as >0. So in the greedy algorithm, we simulate:
We have monster health initial H = h2.
We have Vova health cur = h1.
We need to reduce monster health to <=0 with attacks.
We'll plan each turn:
While monster health > 0:
if cur > a2 or monster health <= a1? Actually we need to decide to attack or heal.
But we need to decide in a way that minimizes total turns. Usually the greedy: if Vova's health is such that he can survive the monster's next attack after his attack (i.e., cur > a2) AND the monster's health is such that one more attack will not kill it? Actually if monster health <= a1, then attacking now will kill it, and we don't need to survive a monster attack because monster won't attack. So we can attack even if cur <= a2? Check: if monster health <= a1, then attacking kills monster immediately; the battle ends before monster attacks. So even if cur is 1, it's safe to attack. So the condition for being able to safely attack (without dying) is either: monster health <= a1 (so kill) OR cur > a2 (so after attack, if monster survives, you can survive its attack). So we should attack if either we can kill monster now, or we have enough health to survive a counterattack. Otherwise we must heal.
To minimize phases, we want to attack as much as possible, but we should only heal when necessary. So the greedy algorithm:
Let cur = h1, monster = h2.
Initialize list actions = [].
While monster > 0:
if monster <= a1:
// killing blow
action = STRIKE
// after strike, monster dies, end.
actions.append(STRIKE)
break
else:
// monster > a1, so if we attack, monster will survive and then attack us.
if cur > a2:
// we can safely attack
action = STRIKE
actions.append(STRIKE)
monster -= a1
cur -= a2 // after monster attack
else:
// not enough health to survive a counterattack, must heal
action = HEAL
actions.append(HEAL)
cur = cur + c1 - a2 // heal then monster attack
// monster remains same
After loop, output number of actions and list.
Does this guarantee minimal number of actions? Let's think.
We are always choosing to attack when it's safe (cur > a2 and monster > a1) or when it's a killing blow. We only heal when it's unsafe to attack. This seems optimal because healing delays the kill, so we want to avoid healing unless necessary. Could there be a scenario where healing earlier allows us to attack later with fewer total actions? No, because each heal adds one action. If we can avoid a heal by attacking now, that's better. But what if attacking now, while safe, leads to a later situation where you must heal more times than if you had healed now? That would be because attacking reduces health by a2, which might cause you to need more heals later? But if you heal now, you increase health, but you still have to perform the same number of attacks (k) overall. The total number of heals needed depends on how many times health dips to <= a2. By attacking when safe, you are reducing health, possibly causing more heals later. But if you heal now, you are adding a heal now, so net heals may increase. However, it might be that by healing now you prevent a future heal? But you already added one heal; so total heals would be at least 1. If you didn't heal now, maybe you'd need 2 heals later? That could increase total heals. Let's test with numbers.
Suppose parameters: a2 = 5, c1 - a2 = something. Starting health h1. We need to do (k-1) attacks that each reduce health by 5. We need to maintain health > a2 before each non-final attack.
Suppose h1 = 6, a2=5, k-1=3 attacks needed. Without heals: start 6 >5 -> attack -> health becomes 1 (6-5). Now before second attack, health=1 <=5, not safe. So need heal. If we heal then: health becomes 1 + (c1 - a2). Let c1 - a2 = 10 (c1=15). Then after heal+attack: health=1+10=11. Then we can attack: 11>5 -> attack -> health becomes 6; then third attack: 6>5 -> attack -> health becomes 1; then final attack (k-th) is safe because killing blow? Wait we have total attacks needed = k. In this scenario, we had k-1=3 non-final attacks? Actually if k=4 total strikes needed, we need 3 non-final strikes. After third non-final, health becomes 1. Then final strike: health=1, but monster health after third non-final? Let's compute properly. But think: if we had healed earlier? Suppose we heal before first attack even though health > a2? That would increase health, but would add a heal. After heal: health=6+10=16, then attack: health=11, attack: health=6, attack: health=1, final strike. So total heals =1 as well. Same number. But what if we delay healing? The greedy approach would: start 6>5 -> attack -> health=1. Now health=1<=5 and monster still alive (not kill). So we must heal. So we heal: health=1+10=11. Then attack: health=6, attack: health=1. Then final attack? But we have to check if after second attack (from 11 to 6) we still have monster health > a1? We'll compute later. So total heals=1. So same.
Now consider scenario where health is exactly a2+1? Attack reduces to 1, then need heal. Could we avoid that heal by healing first? That would add a heal anyway. So no reduction.
What about scenario where healing gives a net increase such that after a few attacks we never dip below a2 again, thus reducing total heals? For example, start health = 7, a2=5, c1 - a2 = 2 (c1=7, a2=5). So heal net +2. We need many attacks, say k-1=4. Without any heal: start 7 >5 attack->2; now 2 <=5, need heal. Heal net +2 -> health=4; still <=5? 4 <=5 so need heal again? Actually before next attack, we need health >5. So we might need multiple heals. Let's simulate greedy: cur=7>5 -> strike -> cur=2; now cur<=5 and monster not dead, must heal: heal -> cur=2+2=4; now cur=4 <=5, still unsafe to strike? Actually condition for strike: if monster > a1 (so would survive) and cur > a2. Here cur=4 not >5, so must heal again: heal -> cur=4+2=6; now cur=6>5, can strike -> cur=1; then cur=1 <=5, must heal -> cur=3; still <=5 -> heal -> cur=5? Actually 3+2=5, but need >5? Since after strike (if we strike), we need cur > a2 (strict >?). Health must be > a2 because after monster attack health becomes cur - a2. If cur = 5, then after attack health = 0 => dead. So we need cur > a2, i.e., cur >= a2+1 = 6. So cur=5 not enough. So we must heal again: cur=5+2=7; then strike -> cur=2; etc. This seems many heals. Could there be a better schedule? Suppose we heal first before any attack: cur=7+2=9; then strike -> cur=4; now cur=4 <=5, must heal -> cur=6; strike -> cur=1; heal -> cur=3; heal -> cur=5; heal -> cur=7; strike -> cur=2; etc. Actually still many heals. The total heals needed is determined by the fact that each strike reduces health by a2 and heals increase by net (c1 - a2). This is like a process where we need to keep health above a2 before strikes. This is reminiscent of "remain above threshold" problem. The minimal number of heals can be computed by simulating the worst-case drop.
But is the greedy of striking when safe indeed optimal? Let's reason more formally.
We need to find minimal h such that there exists a sequence of h heals interspersed among (k-1) non-final strikes such that before each strike, health > a2.
Let’s denote the health before the i-th non-final strike (i from 1 to k-1) as H_i. The evolution:
H_1 = h1 + (net gains from heals before first strike) - 0? Actually after some heals, then strike.
Let we have h heals total. Let x_0 be number of heals before first strike. After those heals, health = h1 + x_0 * (c1 - a2). Then we strike (non-final) #1: after strike and monster attack, health becomes H_1 - a2. That is health before next action.
Let x_1 be number of heals between strike #1 and strike #2. Then before strike #2, health = (H_1 - a2) + x_1*(c1 - a2) = h1 + (x_0 + x_1)(c1 - a2) - a2. Similarly, before strike #j, health = h1 + (sum_{i=0}^{j-1} x_i)(c1 - a2) - (j-1)*a2, where x_i are nonnegative integers, and sum x_i = h (total heals). And we need for each j = 1,..., k-1:
h1 + (sum_{i=0}^{j-1} x_i)*(c1 - a2) - (j-1)*a2 > a2
i.e., h1 + S_{j-1}*(c1 - a2) - (j-1)*a2 > a2
=> h1 + S_{j-1}*(c1 - a2) > j * a2
where S_{j-1} = total heals before strike j.
Also, before final strike (which is strike k), we need health > 0 after all preceding actions. That is after all heals and (k-1) strikes, health = h1 + h*(c1 - a2) - (k-1)a2 > 0. But that's automatically satisfied if the inequality holds for j = k-1? Not exactly. But if h1 + S_{k-1}(c1 - a2) - (k-1)*a2 > a2, then it's definitely >0 because a2>=1. So final condition less strict. Actually we need >0, but we might have health exactly 0 before final strike? That would be dead? Because before final strike health must be >0. If health = 0, Vova is dead, cannot act. So we need >0. But we can check separately.
Goal: minimize h = total heals.
We need to choose distribution of heals across intervals (x_i) to satisfy all inequalities. Since the inequalities are monotonic in S_{j-1} (more heals earlier helps later), the optimal to minimize total heals is to place heals as late as possible? Wait, we need to ensure that before each strike, the cumulative heals up to that point are sufficient. The condition for strike j: h1 + S_{j-1}(c1 - a2) > j * a2. The left side increases with S_{j-1}. To minimize total h, we want S_{j-1} to be just enough to satisfy each inequality, but we also have that S_{j-1} is non-decreasing and S_{k-1} = h. This is like we need to find minimal h such that there exists a sequence of S_j (cumulative heals before strike j+1) with S_0 <= S_1 <= ... <= S_{k-1} = h, each integer, and for each j from 1 to k-1: h1 + S_{j-1}(c1 - a2) > j * a2.
Also final: h1 + h*(c1 - a2) - (k-1)a2 > 0? Actually that's automatic if the last inequality for j = k-1 gives > a2, which is >0. But it's possible that h1 + h(c1 - a2) - (k-1)a2 could be <= a2 but >0. Actually the condition for strike k-1 ensures > a2. So after strike k-1 and monster attack, health becomes (value before strike k-1) - a2 = something >0? Wait careful: The condition before strike k-1 is: h1 + S_{k-2}(c1 - a2) > (k-1)a2. After strike k-1 and monster attack, health becomes h1 + S_{k-2}(c1 - a2) - (k-1)a2. That might be positive but could be <= a2. That's fine because next is final strike? Actually after strike k-1, we might have heals (x_{k-1}) before final strike. But we might not need any heals before final strike if health >0. But the condition for final strike is just health >0 before it. So we need h1 + h(c1 - a2) - (k-1)a2 > 0 (since after all heals, before final strike). That's a separate inequality because we may add heals after the last non-final strike. So S_{k-1} = total heals before strike k (the final). So we need h1 + h(c1 - a2) - (k-1)*a2 > 0. This is less strict than > a2. So we need to satisfy both the (k-1) inequalities for the non-final strikes and the final >0.
Now to minimize total heals, we can choose to add heals also after the last non-final strike if needed to ensure final health >0. But usually it's not needed if health after (k-1) strikes is >0, which might be ensured by the previous inequalities? Not necessarily: after (k-1) strikes, health = h1 + S_{k-2}*(c1 - a2) - (k-1)a2. This could be <=0 even if before each non-final strike health > a2? Let's check: suppose before strike (k-1) we have health > a2, so h1 + S_{k-2}(c1 - a2) > (k-1)a2. Then after strike (k-1) and monster attack, health = h1 + S_{k-2}(c1 - a2) - (k-1)*a2 > 0? Since > (k-1)*a2 minus (k-1)*a2 gives >0. Actually if X > Y, then X - Y > 0. So yes, it's >0. So after (k-1) strikes, health is positive. Then we could directly do final strike (since health >0) without any further heals. So the final condition is automatically satisfied if we have satisfied all non-final strike conditions. So we don't need extra heals after the last non-final strike to ensure final health >0. However, we might still want to heal after last non-final strike if we can't kill in one strike? No, we can kill with final strike, so no need.
Thus, the problem reduces to: find minimal total heals h such that there exists a sequence of cumulative heals before each non-final strike satisfying h1 + S_{j-1}(c1 - a2) > j * a2 for j=1,...,k-1, with S_{j-1} non-decreasing integers up to h. Since we can always place heals as early as needed, the minimal total h is simply the smallest integer H such that we can assign S_{j-1} for each j (which can be at most H) to satisfy each inequality. Because we can always set S_{j-1} = min(H, something) but we need monotonicity. To minimize H, we can set S_{j-1} as the minimum required at each stage, i.e., let required heals before strike j be ceil( (j * a2 + 1 - h1) / (c1 - a2) )? Actually inequality: h1 + S(c1 - a2) > j * a2 => S*(c1 - a2) > j * a2 - h1 => S > (j * a2 - h1) / (c1 - a2). Since S is integer, the minimal S satisfying is floor? Let's define:
Need S_{j-1} >= R_j where R_j = max(0, floor((j * a2 - h1) / (c1 - a2)) + 1?) Let's derive:
We require h1 + S * d > j * a2, where d = c1 - a2 > 0.
=> S > (j * a2 - h1) / d.
If j*a2 - h1 <= 0, then S >= 0 is enough (i.e., no heals needed before that strike). So define required minimal integer S as:
req_j = max(0, floor((j * a2 - h1) / d) + 1) if (ja2 - h1) > 0? Actually if (ja2 - h1) is positive, then we need S >= floor((ja2 - h1)/d) + 1 because S must be strictly greater than that fraction. If (ja2 - h1) <= 0, then S >= 0 works.
But note: S must be integer. So:
If ja2 - h1 < 0, req_j = 0.
Else if (ja2 - h1) is exactly divisible by d? The inequality is strict >, so if (ja2 - h1) is a multiple of d, say = md, then we need S > m, so req_j = m+1.
If not multiple, then S > non-integer, so req_j = floor((j*a2 - h1)/d) + 1.
Thus req_j = max(0, floor((ja2 - h1 + d) / d)? Actually careful: The condition S > X, where X may be non-integer. The smallest integer S satisfying is floor(X)+1. So req_j = max(0, floor( (ja2 - h1) / d ) + 1 ) if (j*a2 - h1) > 0; else 0.
But if (ja2 - h1) is negative, floor negative number plus 1 could be 0 or negative? For example, if ja2 - h1 = -3, floor(-3/d) is -1 (if d positive) actually floor(-3/d) = -1? Let's compute: (j*a2 - h1)/d negative. floor of negative number is less than or equal to that number. For -0.5, floor = -1. So floor(negative) +1 could be 0 or negative? For -3/d, say d=5, -3/5 = -0.6, floor = -1, +1 = 0. So if we apply max(0, floor(...)+1) we get 0. Good. So we can simply define:
req_j = max(0, floor((j*a2 - h1 + d - 1) / d)? That's messy. Better to compute directly.
Now we need to choose a non-decreasing sequence S_0, S_1, ..., S_{k-2} (since S_{j-1} corresponds to cumulative heals before strike j) such that S_{j-1} >= req_j for each j =1..k-1, and final total heals h = S_{k-2} + maybe some extra after last non-final strike? But we don't need extra, but we could set total heals H = S_{k-2} (if we don't heal after the last non-final strike) or maybe we could heal after last non-final strike? That would increase H but not needed, so minimal H is at least max over req_j? However, note that S_{j-1} is cumulative heals before strike j. Since S_{j-1} cannot decrease, the minimal possible total heals H is at least the maximum of req_j, because before each strike j we need at least req_j heals. But also we might need to consider that S_{j-1} must be at least the previous S_{j-2} plus something? Actually if we set total heals H = max_j req_j, can we distribute heals such that before strike j, cumulative heals >= req_j? Yes, we can simply put all H heals before the first strike (i.e., x_0 = H, and no heals later). Then before each strike j, S_{j-1} = H, which is >= req_j. That satisfies all inequalities. So minimal total heals H is at least max_{j=1..k-1} req_j, and we can achieve it by doing all heals upfront. Therefore, minimal number of heals is H_min = max_{j=1..k-1} req_j.
But is it always optimal to do all heals upfront? It yields total phases = k + H_min. The greedy algorithm we described earlier (strike when safe else heal) might produce a different number of heals? Let's test.
The greedy algorithm essentially interleaves heals only when necessary. In many cases, it might also yield H_min. But does it always? Let's test with example: h1=10, a1=6, c1=100, h2=17, a2=5. Compute k = ceil(17/6)=3 (since 26=12 <17, 36=18). So k=3, non-final strikes =2. d = c1 - a2 = 95. Compute req for j=1 (before first strike): ja2 = 5, h1=10 => 5-10=-5<0 => req1=0. For j=2: ja2=10, h1=10 => 10-10=0, strict > so need >0 => req2 = floor(0/95)+1? Actually (j*a2 - h1) =0, need S > 0 => minimal S=1. So req2=1. So H_min = 1. That matches output: 4 phases total = 3 strikes + 1 heal. The greedy algorithm: start cur=10, monster=17. monster > a1? 17>6 so not kill. Check cur > a2? cur=10 >5, so strike. After strike: monster=11, cur=5. Next: monster=11>6? yes. cur=5 >5? No (cur=5 not >5). So must heal: heal, cur=5+95=100? Actually heal then monster attacks: cur = cur + c1 - a2 = 5+100-5=100. Then monster=11. Now cur=100 >5, strike: monster=5, cur=95. Now monster=5 <=6? Actually 5 <=6, so we can kill? At start of next phase, monster=5, cur=95. Since monster <= a1, we attack (kill) without risk. So actions: STRIKE, HEAL, STRIKE, STRIKE? Wait that's 4 actions: first strike, then heal, then strike (non-final? Actually after heal, we struck: monster went from 11 to 5? Let's simulate: initial monster=17, after first strike=11, after heal no change, after second strike monster=5, after final strike monster=-1? Actually second strike reduced to 5, not <=0, so monster still alive, then monster attacks? Wait our sequence: Phase1: strike -> monster=11, cur=5; Phase2: heal -> cur becomes 5+100=105? Wait after heal, monster attacks, so cur = cur + c1 - a2 =5+100-5=100. Phase3: strike -> monster=11-6=5? Actually monster was 11, strike reduces to 5, then monster attacks (since monster alive), cur = 100 - 5 = 95. Phase4: since monster=5 <= a1=6, we can strike and kill without monster attack. So total phases =4, matches output. Greedy yields 1 heal. So yes.
Now test second example: h1=11, a1=6, c1=100, h2=12, a2=5. k = ceil(12/6)=2 (since 26=12). non-final strikes =1. req for j=1: ja2=5, h1=11 => 5-11=-6<0 => req1=0. So H_min=0. Greedy: start cur=11, monster=12>6, cur>5? yes => strike: monster=6, cur=6 (since after monster attack). Now monster=6 <=6? Actually 6 <=6 so we can kill: next phase strike kill. So actions: STRIKE, STRIKE. Total 2 phases, no heals. Good.
Now test a more complex scenario: h1=5, a1=10, c1=8, a2=5 (c1 > a2? 8>5 yes). h2=30? Let's try small to see if greedy might use more heals than necessary. But given that minimal total heals H_min = max req_j, and greedy might sometimes use more heals? We need to check if greedy always achieves exactly H_min. Let's try to construct a counterexample.
The greedy algorithm: at each step, if monster <= a1, strike (kill). Else if cur > a2, strike. Else heal. This algorithm doesn't consider the future explicitly; it just ensures immediate safety. Could it ever heal when it's not strictly necessary to achieve the minimal total heals? Possibly if healing later would require more heals? But we argued that any heal adds one, so minimal is H_min. But could greedy sometimes end up doing more heals than H_min? For instance, if we heal when it's not needed, we increase total heals, which is not minimal. But greedy only heals when cur <= a2 and monster > a1 (so not safe to strike). At that moment, striking would be unsafe (cause death). So healing is necessary to avoid death. But is it always necessary? Could there be an alternative: instead of healing at that point, we could have healed earlier to avoid being in that low health situation? But that would still be a heal, possibly same number. Or could we avoid healing altogether by having struck earlier at different times? But we have a fixed number of strikes. The only way to avoid healing at a point where cur <= a2 and monster > a1 is to have had higher health at that point, which would require prior heals. So healing is unavoidable at that moment if we haven't healed enough earlier. However, could we have avoided the low health situation by not striking earlier when it was safe? For example, if we strike when safe, we reduce health, eventually becoming low. Could we instead have healed earlier (when health was still > a2) to boost health higher, so that later we might need fewer total heals? But that would involve doing a heal when it wasn't immediately necessary, which adds a heal. The total number of heals would increase by that early heal, but could it reduce the number of heals needed later? Possibly if the early heal prevents multiple later heals? But if we add one heal early, total heals becomes at least 1 + later heals (if any). Without early heal, we might need multiple heals later? Let's test with numbers.
We need to find a scenario where the greedy algorithm (strike when safe) leads to needing more heals than the theoretical minimum H_min. Since H_min is the max over j of req_j, which is the minimal cumulative heals needed before each strike. In greedy, the cumulative heals before each strike might not be the maximum possible early; they are added only when needed. But we can also add heals early. The minimal total heals is achieved by doing all heals upfront, giving S_{j-1} = H for all j. That yields H_min = max req_j. Greedy, on the other hand, might result in S_{j-1} that are sometimes less than H_min? But H_min is the maximum requirement; before some strike j, we need at least req_j heals. Greedy will ensure that before each strike, the cumulative heals are at least enough to survive that strike? Let's see.
In greedy, before each strike (non-final), we have cur > a2 (or we wouldn't strike). That condition is exactly h1 + (heals so far)d - (number of strikes so far)a2 > a2? Wait, at the moment of deciding to strike (non-final), we have cur > a2. The cur at that moment is: h1 + (heals performed so far)d - (strikes performed so far)a2 (since each heal net +d, each strike net -a2). Let s be number of strikes already performed (non-final? Actually includes all strikes done so far, all non-final because we haven't killed yet). Let h_sofar be number of heals performed so far. Then cur = h1 + h_sofard - sa2. The condition cur > a2 implies h1 + h_sofard - sa2 > a2 => h1 + h_sofar*d > (s+1)a2. This is exactly the inequality for the next strike (since the next strike would be the (s+1)-th non-final? Let's index: Suppose we have performed s strikes already, and we are about to perform strike number (s+1). This strike is non-final because monster still alive. The requirement is that before this strike, we need health > a2, which translates to h1 + Sd > (s+1)*a2, where S = heals performed before this strike = h_sofar. So greedy ensures that before performing a non-final strike, the condition holds. So after performing some strikes and heals, the inequality holds for the upcoming strike.
Now, what about the cumulative heals distribution? In greedy, we only heal when cur <= a2, i.e., when h1 + h_sofard - sa2 <= a2, i.e., h1 + h_sofard <= (s+1)a2. At that point, we must heal. After healing, h_sofar increases by 1, cur increases by d, and then we might be able to strike again. So essentially greedy ensures that before each strike, the inequality is satisfied strictly, and we heal precisely when the inequality is not satisfied. This is reminiscent of a process that keeps the quantity h1 + h_sofard - sa2 above a2 before each strike. It might result in a pattern where h_sofar grows as needed.
But could it ever lead to total heals greater than the maximum req_j? Let's compute req_j: req_j is the minimal integer S such that h1 + Sd > ja2. Greedy's heals count before strike j (i.e., after (j-1) strikes) is some S_{j-1}. Is it guaranteed that S_{j-1} <= req_j? Actually we need S_{j-1} >= req_j to satisfy inequality. Greedy ensures the inequality holds, so S_{j-1} must be at least req_j. But could S_{j-1} be strictly greater than req_j? Possibly if greedy heals more than necessary. But does greedy ever over-heal? It only heals when condition is not met, and then heals exactly once? After healing once, condition might become met; but it might still not be enough, requiring another heal. So it will heal repeatedly until condition is met. That yields S_{j-1} exactly the smallest integer that satisfies h1 + Sd > ja2? Let's see.
When we are about to perform strike number j (i.e., after (j-1) strikes and some heals), we need h1 + Sd > ja2. Greedy will have performed strikes sequentially. The process: start with S=0, s=0. While s < j (we haven't done j strikes yet), we attempt to do strikes if condition holds; otherwise heal. This is essentially a simulation of a process where we try to increase S only when needed. At the moment just before performing the j-th strike, we will have S such that h1 + Sd > ja2, and we will have performed exactly j-1 strikes. Could it be that S is larger than the minimal required? Let's simulate with numbers where d is small.
Example: h1 = 5, a2 = 5, d = 1 (c1=6, a2=5). Then req for j=1: ja2=5, h1=5, need S > 0 => req1=1. For j=2: need S > (10-5)/1 =5 => req2=6? Actually (25 -5)=5, need S >5 => req2=6. So H_min = max(1,6)=6.
Now simulate greedy:
Parameters: h1=5, a1=? Not needed but assume a1 enough to kill monster in some strikes. Let's define monster health large, but we only care about heals count before we kill. We just want to see how many heals greedy does before performing 2 strikes? Actually we need k strikes total, but we can compute for a given number of non-final strikes. Let's set k-1 = 2? Actually we need to go through the process until monster dead. But for the purpose of counting heals, we can simulate with arbitrary monster health requiring, say, 3 strikes total (k=3 => non-final strikes =2). We'll compute total heals needed by greedy. Since d=1, it's a small net gain.
Let's set a1 such that we need exactly 3 strikes: maybe monster health = something. But the number of strikes needed is independent of healing. So we can just simulate the process of strikes and heals until we have performed 2 non-final strikes, and see total heals.
Initialize cur=5, monster health large, but we only care about strikes. We'll simulate as per greedy:
Phase start: monster > a1? Suppose monster health > a1 (so not kill). cur=5 > a2? cur=5 >5? No, it's equal, not greater. So condition cur > a2 is false. Since cur <= a2, we must heal. So action HEAL.
Heal: cur = cur + c1 - a2 = 5+6-5=6? Actually c1=6, a2=5, so net +1: cur becomes 6.
Now monster attacks? After heal, monster attacks: cur = 6-5=1? Wait careful: After heal action, monster always attacks. So we add c1, then subtract a2: cur = cur_initial + c1 - a2 = 5 + 6 - 5 = 6. Yes, correct.
Now cur=6. monster still > a1. Check cur > a2? 6 >5, so strike.
Strike: monster -= a1; then monster attacks because monster still alive? We need to know if after strike, monster health >0. Since we haven't defined monster health, but assume it's still >0, so monster attacks. So cur = 6 - a2 = 1. So after first strike, cur=1, and we've performed 1 strike.
Now need to perform second non-final strike? Actually after this strike, we have done 1 strike. We need to do one more non-final strike before final? If total strikes needed is 3, we have 1 done, need 2 more (one non-final, then final). But let's continue.
Now cur=1, monster > a1? yes. cur > a2? 1 >5? false, so must heal.
Heal: cur = cur + c1 - a2 = 1+6-5=2. Actually net +1: 1->2? Wait initial cur=1, heal adds c1=6, then monster subtract a2=5 => net +1 => 2. Yes.
Now cur=2, still <=5, so must heal again? Check: cur=2 >5? false, so heal.
Heal: cur = 2+6-5=3.
cur=3 <=5, heal again: cur=4.
cur=4 <=5, heal again: cur=5.
cur=5 >5? false (needs >5). So heal again: cur=6.
Now cur=6 >5, so we can strike.
Strike: cur becomes 6 - a2 = 1. That's second non-final strike done.
Now after two strikes, cur=1. If we need a final strike (third strike) to kill, we check: monster maybe now <= a1? If we set monster health appropriately, after two strikes maybe monster still > a1? But final strike is safe regardless of health if monster <= a1 or if cur > a2? Actually for final strike, if monster > a1, then after strike monster would still be alive? No, final strike must kill. So we need to ensure that after two strikes, monster health <= a1, so that the next strike kills without requiring cur > a2. But we can also if monster still > a1, we would need to strike again, but that would be a non-final strike? Actually if after two strikes monster > a1, we would need a third strike that would not kill, so it would be non-final. So for final to be safe, we need monster health <= a1. So we must ensure that after k-1 strikes, monster health <= a1. Since we defined k = ceil(h2 / a1), after k-1 strikes, monster health = h2 - (k-1)*a1 >0 but <= a1? Let's check: If k = ceil(h2 / a1), then (k-1)a1 < h2 <= ka1. So after k-1 strikes, monster health = h2 - (k-1)*a1, which is between 1 and a1 inclusive. So yes, it is <= a1. So the final strike will kill. Therefore, after performing (k-1) non-final strikes, we can perform the final strike regardless of cur (as long as cur >0). So after our two non-final strikes, cur=1 >0, we can final strike: strike and kill (monster attack omitted). So total heals: count from simulation: we started with a heal, then strike, then we healed 5 times? Let's count: actions: H (heal), S (strike), H, H, H, H, H? Wait we had: start -> heal (1), strike (2), then heal (3), heal (4), heal (5), heal (6), heal (7), then strike (8), then final strike (9). So total heals = 6? Actually we had: after first strike, we healed 5 times? Let's recount:
Initial: cur=5, monster>a1, cur not >a2 so heal: heal count=1, cur=6.
Then strike: strike count=1, cur=1.
Now cur=1 <=5, so heal: heal count=2, cur=2.
cur=2 <=5, heal: count=3, cur=3.
cur=3 <=5, heal: count=4, cur=4.
cur=4 <=5, heal: count=5, cur=5.
cur=5 <=5? Condition for strike: need >5, so not safe, heal: count=6, cur=6.
Now cur=6 >5, strike: strike count=2, cur=1.
Now final strike: strike count=3, kill.
Total heals = 6. This matches H_min = 6. So greedy produced exactly H_min.
So far so good.
But could there be case where greedy results in more heals than H_min? Suppose we have d large. Then req_j might be something like 0 for some j and 1 for later j. Greedy will wait until needed, then heal enough times to get over threshold. It will heal exactly the required number to satisfy the inequality for the upcoming strike. Since the required number for that strike is exactly req_{j} (minimal S), and greedy will increase S until condition holds, and since each heal adds d, after some number of heals, condition will become true. The number of heals added at that point will be exactly the amount needed to go from current S to at least req_{j}. But is it possible that greedy overshoots? Because it heals one at a time, and after each heal it checks if it can strike. It will stop as soon as cur > a2. That condition corresponds to h1 + S*d > (s+1)a2. Starting from a state where inequality is false, each heal increases S by 1, thus left side increases by d. Since d is positive, eventually the inequality becomes true. The smallest S for which it becomes true is exactly req_{s+1} (since s is number of strikes already done). So after we heal until we reach S = req_{s+1}, we will be able to strike. However, note that req_{s+1} might be larger than the current S by more than 1, so we will heal multiple times in a row. After each heal, we re-evaluate: is cur > a2? That's equivalent to checking if h1 + Sd > (s+1)*a2. So we will heal exactly until S reaches the smallest integer satisfying that inequality. So the total S after those heals will be exactly req_{s+1}. So greedy yields S_{j} = req_{j+1} for each j? Let's track.
Let’s define after we have done s strikes (all non-final so far). Let S be heals done so far. Greedy algorithm loop: while (monster > a1) and (cur <= a2) but also monster > a1? Actually the decision: if monster <= a1 -> strike (kill). else if cur > a2 -> strike. else -> heal.
So when monster > a1 and cur <= a2, we heal. After healing, we re-check. So effectively, before performing the next strike (which will be strike number s+1), we will heal until h1 + S*d > (s+1)*a2. Once that holds, we strike. So after the healing phase, the heals count becomes the minimal S' such that h1 + S'd > (s+1)a2, i.e., S' = req_{s+1}. Then we strike, increasing s by 1, and cur becomes h1 + S'd - (s+1)a2 (since after strike and monster attack). That value may be positive but possibly <= a2. Then for next strike, we again may need to heal until S reaches req_{s+2}? But note that after striking, S remains the same (heals count unchanged). So before strike s+2, we have S = req_{s+1}. We need to achieve S >= req_{s+2}. Since req_{s+2} is generally larger than req_{s+1} (because RHS increases), we may need additional heals. The number of additional heals needed is req_{s+2} - req_{s+1} (if req_{s+2} > req_{s+1}). So total heals after finishing all non-final strikes will be req_{k-1} (since last non-final strike is strike number k-1, before it we need S = req_{k-1}). And after final strike, total heals remains that. So total heals = req_{k-1}. But is req_{k-1} equal to max_{j=1..k-1} req_j? Since req_j is non-decreasing? Let's check: req_j is the minimal S such that h1 + Sd > ja2. As j increases, RHS increases, so req_j is non-decreasing. Because if you need a certain S to exceed ja2, to exceed a larger ja2 you need at least as many S (maybe more). So req_j is non-decreasing in j. Therefore, max_{j=1..k-1} req_j = req_{k-1}. So H_min = req_{k-1}. And greedy yields total heals = req_{k-1} as well. So greedy is optimal and yields minimal heals.
But is req_j always non-decreasing? Let's check: Suppose d > 0. The condition h1 + Sd > ja2. For fixed S, as j increases, RHS increases, so the inequality becomes harder to satisfy. So the minimal S required increases with j. Yes, it's monotonic. So H_min = req_{k-1}.
Thus, the greedy algorithm described yields exactly the minimal number of heals.
However, we must also consider the possibility that sometimes we might be able to kill the monster earlier than k strikes if we do more strikes? No, k is minimal number of strikes to reduce monster health to <=0, but it's possible to kill with fewer strikes if we overkill? No, each strike reduces by a1, so to reduce from h2 to <=0, we need at least ceil(h2/a1) strikes. So k is the minimum. So any strategy must have at least k strikes. So minimal total phases is at least k + H_min. And greedy achieves that.
But is there any nuance about the final strike? In greedy, we attack when monster <= a1 (kill) regardless of cur. That is correct because it ends the battle without monster attack. So that yields a strike that might be performed even if cur <= a2. That's fine. So the greedy algorithm yields exactly k strikes and H_min heals.
Thus the problem can be solved by simulating the greedy algorithm and outputting actions.
But we must also consider cases where initial health might be low such that even before any action, cur <= a2, and monster > a1, so we must heal. That's handled.
Edge cases: When c1 > a2, heal net positive.
We must ensure that the simulation terminates because we assume Vova can always win. The problem states "you may assume that Vova never runs out of healing potions, and that he can always win." So simulation will eventually kill monster.
Implementation: read h1, a1, c1; read h2, a2.
Initialize cur_h = h1, monster_h = h2.
List actions = [].
While monster_h > 0:
if monster_h <= a1:
# killing blow
actions.append("STRIKE")
break
else:
if cur_h > a2:
actions.append("STRIKE")
monster_h -= a1
cur_h -= a2
else:
actions.append("HEAL")
cur_h += c1 - a2 # because heal then monster attack
# monster_h unchanged
After loop, print len(actions) and each action.
But is it correct to subtract a2 after a strike when monster_h > a1? Yes, because after strike, if monster_h > a1, it's still alive, so monster attacks. However, after we subtract a1 from monster_h, we need to check if monster_h <= 0? Actually condition "if monster_h <= a1" we check before action. But after we strike, monster_h reduces by a1; it's possible that after reduction, monster_h becomes <= 0, even if it was > a1 before? Wait: monster_h > a1 means monster_h > a1. After subtracting a1, new monster_h = old - a1. Since old > a1, new monster_h > 0? Actually if old > a1, then old - a1 > 0. For example, old = a1+1 gives new =1 >0. So it's still >0. So monster still alive. But could old be exactly a1+0? If old > a1, then old >= a1+1, so new >=1. So yes, still alive. So after strike, monster is still alive, so monster attacks. So subtracting a2 is correct.
But what about the case where old monster_h > a1 but after strike it becomes 0? That would require old = a1, but we excluded that with condition monster_h <= a1. So correct.
One more nuance: In the condition for striking, we used cur_h > a2. But is it sufficient? If cur_h > a2, then after monster attack, health becomes cur_h - a2 > 0, so alive. However, what if cur_h > a2 but after the strike, monster health becomes exactly something? No effect.
Now, what about the scenario where after a heal, we might be able to kill monster in the same phase? That's impossible because heal doesn't affect monster health. So we must attack to kill.
Thus the greedy algorithm is correct.
But wait, could there be a scenario where it's beneficial to attack even when cur_h <= a2, if monster_h <= a1? That's covered by the first condition (monster_h <= a1) which we check first, regardless of cur_h. In that case we attack and kill, no need to check cur_h > a2. So order of checks: if monster can be killed now, we attack. Otherwise, if we can survive a counterattack (cur > a2), we attack. Otherwise, heal.
Now, is it possible that attacking when cur_h <= a2 but monster_h > a1 could still be safe if the attack kills the monster? No, because monster_h > a1 implies after attack, monster health >0, so monster attacks and Vova would die if cur_h <= a2? Actually if cur_h <= a2, after monster attack, health becomes cur_h - a2 <= 0, so Vova would die. So unsafe.
Thus greedy is correct.
But we need to verify that the sequence produced indeed has minimal total phases. We argued that minimal total phases = k + H_min, where H_min = req_{k-1} (if k>=2) else if k=1 (monster health <= a1 initially), then no heals needed, total phases =1. In greedy, if k=1, we attack immediately and win.
Let's test some edge cases manually.
Case: h1=1, a1=100, c1=100, h2=100, a2=1 (c1 > a2). k = ceil(100/100)=1? Actually 1 strike kills? 100 damage, monster health 100 => 1 strike kills. So initial monster_h <= a1? 100 <=100, yes. So we attack, win in 1 phase. Greedy: monster_h <= a1 -> strike. Output 1 STRIKE. Works.
Case: h1=1, a1=1, c1=100, h2=100, a2=99 (c1=100 >99). k = ceil(100/1)=100. So need 100 strikes. non-final strikes = 99. d = c1 - a2 = 1. Compute req for j=1: ja2=99, h1=1 => need S > (99-1)/1 =98 => req1=99? Let's compute: (ja2 - h1)=98, need S > 98 => S >=99. So req1=99. Since req_j non-decreasing, req_{99} will be huge? Actually j goes to 99, so req_{99} = floor((9999 -1)/1)+1? That's large. But is it winnable? Starting health 1, each heal gives net +1. To survive 99 non-final strikes, we need to have health >99 before each strike? Wait a2=99, so need health >99 before each non-final strike. Starting from 1, we need to heal a lot. The required total heals would be something like: before strike 1 need health >99 => need at least 99 heals? Let's compute using formula: h1 + Sd > ja2. For j=1: 1 + S > 99 => S >=99. So after 99 heals, health = 1 + 991 = 100 before first strike? But we would have done 99 heals upfront. Then after first strike and monster attack, health = 100 - 99 = 1. Then before second strike, need health >99 again, so we need more heals. But after first strike, S=99. To get health >99 again, we need additional heals. So total heals needed is more than 99. Let's compute req for j=2: need 1 + S > 198 => S >=198. So req2=198. So total heals = req_{99} = floor((9999 -1)/1)+1 = floor(9801-1? Actually 9999=9801, minus h1=1 => 9800, +1 = 9801? Wait compute properly: For j=99, need 1 + S > 9999 = 9801 => S > 9800 => S >= 9801. So total heals = 9801. That's huge but within bounds? Input constraints up to 100 for health and attack, but h2 up to 100, a2 up to 99, so 9999 = 9801, which could be large but still within reasonable simulation? Since we might need to simulate 9801 heals + 100 strikes = 9901 actions, which is okay? Output could be up to maybe 10000 lines, which is acceptable. However, the problem constraints are small (up to 100), but the required number of actions could be as high as ~10000? Let's estimate worst-case: h1=1, a2=99, c1=100 => d=1. Need k = ceil(h2 / a1). Worst-case h2=100, a1=1 => k=100. Then non-final strikes=99. req_{99} ~= (99*99 - 1) + 1? Actually as computed, 9801. So total actions ~ 9901. That's within typical output limits? Usually Codeforces problems allow up to maybe 10^5 lines. 9901 is fine. But is there any scenario with even larger? If a1=1, h2=100, k=100, a2 can be up to 99, d = c1 - a2, minimum d? Since c1 > a2, minimum d is 1 (c1 = a2+1). So worst-case total heals ~ (k-1)*a2? Actually approximate: req_{k-1} roughly ( (k-1)*a2 - h1 ) / d + something. With d=1, it's about (k-1)*a2. Since k <= 100, a2 <= 99, product <= 9900. So total phases <= 100 + 9900 = 10000. Acceptable.
Thus simulation with while loop up to ~10000 iterations is fine.
Now, we must ensure that simulation terminates correctly and doesn't infinite loop. Since each heal increases cur by d (positive) and each strike reduces monster, eventually monster will be <= a1 and we kill. Given we assume always winnable, it's fine.
So we can implement the greedy simulation and output.
But is there any scenario where greedy might not be optimal because we might want to heal even when cur > a2 to reduce total phases? That would add a heal, increasing total phases, so not optimal. So no.
Thus solution: simulate.
Let's test with some random cases to be safe.
We'll write a small brute-force for small parameters to verify that greedy yields minimal phases. Since constraints small, we can do BFS/DP to find minimal phases for all small values and compare. But given reasoning, it's solid.
Nevertheless, let's test manually a borderline: h1=10, a1=3, c1=5, a2=4 (c1>a2? 5>4 yes). d=1. h2=10. k = ceil(10/3)=4 (since 33=9<10, 43=12). non-final strikes=3. Compute req: j=1: j*a2=4, h1=10 -> 4-10=-6<0 req1=0. j=2: 8-10=-2<0 req2=0. j=3: 12-10=2, need S > 2/1=2 => S>=3? Actually (12-10)=2, need S > 2 => S >=3. So req3=3. So H_min=3. So total phases = 7.
Simulate greedy:
cur=10, monster=10, monster > a1? 10>3, cur > a2? 10>4 yes, so strike: monster=7, cur=6.
cur=6, monster=7>3, cur>4? 6>4 yes, strike: monster=4, cur=2.
cur=2, monster=4>3, cur>4? 2>4 false, so heal: cur=2+5-4=3? Actually heal: cur = cur + c1 - a2 = 2+5-4=3.
Now cur=3, monster=4>3, cur>4? 3>4 false, heal: cur=3+5-4=4.
cur=4, monster=4>3, cur>4? 4>4 false (need >4), heal: cur=4+5-4=5.
cur=5, monster=4>3, cur>4? 5>4 true, strike: monster=4-3=1, cur=5-4=1.
Now monster=1 <= a1? 1<=3 yes, strike (kill). So actions: S, S, H, H, H, S, S? Wait count: 1st S, 2nd S, then H, H, H, then S (third strike), then S (final). That's 7 actions: S,S,H,H,H,S,S. Heals=3. That matches H_min=3.
Now, could there be a sequence with only 2 heals? Let's check if possible. We need to survive 3 non-final strikes. Starting h1=10, after two strikes without heal: first strike: cur=6, second strike: cur=2. Before third strike, we need cur >4. So we need to increase cur from 2 to at least 5 before third strike. Each heal nets +1. So we need at least 3 heals (2->3,3->4,4->5). So indeed 3 heals required. So minimal.
Now, suppose we had healed earlier: after first strike, cur=6, we could heal to get higher, but that would increase heals count, not reduce. So greedy optimal.
Thus greedy works.
Now, about the condition "if monster <= a1" we attack. But what if monster <= a1 but cur <= 0? That cannot happen because cur >0 at start of phase. So fine.
One more nuance: After a heal, we always get monster attack. That reduces cur by a2. But what if after heal and monster attack, cur becomes <=0? Since c1 > a2, cur after heal and attack = previous_cur + c1 - a2. Since previous_cur >0, and c1 - a2 >=1, result >=2? Actually if previous_cur =1, c1 - a2 =1, result =2 >0. So always >0. So safe.
Thus implementation is straightforward.
But wait: In the greedy, after we heal, we don't check if monster <= a1 before the monster attack? The monster attack always happens if monster is alive after Vova's action. So after heal, monster is alive, so it attacks. So we correctly update cur = cur + c1 - a2.
Now, are there any scenarios where after a strike (non-final) and monster attack, cur becomes <=0? That would be death, which our algorithm avoids because we only strike if cur > a2 (so after attack cur >0). So safe.
Thus the algorithm is correct.
Now output: first line number of actions n, then each action.
We should test with given examples.
Example 1: h1=10, a1=6, c1=100, h2=17, a2=5. Simulate:
cur=10, monster=17>6, cur>5? yes => strike: monster=11, cur=5.
cur=5, monster=11>6, cur>5? 5>5? false => heal: cur=5+100-5=100.
cur=100, monster=11>6, cur>5? yes => strike: monster=5, cur=95.
cur=95, monster=5<=6? yes => strike: kill. Actions: STRIKE, HEAL, STRIKE, STRIKE. Output 4 and these lines. matches.
Example 2: h1=11, a1=6, c1=100, h2=12, a2=5.
cur=11, monster=12>6, cur>5? yes => strike: monster=6, cur=6.
cur=6, monster=6<=6? yes => strike: kill. Actions: STRIKE, STRIKE. Output 2. matches.
Now test a scenario where initial health is low and must heal first.
h1=1, a1=10, c1=6, a2=5, h2=20. d=1. k = ceil(20/10)=2. non-final strikes =1. req for j=1: j*a2=5, h1=1 => need S > (5-1)/1=4 => S>=5. So H_min=5. So total phases = 7? Actually k=2 strikes, heals=5 => total 7.
Simulate greedy:
cur=1, monster=20>10? yes (20>10). cur>5? 1>5 false, so heal: cur=1+6-5=2.
cur=2, monster>10, cur>5? false, heal: cur=3.
cur=3, heal: cur=4.
cur=4, heal: cur=5.
cur=5, cur>5? false (needs >5), heal: cur=6.
cur=6, cur>5 true, strike: monster=10, cur=6-5=1.
Now monster=10 <= a1? 10<=10, yes, strike: kill. Actions: 5 heals, 2 strikes =7. Good.
Now could there be a scenario where we might want to strike even when cur <= a2 if monster <= a1? Already handled.
Thus solution is simple.
But wait, the problem statement: "It is possible that the battle ends in a middle of a phase after Vova's attack." That's exactly what we do.
Now one more thought: Could there be a scenario where it's beneficial to heal even when cur > a2, if that allows us to finish in fewer phases? For instance, if by healing we can increase health enough to survive multiple strikes without needing to heal later, but we already have enough health to survive next strike. Healing now would add a turn, increasing total phases. So not beneficial.
Thus greedy is optimal.
Now let's think about correctness proof: The greedy algorithm yields minimal number of phases. Proof sketch: Let k = minimal number of strikes needed. Any strategy must contain at least k strikes. The only way to reduce total phases is to minimize heals. The greedy algorithm ensures that before each strike (except possibly the final), the health is just enough to survive the next monster attack, and it never heals unless necessary. This results in the minimal number of heals, as shown by the req analysis. Therefore total phases = k + H_min is minimal.
Implementation details: Since we are to output the strategy, we can just simulate.
Edge Cases: If h2 == 0? Not possible since h2 >=1. So always at least one strike.
Now consider scenario where a1 is so high that one strike kills monster initially: then we just output 1 STRIKE.
If a1 is low and we need many strikes, simulation may take many steps but within limits.
Now we must ensure the condition for strike when monster > a1 but cur > a2 is correct. What if cur > a2 but after strike, monster health becomes >0 but maybe exactly such that next phase we could kill? That's fine.
Now about reading input: three ints then two ints.
Now writing code: We'll implement while monster > 0: if monster <= a1: actions.append("STRIKE") break. else if cur > a2: actions.append("STRIKE"); monster -= a1; cur -= a2. else: actions.append("HEAL"); cur += c1 - a2.
But note: after heal, we did not decrease monster. Also after heal, we did not check if monster <= a1? Actually after heal, monster unchanged, so still > a1? Possibly, but we go to next iteration.
One nuance: after a strike that reduces monster, it's possible that monster becomes <= a1, but we still have to endure the monster attack? Wait, in our simulation, when we decide to strike with condition cur > a2 (and monster > a1), we then subtract a1 from monster and then subtract a2 from cur. But what if after subtracting a1, monster becomes <= a1? That would mean monster is still alive? Actually monster becomes <= a1 but >0? For example, monster= a1+1, after strike becomes 1, which is <= a1. But in the same phase, after Vova's attack, the monster attacks (since it's still alive). So we correctly subtract a2. In the next phase, monster is <= a1, so we will kill without taking damage. That's correct. So simulation is correct.
But is there any scenario where after a strike, monster becomes 0 or negative? That would happen if monster <= a1 before strike, which we already handled by separate condition. So not in this branch.
Thus algorithm stands.
Now we need to produce output. Since the number of actions may be up to ~10000, we can store strings in a list and output.
Now we should double-check with random tests using a brute-force DP for small values to ensure greedy indeed yields minimal phases. Let's do mental reasoning for all small combos up to maybe 5 each. But I'm confident.
One more nuance: The problem statement says "It is possible that the battle ends in a middle of a phase after Vova's attack." This means if Vova attacks and monster health drops to 0 or lower, the battle ends immediately, and monster does not attack. In our simulation, we have a separate branch for monster <= a1 where we attack and break. However, what about the case where monster > a1 but after our attack monster health becomes <=0? Is that possible? As argued, if monster > a1, then monster - a1 >= 1, so >0. So cannot become <=0. So fine.
Now, one more subtlety: What if a1 is very small such that monster > a1 but monster - a1 could be 0? Example: monster = a1 exactly? Then monster <= a1. So branch covers it. So fine.
Thus algorithm correct.
Now we will implement.
But wait, there is another scenario: when cur > a2 but after strike, cur - a2 might become <=0? That would require cur <= a2, which is excluded. So safe.
Thus solution is trivial.
Now we need to output the actions. The problem requires "STRIKE" and "HEAL" in uppercase.
Now, I'll produce final answer.
But before finalizing, let's test with some random values manually computed to ensure no off-by-one.
Test: h1=5, a1=1, c1=3, a2=2 (c1>2). d=1. h2=3. k = ceil(3/1)=3. non-final strikes=2. Compute req: j=1: j*a2=2, h1=5 => 2-5=-3<0 req1=0. j=2: 4-5=-1<0 req2=0. So H_min=0. So total phases = 3. Simulate:
cur=5, monster=3>1? yes, cur>2? 5>2 yes, strike: monster=2, cur=5-2=3.
cur=3, monster=2>1? yes, cur>2? 3>2 yes, strike: monster=1, cur=3-2=1.
cur=1, monster=1<=1? yes, strike: kill. Actions: S,S,S. Total 3. No heals. Works.
Now test: h1=2, a1=1, c1=3, a2=2, h2=3. d=1. k=3, non-final strikes=2. req1: j=1 => 2-2=0? Actually j*a2=2, h1=2 => 2-2=0, need S >0 => req1=1. req2: j=2 => 4-2=2, need S >2 => req2=3? Actually (4-2)=2, need S >2 => S>=3. So H_min=3. total phases = 6. Simulate greedy:
cur=2, monster=3>1, cur>2? 2>2? false, heal: cur=2+3-2=3.
cur=3, monster=3>1, cur>2? 3>2 yes, strike: monster=2, cur=3-2=1.
cur=1, monster=2>1, cur>2? 1>2 false, heal: cur=1+1=2? Actually heal net +1 => cur=2.
cur=2, monster=2>1, cur>2? 2>2 false, heal: cur=3.
cur=3, monster=2>1, cur>2? 3>2, strike: monster=1, cur=3-2=1.
cur=1, monster=1<=1, strike: kill. Actions: H,S,H,H,S,S. That's 4 heals? Wait count: first heal, then strike, then heal, then heal, then strike, then strike = 3 heals? Actually: H (1), S (2), H (3), H (4), S (5), S (6). So heals =3. That's H_min=3. So works.
Now, what about scenario where after a heal, cur becomes > a2 and we strike, but maybe we could have struck earlier? But we healed because cur was <= a2. So fine.
Now, consider scenario where c1 is much larger than a2, so d is large. Then healing once might boost health significantly, maybe making it possible to do many strikes without further heals. Greedy will heal only when needed, and after healing, cur becomes large, then it might do many strikes before needing heal again. That should be optimal.
Now, what about the possibility that after some strikes, monster health becomes low enough that we can kill with one strike, but our health is low, and we might be forced to heal even though we could just strike to kill? That's handled by checking monster <= a1 first: if monster <= a1, we strike regardless of cur. So that avoids unnecessary heals at the end.
Thus algorithm is correct.
Now we must consider the output format: first line n, then n lines. If multiple optimal, any is fine.
Now let's code.
But one more check: In the heal action, we increase cur by c1 - a2. However, is it possible that after heal, we should check if monster <= a1 before monster attacks? No, because monster attack occurs after Vova's action regardless of whether the monster could be killed? Actually the description: "Then, if the battle is not over yet, the Modcrab attacks Vova." So after Vova's action (heal), we check if battle is over. Could it be over? Only if Vova's health <=0? That can't happen after heal. Monster health unchanged, so battle not over. So monster attacks. So correct.
Now, about the attack action: after Vova attacks, we check if battle is over (monster health <=0). If so, battle ends and monster does not attack. In our simulation, for the branch where we attack with monster > a1, we assumed monster still alive and thus subtract a2. But is it possible that monster > a1 yet after subtracting a1, monster becomes <=0? As argued, no. Because if monster > a1, then monster - a1 >= 1. So it's >0. So safe.
But what if a1 is zero? a1 >=1 per constraints. So fine.
Now, about the branch where monster <= a1, we attack and break without subtracting a2. That's correct.
Now, the loop condition: while monster > 0: but after we strike and kill, we break, so monster becomes <=0 and loop ends.
Now, about storing actions: we should store as we go.
Now, complexity: at most about 10000 iterations, fine.
Thus solution.
But we can also compute directly the minimal heals and produce sequence without simulation? But simulation is simpler.
Now, just to be thorough, we should test with maximum values to ensure no infinite loop. For instance, h1=100, a1=1, c1=100, a2=99, h2=100. d=1. k=100. non-final strikes=99. req_{99}=? h1=100, j=99 => need 100 + S > 9999 = 9801 => S > 9701 => S=9702? Let's compute: 9999=9801, h1=100, so need S > (9801-100)=9701 => S>=9702. So total heals=9702, total phases = 9802. That's about 10k, still fine. Simulating 9802 steps is okay. Input output size maybe 10k lines, acceptable.
But Codeforces problem likely expects simulation.
Now, there is a known problem "Modcrab" from Codeforces Round # something. I recall the solution is exactly this greedy.
Thus final answer: implement as described.
Now I'll produce code.
But wait: Should we consider the possibility that after a heal, cur becomes huge, but we might still need to heal again if after many strikes cur drops below a2? That's fine.
Now, implement in Python:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data: return
h1, a1, c1 = map(int, data[:3])
h2, a2 = map(int, data[3:5])
cur = h1
mon = h2
actions = []
while mon > 0:
if mon <= a1:
actions.append("STRIKE")
break
else:
if cur > a2:
actions.append("STRIKE")
mon -= a1
cur -= a2
else:
actions.append("HEAL")
cur += c1 - a2
print(len(actions))
print("\n".join(actions))
That's it.
But ensure that after heal, we don't need to check if cur is positive (it is). Also ensure that after strike and cur decrease, cur may become <=0? Only if cur <= a2 before, which we avoided.
Now test with the examples.
We'll run mental:
Example1: actions length 4, prints.
Now potential pitfalls: The input might have extra spaces, but reading all is fine.
Now, output format: each action on its own line.
Now, edge case: If h2 is 0? Not possible.
Now, final code.
But before final, we can consider an alternative: Some solutions compute required heals directly and then produce pattern. But not needed.
Thus answer.
But wait: There's a nuance about the condition "cur > a2". In the problem, health drops to 0 or lower. So if cur == a2, and we attack (non-killing), after monster attack, health becomes 0 => dead. So we need cur > a2. So condition is correct.
Now, what about the condition "if monster <= a1"? If monster == a1, attacking kills, no monster attack. Good.
Now, what about the heal: after heal, monster attacks; if cur + c1 - a2 becomes <=0? Since c1 > a2, cur + c1 - a2 >= cur + 1 > 0. So safe.
Now, we are done.
But wait: The problem statement says "It is guaranteed that c1 > a2." So good.
Thus final.
Now, I'll produce final answer with explanation. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
h1, a1, c1 = map(int, data[:3])
h2, a2 = map(int, data[3:5])
cur_h = h1 # Vova's current health
mon_h = h2 # Monster's current health
actions = []
while mon_h > 0:
# If one strike kills the monster, do it immediately.
if mon_h <= a1:
actions.append("STRIKE")
break
# Otherwise, the monster will survive the strike and then attack.
if cur_h > a2: # safe to strike
actions.append("STRIKE")
mon_h -= a1
cur_h -= a2 # monster counterattacks
else: # must heal
actions.append("HEAL")
cur_h += c1 - a2 # heal then monster attack (net gain)
# Output
print(len(actions))
print("\n".join(actions))
if name == "main":
solve()