Given a sequence of matrices, find the most efficient way to multiply these matrices together. Decide the ordering to minimize the count of scalar multiplications. Input is an array p where p[i-1] x p[i] represents dimensions of Ai.
Examples
Input:p = [40, 20, 30, 10, 30]
Output:26000
Min multiplications cost is (A1 * (A2 * A3)) * A4 or similar configuration, cost 26000.