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: Wrong Answer)

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) {
                if(src[i-1].Union(src[i]).Count()>2) {
					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());
                } else {
                    subList.Insert(i-1, src[i-1].Union(src[i]).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 = @"


12
1 34
34 44
44 13
13 30
30 1
1 9
9 3
3 7
7 7
7 20
20 12
12 2


";
    }

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