#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int c=1e5+5;
vector<pair<int,ll>> adj[c];
vector<ll> ut;
ll dfs(int v, int p, ll len){
ll maxi=0;
for(pair<int,ll> x: adj[v]){
if(x.first != p){
ll cur=dfs(x.first,v,x.second);
if(cur>maxi){
swap(cur,maxi);
}
ut.push_back(cur);
}
}
return maxi+len;
}
int n,k;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>k;
for(int i=1;i<n;i++){
int a,b,val;
cin>>a>>b>>val;
adj[a].push_back(make_pair(b,val));
adj[b].push_back(make_pair(a,val));
}
for(int i=1;i<=n;i++){
ut.clear();
ll ans=0;
ut.push_back(dfs(i,0,0));
sort(ut.rbegin(),ut.rend());
for(int i=0;i<min(k,(int)ut.size());i++) ans+=ut[i];
cout << ans << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2672 KB |
Output is correct |
3 |
Correct |
3 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
3 ms |
2688 KB |
Output is correct |
6 |
Correct |
3 ms |
2644 KB |
Output is correct |
7 |
Correct |
3 ms |
2668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2672 KB |
Output is correct |
3 |
Correct |
3 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
3 ms |
2688 KB |
Output is correct |
6 |
Correct |
3 ms |
2644 KB |
Output is correct |
7 |
Correct |
3 ms |
2668 KB |
Output is correct |
8 |
Correct |
45 ms |
2740 KB |
Output is correct |
9 |
Correct |
39 ms |
2812 KB |
Output is correct |
10 |
Correct |
29 ms |
2776 KB |
Output is correct |
11 |
Correct |
45 ms |
2872 KB |
Output is correct |
12 |
Correct |
33 ms |
2680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2672 KB |
Output is correct |
3 |
Correct |
3 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
3 ms |
2688 KB |
Output is correct |
6 |
Correct |
3 ms |
2644 KB |
Output is correct |
7 |
Correct |
3 ms |
2668 KB |
Output is correct |
8 |
Correct |
45 ms |
2740 KB |
Output is correct |
9 |
Correct |
39 ms |
2812 KB |
Output is correct |
10 |
Correct |
29 ms |
2776 KB |
Output is correct |
11 |
Correct |
45 ms |
2872 KB |
Output is correct |
12 |
Correct |
33 ms |
2680 KB |
Output is correct |
13 |
Correct |
195 ms |
2852 KB |
Output is correct |
14 |
Correct |
168 ms |
2856 KB |
Output is correct |
15 |
Correct |
113 ms |
2896 KB |
Output is correct |
16 |
Correct |
194 ms |
2904 KB |
Output is correct |
17 |
Correct |
143 ms |
2852 KB |
Output is correct |
18 |
Correct |
101 ms |
2772 KB |
Output is correct |
19 |
Correct |
191 ms |
2844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1072 ms |
10952 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2672 KB |
Output is correct |
3 |
Correct |
3 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
3 ms |
2688 KB |
Output is correct |
6 |
Correct |
3 ms |
2644 KB |
Output is correct |
7 |
Correct |
3 ms |
2668 KB |
Output is correct |
8 |
Correct |
45 ms |
2740 KB |
Output is correct |
9 |
Correct |
39 ms |
2812 KB |
Output is correct |
10 |
Correct |
29 ms |
2776 KB |
Output is correct |
11 |
Correct |
45 ms |
2872 KB |
Output is correct |
12 |
Correct |
33 ms |
2680 KB |
Output is correct |
13 |
Correct |
195 ms |
2852 KB |
Output is correct |
14 |
Correct |
168 ms |
2856 KB |
Output is correct |
15 |
Correct |
113 ms |
2896 KB |
Output is correct |
16 |
Correct |
194 ms |
2904 KB |
Output is correct |
17 |
Correct |
143 ms |
2852 KB |
Output is correct |
18 |
Correct |
101 ms |
2772 KB |
Output is correct |
19 |
Correct |
191 ms |
2844 KB |
Output is correct |
20 |
Execution timed out |
1072 ms |
10952 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |