DP solution to matrix chain multiplication