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: Runtime Error)

using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;

public class Program
{

    public void Proc() {
        int inputCount = int.Parse(Reader.ReadLine());
        long ans = 0;
        List<int[]> list = new List<int[]>();
        for (int i = 0; i < inputCount; i++) {
            list.Add(Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray());
        }
        ans = GetAns(list);
        Console.WriteLine(ans);
    }

    private Dictionary<string, long> dic = new Dictionary<string, long>();
    private long GetAns(List<int[]> src) {
        string key = string.Join("#", src.Select(a => a[0] + "#" + a[1]).ToArray());
        if(dic.ContainsKey(key)) {
            return dic[key];
        }

        long ans = long.MaxValue;
        for (int i = 1; i < src.Count; i++) {
            long tmp = 1;
            foreach(int num in src[i-1].Union(src[i])) {
                tmp *= num;
            }
            List<int[]> subList = new List<int[]>();
            subList.AddRange(src);
            subList.RemoveAt(i);
            subList.RemoveAt(i-1);
            if(subList.Count > 0) {
				int remNum = src[i - 1].Where(a => src[i].Any(b => b == a)).First();
                int num1 = src[i - 1].First((a => a != remNum));
                int num2 = src[i].First(a => a != remNum);
                subList.Insert(i-1, new int[]{num1,num2}.OrderBy(a=>a).ToArray());
                tmp += GetAns(subList);
			}
            ans = Math.Min(ans, tmp);
        }
        if(ans == long.MaxValue) {
            ans = 0;
        }
        dic[key] = ans;
        return ans;
    }


    public class Reader
    {
        private static StringReader sr;
        public static bool IsDebug = false;
        public static string ReadLine() {
            if(IsDebug) {
                if(sr == null) {
                    sr = new StringReader(InputText.Trim());
                }
                return sr.ReadLine();
            } else {
                return Console.ReadLine();
            }
        }
        private static string InputText = @"


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



";
    }

    public static void Main(string[] args)
    {
#if DEBUG
        Reader.IsDebug = true;
#endif
        Program prg = new Program();
        prg.Proc();
	}
}