James58899 says to YSITD
用dp之類的吧