Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Matrix-chain Multiplication

The goal of the matrix-chain multiplication problem is to find the most efficient way to multiply given $n$ matrices $M_1, M_2, M_3,...,M_n$.

Write a program which reads dimensions of $M_i$, and finds the minimum number of scalar multiplications to compute the maxrix-chain multiplication $M_1M_2...M_n$.

Input

In the first line, an integer $n$ is given. In the following $n$ lines, the dimension of matrix $M_i$ ($i = 1...n$) is given by two integers $r$ and $c$ which respectively represents the number of rows and columns of $M_i$.

Output

Print the minimum number of scalar multiplication in a line.

Constraints

$1 \leq n \leq 100$

$1 \leq r, c \leq 100$

Sample Input 1

6
30 35
35 15
15 5
5 10
10 20
20 25

Sample Output 1

15125

Solution in C# (judge verdict: Accepted)

using System;
using System.Linq;

namespace ALDS1_10_B
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            int[] rowSize = new int[n];
            int[] colSize = new int[n];
            int[,] minMultCount = new int[n, n];
            for(int i = 0; i < n; i++){
                for (int j = 0; j < n; j++)
                {
                    if (i == j)
                    {
                        minMultCount[i, j] = 0;
                    }
                    else
                    {
                        minMultCount[i, j] = -1;
                    }
                }
            }
            for (int i = 0; i < n; i++)
            {
                int[] tmpLine = Console.ReadLine()
                                       .Split(' ')
                                       .Select(s => int.Parse(s))
                                       .ToArray();
                rowSize[i] = tmpLine[0];
                colSize[i] = tmpLine[1];
            }
            Console.WriteLine(Program.GetMinMultCount(rowSize, colSize, minMultCount, 0, n - 1));
            //Console.ReadLine();
        }
        
        private static int GetMinMultCount(int[] rowSize, int[] colSize, int[,] minMultCount, int startIndex, int endIndex){
            if (startIndex > endIndex)
            {
                throw new ArgumentException();
            }
            
            if (minMultCount[startIndex, endIndex] >= 0)
            {
                return minMultCount[startIndex, endIndex];
            }

            int minMultCountTmp = (int)Math.Pow(2, 30);
            for (int i = startIndex; i < endIndex; i++)
            {
                minMultCountTmp = Math.Min(minMultCountTmp
                                           ,Program.GetMinMultCount(rowSize, colSize, minMultCount, startIndex, i)
                                           + Program.GetMinMultCount(rowSize, colSize, minMultCount, i + 1, endIndex)
                                           + rowSize[startIndex] * colSize[i] * colSize[endIndex]);
            }
            
            minMultCount[startIndex, endIndex] = minMultCountTmp;
            return minMultCount[startIndex, endIndex];
        }
    }
}