optimize the DP searching, but still not pass the time limit