using System; using System.Text; using System.Linq; using System.Collections.Generic; public class Program { public void Proc() { Reader.IsDebug = false; int[] inpt = Reader.ReadLine().Split(' ').Select(a=>int.Parse(a)).ToArray(); this.Map = new TreeNode[inpt[0]]; this.TimeLimit = inpt[1]; for(int i=0; iint.Parse(a)).ToArray(); int[][] posList = new int[][]{new int[]{inpt[0], inpt[1]}, new int[] {inpt[1], inpt[0]}}; foreach(int[] pos in posList) { if(!this.Road.ContainsKey(pos[0])) { this.Road.Add(pos[0], new Dictionary()); } this.Road[pos[0]][pos[1]] = inpt[2]; } } TreeNode root = this.Map[0]; SetUpTree(root); SetCost(root); Console.WriteLine(root.CostDic.Max(a=>a.Value)); } private void SetCost(TreeNode t) { if(t.ToRootCost * 2 > TimeLimit) { return; } List> dicList = new List>(); foreach(TreeNode sub in t.Items) { SetCost(sub); Dictionary tmp = new Dictionary(); sub.CostDic.ToList().ForEach(a=>tmp.Add(a.Key + (sub.ToParentCost*2), a.Value)); if(tmp.Count > 0) { dicList.Add(tmp); } } this.MargeDic.Clear(); t.CostDic.Add(0, t.Tax); Dictionary marged = this.Marge(dicList, 0); marged.Where(a=>a.Key <= TimeLimit - (t.ToRootCost * 2)).ToList().ForEach(a=>{ if(t.CostDic.ContainsKey(a.Key)) { t.CostDic[a.Key] = Math.Max(t.CostDic[a.Key], a.Value + t.Tax); } else { t.CostDic.Add(a.Key, a.Value + t.Tax); } }); } private Dictionary> MargeDic = new Dictionary>(); private Dictionary Marge(List> list, int idx) { if(MargeDic.ContainsKey(idx)) { return MargeDic[idx]; } if(idx == list.Count-1) { return list[idx]; } if(list.Count <= 0) { return new Dictionary(); } Dictionary ans = new Dictionary(); list[idx].ToList().ForEach(a=>ans.Add(a.Key, a.Value)); Dictionary ret = Marge(list, idx+1); ret.ToList().ForEach(a=>{ if(ans.ContainsKey(a.Key)) { ans[a.Key] = Math.Max(ans[a.Key], a.Value); } else { ans.Add(a.Key, a.Value); } list[idx].ToList().ForEach(b=>{ int newKey = a.Key + b.Key; long newVal = a.Value + b.Value; if(ans.ContainsKey(newKey)) { ans[newKey] = Math.Max(ans[newKey], newVal); } else { ans.Add(newKey, newVal); } }); }); MargeDic[idx] = ans; return ans; } private void SetUpTree(TreeNode t) { foreach(int subTreeId in this.Road[t.ID].Keys) { if(t.Parent != null && subTreeId == t.Parent.ID) { continue; } t.AddItem(this.Map[subTreeId], this.Road[t.ID][subTreeId]); SetUpTree(this.Map[subTreeId]); } } private Dictionary> Road = new Dictionary>(); public int TimeLimit = 0; public TreeNode[] Map; public class TreeNode { public int ID; public long Tax; public int ToRootCost; public int ToParentCost; public TreeNode Parent = null; public List Items = new List(); public Dictionary CostDic = new Dictionary(); public TreeNode(int id, long tax) { this.ID = id; this.Tax = tax; } public void AddItem(TreeNode subTree, int Cost) { subTree.Parent = this; this.Items.Add(subTree); subTree.ToParentCost = Cost; subTree.ToRootCost = this.ToRootCost + Cost; } } public class Reader { public static bool IsDebug = true; private static System.IO.StringReader SReader; private static string InitText = @" "; public static string ReadLine() { if(IsDebug) { if(SReader == null) { SReader = new System.IO.StringReader(InitText.Trim()); } return SReader.ReadLine(); } else { return Console.ReadLine(); } } } public static void Main(string[] args) { Program prg = new Program(); prg.Proc(); } }