Minimum Money Needed problem

I can’t pass all the cases. please correct it.

You are taking i and j as same state i.e. w. Try to do it by taking one state for w.