Hints: Flip Flops
You can kill monsters in any order, but throwing flip flops only increases a monster’s power. Before you kill a monster, you may spend flip flops to raise its $a_i$ (each flip flop adds $1$), but you can never raise it above your current combat power $c$ plus the sum of powers you have already absorbed from killed monsters.
What invariant suggests sorting the monsters by $a_i$ before simulating?
Answer to Hint 2: After sorting, process monsters one by one. Let sum be the total $a$ you have already gained from kills (so your power before the next kill is $c + \text{sum}$). For the next monster with current value $a_i$, you may raise it to any $x \in [a_i,, \min(c+\text{sum},, a_i+k)]$ using $x-a_i$ flip flops, then add $x$ to sum and continue.
Why can you stop the first time $a_i > c + \text{sum}$ (before spending flips on that monster)?
a; maintain sum and remaining k. For each a[i], let upper = c + sum. If a[i] > upper, break. Otherwise set x = min(upper, a[i] + k), subtract x - a[i] from k, add x to sum. Print c + sum.