package yukicoder; import java.util.Scanner; import java.util.List; import java.util.ArrayList; public class No1488 { private static class Peek { List edge; List peeks; public Peek() { edge = new ArrayList(); peeks = new ArrayList(); } } private static class Queue { int i; List edge; public Queue(int i) { this.i = i; this.edge = new ArrayList(); } public Queue(int i, List edge, int c) { this.i = i; this.edge = new ArrayList(edge); this.edge.add(c); } } public static void main(String[] args) { Scanner scan = new Scanner(System.in); int N = scan.nextInt(); int K = scan.nextInt(); List v = new ArrayList(); for (int i=0; i < N; i++) { v.add(new Peek()); } int[] weight = new int[N-1]; for (int i=0; i < N-1; i++) { int a = scan.nextInt() - 1; int b = scan.nextInt() - 1; int c = scan.nextInt(); v.get(a).peeks.add(new Integer[] {b, i}); v.get(b).peeks.add(new Integer[] {a, i}); weight[i] = c; } scan.close(); List q = new ArrayList(); q.add(new Queue(0)); int[] e = new int[N-1]; while (q.size() > 0) { Queue qi = q.remove(0); v.get(qi.i).edge = qi.edge; boolean cnt = true; for (Integer[] j : v.get(qi.i).peeks) { if (j[0] == 0 || v.get(j[0]).edge.size() > 0) { continue; } cnt = false; q.add(new Queue(j[0], qi.edge, j[1])); } if (cnt) { for (int j : qi.edge) { e[j]++; } } } int[][] dp = new int[N][K+1]; int val = 0; for (int i=0; i < N-1; i++) { val += weight[i] * e[i]; for (int w = 0; w <= K; w++) { if (w >= weight[i]) { dp[i+1][w] = Math.max(dp[i][w-weight[i]] + weight[i] * e[i], dp[i][w]); } else { dp[i+1][w] = dp[i][w]; } } } System.out.println(dp[N-1][K]+val); } }