1 #include<cstdio>
2 #include<algorithm>
3 #include<cstring>
4
5 using namespace std;
6
7 const int N = 110;
8
9 struct Edge{
10 int to,nxt,w;
11 }e[10010];
12 int head[10010],tot;
13
14 int dp[N][N];
15 int Sum,n,m;
16
17 inline int read() {
18 int x = 0,f = 1;char ch = getchar ();
19 for (; ch<‘0‘||ch>‘9‘; ch = getchar())
20 if (ch==‘-‘) f = -1;
21 for (; ch>=‘0‘&ch<=‘9‘; ch = getchar())
22 x = x*10+ch-‘0‘;
23 return x*f;
24 }
25
26 inline void add_edge(int u,int v,int w) {
27 e[++tot].to = v,e[tot].w = w,e[tot].nxt = head[u],head[u] = tot;
28 }
29
30 void dfs(int u,int fa) {
31 for (int i=head[u]; i; i=e[i].nxt) {
32 int v = e[i].to,w = e[i].w;
33 if (v==fa) continue;
34 dfs(v,u);
35 for (int j=m; j>=1; --j)
36 for (int k=0; k<j; ++k)
37 dp[u][j] = max(dp[u][j],dp[v][k]+dp[u][j-k-1]+w);
38 }
39 }
40 int main() {
41
42 n = read(),m = read();
43 for (int a,b,c,i=1; i<n; ++i) {
44 a = read(),b = read(),c = read();
45 add_edge(a,b,c),add_edge(b,a,c);
46 }
47
48 dfs(1,0);
49 printf("%d",dp[1][m]);
50
51 return 0;
52 }