Comments

Thanks for the help! Actually I was missing the case

(dp[n][1] + d) + d*(n-1))

when outputting the final answer which I think is required if we have an odd number of levels and we end up taking attack 2 on all of them. This was giving me wrong answer.

I cannot understand why we need the case with odd-length sequence. Since when we have the odd length sequence we should convert it to an even length sequence based on the fact that the cost of one attack >= 2 attack and if we have an odd sequence then we are going to spend 2*d anyway so why not just make it an even length sequence?

Rather than using the logarithms I tried another approach where when computing the probability in linear time I would make sure that it was always less than 1 so that there was never any overflow. But I keep getting wrong answer with this approach. I think this might be due to precision issues but can't understand why

For question F I am not quite sure why we had to run BFS (topological sort) on the graph? It is certainly cyclic. I tried DFS and got stack overflow but I am still not very sure where we got the idea to run the BFS from.