local mmi, mma = math.min, math.max local n, k = io.read("*n", "*n") local edge = {} local parent = {} local childcnt = {} local weight = {} local len = {} local base = 0 for i = 1, n do edge[i] = {} parent[i] = 0 childcnt[i] = 0 weight[i] = 0 len[i] = 0 end for i = 1, n - 1 do local a, b, c = io.read("*n", "*n", "*n") edge[a][b] = c edge[b][a] = c end local tasks = {1} for i = 1, n do local src = tasks[i] for dst, l in pairs(edge[src]) do if parent[src] ~= dst then parent[dst] = src len[dst] = len[src] + l childcnt[src] = childcnt[src] + 1 table.insert(tasks, dst) end end end local edgecost, edgevalue = {}, {} tasks = {} for i = 1, n do if childcnt[i] == 0 then table.insert(tasks, i) weight[i] = 1 base = base + len[i] end end for i = 1, n - 1 do local child = tasks[i] local p = parent[child] table.insert(edgecost, edge[child][p]) table.insert(edgevalue, edge[child][p] * weight[child]) weight[p] = weight[p] + weight[child] childcnt[p] = childcnt[p] - 1 if childcnt[p] == 0 then table.insert(tasks, p) end end local dp = {} for i = 1, k do dp[i] = 0 end for i = 1, n - 1 do local c, v = edgecost[i], edgevalue[i] for j = k - c, 1, -1 do dp[j + c] = mma(dp[j + c], dp[j] + v) end if c <= k then dp[c] = mma(dp[c], v) end end local ret = 0 for i = 1, k do ret = mma(ret, dp[i]) end print(ret + base)