#include<bits/stdc++.h>
using namespace std;
long long a,b,c,d,e,i,j,ii,jj,zx,xc,K,f[100009],g[100009],DP[100009],dep[100009],msh[100009],msh2[100009],pas;
vector <pair <long long, long long> > v[100009];
void dfsst(long long q, long long w){
msh[q]=w;
for(vector <pair <long long, long long> >::iterator it=v[q].begin(); it!=v[q].end(); it++){
if((*it).first==w) continue;
dep[(*it).first]=dep[q]+(*it).second;
msh2[(*it).first]=(*it).second;
dfsst((*it).first,q);
}
}
void dfs(long long q, long long w){
DP[q]=q;
for(vector <pair <long long, long long> >::iterator it=v[q].begin(); it!=v[q].end(); it++){
if((*it).first==w) continue;
dfs((*it).first,q);
g[(*it).first]=DP[(*it).first];
if(dep[DP[q]]<dep[DP[(*it).first]]) DP[q]=DP[(*it).first];
}
}
int main(){
/*freopen("paths.in","r",stdin);
freopen("paths.out","w",stdout);*/
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>a>>K;
for(i=1; i<a; i++){
cin>>c>>d>>e;
v[c].push_back(make_pair(d,e));
v[d].push_back(make_pair(c,e));
}
for(ii=1; ii<=a; ii++){
for(i=0; i<=a+1; i++){
f[i]=0;
}
dep[ii]=0;msh2[ii]=0;
dfsst(ii,0);
dfs(ii,0);
for(i=1; i<=a; i++){
f[g[i]]+=msh2[i];
}
sort(f+1,f+a+1);
pas=0;
for(i=a; i>=a-K+1; i--){
pas+=f[i];
}
cout<<pas<<"\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
3 ms |
2764 KB |
Output is correct |
4 |
Correct |
3 ms |
2636 KB |
Output is correct |
5 |
Correct |
3 ms |
2636 KB |
Output is correct |
6 |
Correct |
3 ms |
2636 KB |
Output is correct |
7 |
Correct |
3 ms |
2636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
3 ms |
2764 KB |
Output is correct |
4 |
Correct |
3 ms |
2636 KB |
Output is correct |
5 |
Correct |
3 ms |
2636 KB |
Output is correct |
6 |
Correct |
3 ms |
2636 KB |
Output is correct |
7 |
Correct |
3 ms |
2636 KB |
Output is correct |
8 |
Correct |
62 ms |
2784 KB |
Output is correct |
9 |
Correct |
58 ms |
2828 KB |
Output is correct |
10 |
Correct |
52 ms |
2916 KB |
Output is correct |
11 |
Correct |
58 ms |
2784 KB |
Output is correct |
12 |
Correct |
52 ms |
2764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
3 ms |
2764 KB |
Output is correct |
4 |
Correct |
3 ms |
2636 KB |
Output is correct |
5 |
Correct |
3 ms |
2636 KB |
Output is correct |
6 |
Correct |
3 ms |
2636 KB |
Output is correct |
7 |
Correct |
3 ms |
2636 KB |
Output is correct |
8 |
Correct |
62 ms |
2784 KB |
Output is correct |
9 |
Correct |
58 ms |
2828 KB |
Output is correct |
10 |
Correct |
52 ms |
2916 KB |
Output is correct |
11 |
Correct |
58 ms |
2784 KB |
Output is correct |
12 |
Correct |
52 ms |
2764 KB |
Output is correct |
13 |
Correct |
279 ms |
2892 KB |
Output is correct |
14 |
Correct |
289 ms |
2956 KB |
Output is correct |
15 |
Correct |
215 ms |
2920 KB |
Output is correct |
16 |
Correct |
265 ms |
2876 KB |
Output is correct |
17 |
Correct |
239 ms |
2892 KB |
Output is correct |
18 |
Correct |
149 ms |
2836 KB |
Output is correct |
19 |
Correct |
281 ms |
2884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1092 ms |
12360 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
3 ms |
2764 KB |
Output is correct |
4 |
Correct |
3 ms |
2636 KB |
Output is correct |
5 |
Correct |
3 ms |
2636 KB |
Output is correct |
6 |
Correct |
3 ms |
2636 KB |
Output is correct |
7 |
Correct |
3 ms |
2636 KB |
Output is correct |
8 |
Correct |
62 ms |
2784 KB |
Output is correct |
9 |
Correct |
58 ms |
2828 KB |
Output is correct |
10 |
Correct |
52 ms |
2916 KB |
Output is correct |
11 |
Correct |
58 ms |
2784 KB |
Output is correct |
12 |
Correct |
52 ms |
2764 KB |
Output is correct |
13 |
Correct |
279 ms |
2892 KB |
Output is correct |
14 |
Correct |
289 ms |
2956 KB |
Output is correct |
15 |
Correct |
215 ms |
2920 KB |
Output is correct |
16 |
Correct |
265 ms |
2876 KB |
Output is correct |
17 |
Correct |
239 ms |
2892 KB |
Output is correct |
18 |
Correct |
149 ms |
2836 KB |
Output is correct |
19 |
Correct |
281 ms |
2884 KB |
Output is correct |
20 |
Execution timed out |
1092 ms |
12360 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |