using System; using System.IO; using System.Linq; using ll = System.Int64; class vinh { static void uia() { int n = uiia.cin(); if(n == 0) return; int k = uiia.cin(); ll[] a = new ll[n + 1]; for(int i = 1; i <= n; i++){ a[i] = uiia.nl(); } int[] head = new int[n + 1]; int[] to = new int[2 * n + 5]; ll[] we = new ll[2 * n + 5]; int[] next = new int[2 * n + 5]; int cd = 1; for(int i = 1; i < n; i++){ int u = uiia.cin(); int v = uiia.cin(); ll w = uiia.nl(); to[cd] = v; we[cd] = w; next[cd] = head[u]; head[u] = cd++; to[cd] = u; we[cd] = w; next[cd] = head[v]; head[v] = cd++; } int[] bgb = new int[n + 1]; int[] de = new int[n + 1]; int dd = 0; int[] q = new int[n + 1]; int h = 0, t = 0; q[t++] = 1; bgb[1] = 0; while(h < t){ int u = q[h++]; de[++dd] = u; for(int e = head[u]; e != 0; e = next[e]){ int v = to[e]; if(v != bgb[u]){ bgb[v] = u; q[t++] = v; } } } ll[][] dp = new ll[n + 1][]; int[] sz = new int[n + 1]; for(int id = n; id >= 1; id--){ int u = de[id]; sz[u] = 1; dp[u] = new ll[2]; dp[u][0] = 0; dp[u][1] = a[u]; for(int e = head[u]; e != 0; e = next[e]){ int v = to[e]; if(v == bgb[u]) continue; int lu = Math.Min(k, sz[u]); int lv = Math.Min(k, sz[v]); int ns = Math.Min(k, sz[u] + sz[v]); ll[] ndp = new ll[ns + 1]; for(int i = 0; i <= ns; i++) ndp[i] = -1000000000000000000L; ll w = we[e]; for(int i = 0; i <= lu; i++){ if(dp[u][i] <= -500000000000000000L) continue; for(int j = 0; j <= lv; j++){ if(i + j > k) break; if(dp[v][j] <= -500000000000000000L) continue; ll c = dp[u][i] + dp[v][j]; if(j > 0 && j < k) c -= w; if(c > ndp[i + j]) ndp[i + j] = c; } } dp[u] = ndp; sz[u] = ns; } } uiia.coutl(dp[1][k]); } static void LOVE(string s) { if(File.Exists(s + ".inp")) { uiia.r = new FileStream(s + ".inp", FileMode.Open, FileAccess.Read); uiia.w = new StreamWriter(new FileStream(s + ".out", FileMode.Create, FileAccess.Write), System.Text.Encoding.ASCII, 65536); } else if(File.Exists("input.txt")) { uiia.r = new FileStream("input.txt", FileMode.Open, FileAccess.Read); } } static void Main() { uiia.init(); LOVE("HIV à?"); int t = 1; while(t-- > 0) uia(); uiia.flush(); } } public static class uiia { public static Stream r; public static StreamWriter w; static byte[] buf = new byte[65536]; static int id = 0, cnt = 0; public static void init() { r = Console.OpenStandardInput(); w = new StreamWriter(Console.OpenStandardOutput(), System.Text.Encoding.ASCII, 65536); } static int Read() { if(id >= cnt) { id = 0; cnt = r.Read(buf, 0, 65536); if(cnt == 0) return -1; } return buf[id++]; } public static string next() { int c = Read(); while(c <= 32) { if(c == -1) return null; c = Read(); } var sb = new System.Text.StringBuilder(); while(c > 32) { sb.Append((char)c); c = Read(); } return sb.ToString(); } public static int cin() =>(int)nl(); public static ll nl() { int c = Read(); while(c <= 32) { if(c == -1) return 0; c = Read(); } bool neg = false; if(c == '-') { neg = true; c = Read(); } ll res = 0; while(c > 32) { res = res * 10 + c - '0'; c = Read(); } return neg ? -res : res; } public static void cout(object o) => w.Write(o); public static void coutl(object o) => w.WriteLine(o); public static void flush() => w.Flush(); }