import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { static List> list; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); list = new ArrayList<>(n); for (int i = 0; i < n; i++) { list.add(new ArrayList<>()); } Hen[] arr = new Hen[n - 1]; for (int i = 0; i < n - 1; i++) { Hen h = new Hen(); h.a = sc.nextInt() - 1; h.b = sc.nextInt() - 1; h.c = sc.nextInt(); list.get(h.a).add(h); list.get(h.b).add(h); arr[i] = h; } sc.close(); dfs(0, -1); long[][] dp = new long[n][k + 1]; for (int i = 0; i < n - 1; i++) { for (int j = 0; j <= k; j++) { dp[i + 1][j] = dp[i][j]; } for (int j = 0; j <= k; j++) { int j2 = j + arr[i].c; if (j2 <= k) { dp[i + 1][j2] = Math.max(dp[i + 1][j2], dp[i][j] + arr[i].c * arr[i].d); } else { break; } } } long sum = 0; for (int i = 0; i < n - 1; i++) { sum += arr[i].c * arr[i].d; } long max = 0; for (int i = 0; i <= k; i++) { max = Math.max(max, dp[n - 1][i]); } System.out.println(sum + max); } static class Hen { int a, b, c, d; } static int dfs(int x, int p) { int ret = 0; if (list.get(x).size() == 1) { ret = 1; } for (Hen h : list.get(x)) { int nx = h.a; if (nx == x) { nx = h.b; } if (nx != p) { h.d = dfs(nx, x); ret += h.d; } } return ret; } }