1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
#include<bits/stdc++.h>
using namespace std;

const int N = 25 * 1000 + 7;
const long long mod = 1e9+7;

long long start;
int n, k, p, a, b, p_max;
int parents[N][20];
int dep[N];
int preprocess[N];
long long val[N];
long long powers[100];
bool odw[N];

vector<pair<int, int> >G[N];
vector<long long>v;

void dfs(int x, int o) {
	parents[x][0] = o;
	for(int i = 1; i < 20; i++)
		parents[x][i] = parents[parents[x][i-1]][i-1];
	dep[x] = dep[o] + 1;
	
	for(int i = 0; i < (int)(G[x].size()); i++)
		if(G[x][i].first != o) {
			val[G[x][i].first] = val[x] + powers[G[x][i].second];
			dfs(G[x][i].first, x);
		}
}

int lca(int a, int b) {
	if(dep[a] > dep[b]) swap(a,b);
	for(int i = 19; i >= 0; i--)
		if(dep[parents[b][i]] >= dep[a])
			b = parents[b][i];
	if(a == b) return a;
	for(int i = 19; i >= 0; i--)
		if(parents[a][i] != parents[b][i]) {
			a = parents[a][i];
			b = parents[b][i];
		}
	return parents[a][0];
}

bool test(long long x) {
	int sure = 0, less = 0;
	for(int i = 0; i < (int)(v.size()); i++) {
		if(v[i] < x || v[i] == x && !sure) less++;
		if(v[i] == x) sure++;
		if(less > k) return false;
	}
	//cout << "less: " << less << "  sure: " << sure << "  x: " << x << "\n\n";
	if((less == k && sure) || less < k) return true;
	return false;
}

void do_it() {
	for(int i = 1; i < n; i++)
		for(int j = i + 1; j <= n; j++) {
			int LCA = lca(i,j);
			long long va = 0;
			if(LCA != 1) va = val[i] - val[LCA] + val[j] - val[LCA];
			else va = val[i] + val[j];
			v.push_back(va);
		}
}

long long bs() {
	do_it();
	long long L = 0, P = 3125e10, S = 0;
	while(L < P) {
		//cout << S << "\n";
		S = (L + P + 1) / 2;
		if(test(S)) L = S;
		else P = S - 1;
	}
	return L;
}

int main() {

    ios_base::sync_with_stdio(0);
    cin >> n >> k;

    long long w = n;
    for(int i = 1; i < 100; i++) {
        powers[i] = w;
        w *= (long long)(n);
    }
	
	//for(int i = 1; i <= 10; i++) cout << powers[i] << " ";
	//cout << "\n";
	
    for(int i = 1; i < n; i++) {
        cin >> a >> b >> p;
        //p_max = max(p_max, p);
        G[a].push_back(make_pair(b,p));
        G[b].push_back(make_pair(a,p));
    }
	
	for(int i = 0; i < 20; i++) parents[1][i] = 1;
	dfs(1,1);
	
	//for(int i = 1; i <= 50; i++) cout << parents[i][0] << " ";
	//cout << "\n";
	
	cout << bs() % mod;
	return 0;
}