second solution using quadratic equation
tidy code
solve manage your energy
greedy solution (slow)