Solution
Energy[N][M]) - The energy needed to kill each monster.
Conf[N][M] - The associated confidence.
Stren[N] - The minimum energy that you will need to pass each cell.
At the last cell, the needed strength is easily calculated:
Strength[n-1] = min_i Energy[n-1][i]
The next strengths are then iteratively calculated, from back to the top:
Strength[j] = min_i (Energy[j][i], Energy[j][i] - Conf[j][i] + Strength[j+1]) For all j
Final result: Strength[0]
The key point is the recursive formula:
Strength[j] = min_i (Energy[j][i], Energy[j][i] - Conf[j][i] + Strength[j+1])
For Cell j, the strength Strength[j] must fulfill two constraints.
- You have to kill one of the monster in the current Cell.
Strength[j][i] >= Energy[j][i]
After filling monster i, keep enough strength, so that the remaining strength is higher than Strength[j+], in order to pass the next cells.
Remaining strength = Strength[j][i] - Energy[j][i] + Conf[j][i] >= Strength[j+1]
After this, you can select the best one by minimizing the maximum of these two thresholds.
I hope this helps.