DP exceed time limit time and space: n square