using System; using System.Collections.Generic; using System.Linq; class Program { static string InputPattern = "InputX"; static List GetInputList() { var WillReturn = new List(); if (InputPattern == "Input1") { WillReturn.Add("4"); WillReturn.Add("-1 16 15 38"); WillReturn.Add("16 -1 22 17"); WillReturn.Add("15 22 -1 19"); WillReturn.Add("38 17 19 -1"); //60 } else if (InputPattern == "Input2") { WillReturn.Add("6"); WillReturn.Add("-1 14 29 35 39 8"); WillReturn.Add("14 -1 35 41 34 3"); WillReturn.Add("29 35 -1 14 21 21"); WillReturn.Add("35 41 14 -1 12 42"); WillReturn.Add("39 34 21 12 -1 28"); WillReturn.Add("8 3 21 42 28 -1"); //116 } else { string wkStr; while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr); } return WillReturn; } static void Main() { List InputList = GetInputList(); int[] wkArr = { }; Action SplitAct = pStr => wkArr = pStr.Split(' ').Select(X => int.Parse(X)).ToArray(); int N = int.Parse(InputList[0]); var FArrList = new List(); foreach (string EachStr in InputList.Skip(1)) { SplitAct(EachStr); FArrList.Add(wkArr); } int UB = (1 << N) - 1; //相性度合計の最大値[登場した人のBit位置]なDP表 var PrevDP = new Nullable[UB + 1]; PrevDP[0] = 0; //ペアの作成でループ for (int I = 1; I <= N / 2; I++) { var CurrDP = new Nullable[UB + 1]; for (int J = 0; J <= UB; J++) { if (PrevDP[J].HasValue == false) continue; for (int K = 0; K <= N - 1; K++) { //使用済ならContinue if ((J & DeriveBitPos(K)) > 0) continue; for (int L = K + 1; L <= N - 1; L++) { //使用済ならContinue if ((J & DeriveBitPos(L)) > 0) continue; int NewJ = J; NewJ |= DeriveBitPos(K); NewJ |= DeriveBitPos(L); int NewVal = PrevDP[J].Value + FArrList[K][L]; if (CurrDP[NewJ].HasValue && NewVal <= CurrDP[NewJ]) continue; CurrDP[NewJ] = NewVal; } //Breakして、最小の人番号からペアを作成するようにする break; } } PrevDP = CurrDP; } Console.WriteLine(PrevDP[UB]); } //人番号を引数として対応するビット位置を返す static int DeriveBitPos(int pHitoNo) { return 1 << pHitoNo; } }