Is this question uses DP?

my code is giving correct answer not for higher values of edges n node why??

how to optimise this code???