#include<bits/stdc++.h>
using namespace std;
#define jizz ios_base::sync_with_stdio(false);cin.tie(NULL);
typedef long long ll;
typedef pair<ll,ll> pii;
#define F first
#define S second
const int MAXN=2E5+10;
vector<pii> G[MAXN];
ll sum[MAXN], sum2[MAXN], path[MAXN], nxt[MAXN], ans[MAXN];
void init(int x, int f){
for(auto i:G[x]){
if(i.F!=f){
init(i.F,x);
} else{
sum[1]+=i.S;
}
}
}
void getSum(int x, int f, ll s){
ll maxpath=0, secpath=0;
for(auto i:G[x]){
if(i.F==f){
sum[x]=s-i.S;
break;
}
}
for(auto i:G[x]){
if(i.F!=f){
getSum(i.F,x,sum[x]+i.S);
ll cp=path[i.F]+i.S;
if(cp>maxpath){
nxt[x]=i.F;
secpath=maxpath;
maxpath=cp;
} else{
secpath=max(secpath,cp);
}
}
}
path[x]=maxpath;
sum2[x]=sum[x]+maxpath+secpath;
}
void getNewPath(int x, int f){
for(auto i:G[x]){
if(i.F!=f){
getNewPath(i.F,x);
ll cp=path[i.F]+i.S;
if(cp>path[x]){
path[x]=cp;
nxt[x]=i.F;
}
}
}
}
priority_queue<ll> pq;
void getPQ(int x, int f, int t, ll s){
if(x==t) pq.push(s+path[x]);
for(auto i:G[x]){
if(i.F==nxt[x]){
getPQ(i.F,x,t,i.S);
}
else if(i.F!=f){
getPQ(i.F,x,i.F,i.S);
}
}
}
int main(){ jizz
int n;
cin>>n;
ll all=0;
for(int i=0;i<n-1;i++){
int a, b, c, d;
cin>>a>>b>>c>>d;
G[a].push_back({b,c});
G[b].push_back({a,d});
all=all+c+d;
}
init(1,0);
getSum(1,0,sum[1]);
int newroot=0;
for(int i=1;i<=n;i++){
ans[1]=max(ans[1],sum[i]);
if(sum2[i]>ans[2]){
ans[2]=sum2[i];
newroot=i;
}
}
while(nxt[newroot]) newroot=nxt[newroot];
for(int i=1;i<=n;i++){
path[i]=0;
nxt[i]=0;
}
//cout<<"nr "<<newroot<<"\n";
getNewPath(newroot,0);
getPQ(newroot,0,newroot,0);
pq.pop();
for(int i=3;i<=n;i++){
if(!pq.empty()){
ans[i]=ans[i-1]+pq.top();
pq.pop();
} else{
ans[i]=all;
}
}
int q;
cin>>q;
for(int i=0;i<q;i++){
int e;
cin>>e;
cout<<all-ans[e]<<"\n";
}
return 0;
}
/*
6
1 6 6 12
6 2 5 16
1 4 13 4
5 1 19 3
3 1 9 13
3
1
2
3
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5248 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
4 ms |
5100 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
389 ms |
24416 KB |
Output is correct |
3 |
Correct |
401 ms |
42880 KB |
Output is correct |
4 |
Correct |
346 ms |
24412 KB |
Output is correct |
5 |
Correct |
328 ms |
24420 KB |
Output is correct |
6 |
Correct |
364 ms |
27104 KB |
Output is correct |
7 |
Correct |
297 ms |
25180 KB |
Output is correct |
8 |
Correct |
418 ms |
42476 KB |
Output is correct |
9 |
Correct |
228 ms |
26452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
348 ms |
24556 KB |
Output is correct |
3 |
Correct |
418 ms |
44652 KB |
Output is correct |
4 |
Correct |
346 ms |
24676 KB |
Output is correct |
5 |
Correct |
332 ms |
24420 KB |
Output is correct |
6 |
Correct |
393 ms |
27616 KB |
Output is correct |
7 |
Correct |
254 ms |
26264 KB |
Output is correct |
8 |
Correct |
386 ms |
36324 KB |
Output is correct |
9 |
Correct |
250 ms |
26464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5248 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
4 ms |
5100 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
6 ms |
5356 KB |
Output is correct |
14 |
Correct |
8 ms |
5484 KB |
Output is correct |
15 |
Correct |
6 ms |
5356 KB |
Output is correct |
16 |
Correct |
6 ms |
5356 KB |
Output is correct |
17 |
Correct |
6 ms |
5376 KB |
Output is correct |
18 |
Correct |
6 ms |
5356 KB |
Output is correct |
19 |
Correct |
5 ms |
5356 KB |
Output is correct |
20 |
Correct |
6 ms |
5356 KB |
Output is correct |
21 |
Correct |
6 ms |
5356 KB |
Output is correct |
22 |
Correct |
6 ms |
5356 KB |
Output is correct |
23 |
Correct |
6 ms |
5356 KB |
Output is correct |
24 |
Correct |
6 ms |
5356 KB |
Output is correct |
25 |
Correct |
8 ms |
5484 KB |
Output is correct |
26 |
Correct |
6 ms |
5356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
389 ms |
24416 KB |
Output is correct |
3 |
Correct |
401 ms |
42880 KB |
Output is correct |
4 |
Correct |
346 ms |
24412 KB |
Output is correct |
5 |
Correct |
328 ms |
24420 KB |
Output is correct |
6 |
Correct |
364 ms |
27104 KB |
Output is correct |
7 |
Correct |
297 ms |
25180 KB |
Output is correct |
8 |
Correct |
418 ms |
42476 KB |
Output is correct |
9 |
Correct |
228 ms |
26452 KB |
Output is correct |
10 |
Correct |
4 ms |
5100 KB |
Output is correct |
11 |
Correct |
348 ms |
24556 KB |
Output is correct |
12 |
Correct |
418 ms |
44652 KB |
Output is correct |
13 |
Correct |
346 ms |
24676 KB |
Output is correct |
14 |
Correct |
332 ms |
24420 KB |
Output is correct |
15 |
Correct |
393 ms |
27616 KB |
Output is correct |
16 |
Correct |
254 ms |
26264 KB |
Output is correct |
17 |
Correct |
386 ms |
36324 KB |
Output is correct |
18 |
Correct |
250 ms |
26464 KB |
Output is correct |
19 |
Correct |
4 ms |
5100 KB |
Output is correct |
20 |
Correct |
364 ms |
24448 KB |
Output is correct |
21 |
Correct |
420 ms |
50924 KB |
Output is correct |
22 |
Correct |
371 ms |
28904 KB |
Output is correct |
23 |
Correct |
368 ms |
30560 KB |
Output is correct |
24 |
Correct |
350 ms |
29536 KB |
Output is correct |
25 |
Correct |
380 ms |
30492 KB |
Output is correct |
26 |
Correct |
356 ms |
29412 KB |
Output is correct |
27 |
Correct |
354 ms |
30472 KB |
Output is correct |
28 |
Correct |
381 ms |
32996 KB |
Output is correct |
29 |
Correct |
374 ms |
30860 KB |
Output is correct |
30 |
Correct |
328 ms |
29276 KB |
Output is correct |
31 |
Correct |
295 ms |
30428 KB |
Output is correct |
32 |
Correct |
388 ms |
42740 KB |
Output is correct |
33 |
Correct |
226 ms |
32532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5248 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
4 ms |
5100 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
389 ms |
24416 KB |
Output is correct |
14 |
Correct |
401 ms |
42880 KB |
Output is correct |
15 |
Correct |
346 ms |
24412 KB |
Output is correct |
16 |
Correct |
328 ms |
24420 KB |
Output is correct |
17 |
Correct |
364 ms |
27104 KB |
Output is correct |
18 |
Correct |
297 ms |
25180 KB |
Output is correct |
19 |
Correct |
418 ms |
42476 KB |
Output is correct |
20 |
Correct |
228 ms |
26452 KB |
Output is correct |
21 |
Correct |
4 ms |
5100 KB |
Output is correct |
22 |
Correct |
348 ms |
24556 KB |
Output is correct |
23 |
Correct |
418 ms |
44652 KB |
Output is correct |
24 |
Correct |
346 ms |
24676 KB |
Output is correct |
25 |
Correct |
332 ms |
24420 KB |
Output is correct |
26 |
Correct |
393 ms |
27616 KB |
Output is correct |
27 |
Correct |
254 ms |
26264 KB |
Output is correct |
28 |
Correct |
386 ms |
36324 KB |
Output is correct |
29 |
Correct |
250 ms |
26464 KB |
Output is correct |
30 |
Correct |
4 ms |
5100 KB |
Output is correct |
31 |
Correct |
6 ms |
5356 KB |
Output is correct |
32 |
Correct |
8 ms |
5484 KB |
Output is correct |
33 |
Correct |
6 ms |
5356 KB |
Output is correct |
34 |
Correct |
6 ms |
5356 KB |
Output is correct |
35 |
Correct |
6 ms |
5376 KB |
Output is correct |
36 |
Correct |
6 ms |
5356 KB |
Output is correct |
37 |
Correct |
5 ms |
5356 KB |
Output is correct |
38 |
Correct |
6 ms |
5356 KB |
Output is correct |
39 |
Correct |
6 ms |
5356 KB |
Output is correct |
40 |
Correct |
6 ms |
5356 KB |
Output is correct |
41 |
Correct |
6 ms |
5356 KB |
Output is correct |
42 |
Correct |
6 ms |
5356 KB |
Output is correct |
43 |
Correct |
8 ms |
5484 KB |
Output is correct |
44 |
Correct |
6 ms |
5356 KB |
Output is correct |
45 |
Correct |
4 ms |
5100 KB |
Output is correct |
46 |
Correct |
364 ms |
24448 KB |
Output is correct |
47 |
Correct |
420 ms |
50924 KB |
Output is correct |
48 |
Correct |
371 ms |
28904 KB |
Output is correct |
49 |
Correct |
368 ms |
30560 KB |
Output is correct |
50 |
Correct |
350 ms |
29536 KB |
Output is correct |
51 |
Correct |
380 ms |
30492 KB |
Output is correct |
52 |
Correct |
356 ms |
29412 KB |
Output is correct |
53 |
Correct |
354 ms |
30472 KB |
Output is correct |
54 |
Correct |
381 ms |
32996 KB |
Output is correct |
55 |
Correct |
374 ms |
30860 KB |
Output is correct |
56 |
Correct |
328 ms |
29276 KB |
Output is correct |
57 |
Correct |
295 ms |
30428 KB |
Output is correct |
58 |
Correct |
388 ms |
42740 KB |
Output is correct |
59 |
Correct |
226 ms |
32532 KB |
Output is correct |
60 |
Correct |
4 ms |
5100 KB |
Output is correct |
61 |
Correct |
396 ms |
32916 KB |
Output is correct |
62 |
Correct |
478 ms |
50156 KB |
Output is correct |
63 |
Correct |
375 ms |
31592 KB |
Output is correct |
64 |
Correct |
420 ms |
33248 KB |
Output is correct |
65 |
Correct |
391 ms |
31840 KB |
Output is correct |
66 |
Correct |
377 ms |
33248 KB |
Output is correct |
67 |
Correct |
376 ms |
31844 KB |
Output is correct |
68 |
Correct |
372 ms |
33116 KB |
Output is correct |
69 |
Correct |
435 ms |
35684 KB |
Output is correct |
70 |
Correct |
410 ms |
33632 KB |
Output is correct |
71 |
Correct |
372 ms |
32096 KB |
Output is correct |
72 |
Correct |
334 ms |
33504 KB |
Output is correct |
73 |
Correct |
442 ms |
45048 KB |
Output is correct |
74 |
Correct |
265 ms |
36728 KB |
Output is correct |