#include #include #include using namespace std; typedef long long int lli; const lli maxn = lli(2e5)+5; vector> graph[maxn]; pair farthest(lli node, lli par) { pair res = {0ll, node}; for(auto it: graph[node]) { if(it.first != par) { auto tmp = farthest(it.first, node); tmp.first += it.second; res = max(res, tmp); } } return res; } int main(void) { lli n, k; scanf("%lld%lld", &n, &k); lli edgesum = 0ll; for(lli i = 1;i < n;i++) { lli u, v, c; scanf("%lld%lld%lld", &u, &v, &c); u--, v--; graph[u].push_back({v, c}); graph[v].push_back({u, c}); edgesum += c; } if(k == 1ll) { auto corner = farthest(0ll, -1ll); printf("%lld\n", corner.first); } else printf("%lld\n", edgesum); }