N, K = gets.split.map(&:to_i) E = Hash.new { |h, k| h[k] = [] } C = Array.new(N + 1) { Array.new(N + 1, 0) } L = [] (N - 1).times do a, b, c = gets.split.map(&:to_i) if a < b L << [a, b, c] E[a] << [b, c] E[b] << [a, c] else L << [b, a, c] E[a] << [b, c] E[b] << [a, c] end end def dfs(v, parent = -1) val = 0 E[v].each do |u, c| next if u == parent a = dfs(u, v) C[v][u] = C[u][v] = a val += a end [1, val].max end dfs(1) dp = Hash.new(0) dp[0] = L.map { |u, v, c| C[u][v] * c }.sum L.each do |u, v, c| temp = dp.dup dp.each do |k, val| nk = k + c nc = val + C[u][v] * c next if nk > K next if temp[nk] >= nc temp[nk] = nc end dp = temp end pp dp.values.max