time O(n^2) space O(n^3)

dp