using System; using System.Collections.Generic; using System.Text; using System.Linq; class Program { public void Proc() { Reader.IsDebug = false; int[] inpt = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray(); this.MuraCount = inpt[0]; this.TimeLimit = inpt[1]; this.Zeisyu = new long[this.MuraCount + 1]; for (int i = 0; i < MuraCount; i++) { this.Zeisyu[i] = long.Parse(Reader.ReadLine()); } for (int i = 0; i < MuraCount - 1; i++) { inpt = Reader.ReadLine().Split(' ').Select(a=>int.Parse(a)).ToArray(); int m1 = inpt[0]; int m2 = inpt[1]; int cost = inpt[2]; if (!this.Road.ContainsKey(m1)) { this.Road.Add(m1, new Dictionary()); } this.Road[m1][m2] = cost; if(!this.Road.ContainsKey(m2)) { this.Road.Add(m2, new Dictionary()); } this.Road[m2][m1] = cost; } bool[] flags = new bool[MuraCount]; flags[0] = true; long ans = Zeisyu[0]; ans = Math.Max(ans, this.GetAns(0, TimeLimit, flags) + Zeisyu[0]); Console.WriteLine(ans); } private Dictionary>> dic = new Dictionary>>(); private long GetAns(int pos, int remainTime, bool[] flags) { string key = string.Join(string.Empty, flags.Select(a => a ? 1 : 0)); if (!dic.ContainsKey(pos)) { dic.Add(pos, new Dictionary>()); } if (!dic[pos].ContainsKey(remainTime)) { dic[pos].Add(remainTime, new Dictionary()); } if (dic[pos][remainTime].ContainsKey(key)) { return dic[pos][remainTime][key]; } if (remainTime < 0) { return -1; } if (remainTime == 0) { if (pos == 0) { return 0; } else { return -1; } } long ans = -1; if (pos == 0) { ans = 0; } if (dic.ContainsKey(pos)) { bool canGetTax = !flags[pos]; flags[pos] = true; foreach (int nextPos in Road[pos].Keys) { long ret = this.GetAns(nextPos, remainTime - Road[pos][nextPos], flags); if (ret >= 0) { if (canGetTax) { ret += Zeisyu[pos]; } ans = Math.Max(ans, ret); } } if (canGetTax) { flags[pos] = false; } } dic[pos][remainTime].Add(key, ans); return ans; } private Dictionary> Road = new Dictionary>(); private int MuraCount; private int TimeLimit; long[] Zeisyu; public class Reader { public static bool IsDebug = true; private static String PlainInput = @" 2 50 10 50 0 1 50 "; private static System.IO.StringReader Sr = null; public static string ReadLine() { if (IsDebug) { if (Sr == null) { Sr = new System.IO.StringReader(PlainInput.Trim()); } return Sr.ReadLine(); } else { return Console.ReadLine(); } } } static void Main() { Program prg = new Program(); prg.Proc(); } }