Improve the DP to be O(n^3) from O(n^4) by removing a unnecessary loop.