dp, answer is correct. but still TLE. still need some optimization.