#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#include <ext/rope>
using namespace __gnu_cxx;
typedef tree<long long, null_type, less<long long>,
rb_tree_tag, tree_order_statistics_node_update> pbds;
//less_equal for identical elements
#define DEBUG
#ifdef DEBUG
#define debug(...) printf(__VA_ARGS__);
#else
#define debug(...)
#endif
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb emplace_back
#define sz(x) (int)x.size()
#define mnto(x,y) x=min(x,(__typeof__(x))y)
#define mxto(x,y) x=max(x,(__typeof__(x))y)
#define INF 1023456789
#define LINF 1023456789123456789
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<pll> vll;
mt19937 rng(time(0));
int n,m,p[300005],c[300005];
ll a[300005],b[300005];
priority_queue<ll> pq[300005];
void join(int x,int y){ //join x to y
a[x]+=a[y];
b[x]+=b[y];
if(sz(pq[x])<sz(pq[y]))swap(pq[x],pq[y]);
while(!pq[y].empty()){
pq[x].push(pq[y].top());
pq[y].pop();
}
}
void print(int x){
pf("%d: %lld %lld\n",x,a[x],b[x]);
while(!pq[x].empty()){
pf("%lld ",pq[x].top());
pq[0].push(pq[x].top());
pq[x].pop();
}
pf("\n");
while(!pq[0].empty()){
pq[x].push(pq[0].top());
pq[0].pop();
}
}
int main(){
sf("%d%d",&n,&m);
for(int i=2;i<=n+m;++i){
sf("%d%d",&p[i],&c[i]);
}
for(int i=n+m;i>n;--i){ //leaf nodes
pq[i].push(c[i]);
pq[i].push(c[i]);
a[i]=1;b[i]=-c[i];
//print(i);
join(p[i],i);
}
for(int i=n;i>=2;--i){
while(a[i]>1){
--a[i];
b[i]+=pq[i].top();
pq[i].pop();
}
ll p0=pq[i].top();
pq[i].pop();
ll p1=pq[i].top();
pq[i].pop();
pq[i].push(p1+c[i]);
pq[i].push(p0+c[i]);
b[i]-=c[i];
//print(i);
join(p[i],i);
}
while(a[1]){
--a[1];
b[1]+=pq[1].top();
pq[1].pop();
}
pf("%lld\n",b[1]);
}
/*
4 6
1 5
2 5
2 8
3 3
3 2
3 3
2 9
4 4
4 3
*/
Compilation message
fireworks.cpp: In function 'int main()':
fireworks.cpp:71:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | sf("%d%d",&n,&m);
| ^
fireworks.cpp:73:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | sf("%d%d",&p[i],&c[i]);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
9676 KB |
Output is correct |
2 |
Correct |
8 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
7 ms |
9676 KB |
Output is correct |
5 |
Correct |
8 ms |
9676 KB |
Output is correct |
6 |
Correct |
6 ms |
9616 KB |
Output is correct |
7 |
Correct |
6 ms |
9704 KB |
Output is correct |
8 |
Correct |
8 ms |
9676 KB |
Output is correct |
9 |
Correct |
7 ms |
9644 KB |
Output is correct |
10 |
Correct |
7 ms |
9700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
9676 KB |
Output is correct |
2 |
Correct |
7 ms |
9676 KB |
Output is correct |
3 |
Correct |
8 ms |
9640 KB |
Output is correct |
4 |
Correct |
7 ms |
9728 KB |
Output is correct |
5 |
Correct |
7 ms |
9676 KB |
Output is correct |
6 |
Correct |
6 ms |
9704 KB |
Output is correct |
7 |
Correct |
7 ms |
9612 KB |
Output is correct |
8 |
Correct |
8 ms |
9700 KB |
Output is correct |
9 |
Correct |
7 ms |
9676 KB |
Output is correct |
10 |
Correct |
7 ms |
9676 KB |
Output is correct |
11 |
Correct |
7 ms |
9676 KB |
Output is correct |
12 |
Correct |
7 ms |
9704 KB |
Output is correct |
13 |
Correct |
6 ms |
9676 KB |
Output is correct |
14 |
Correct |
6 ms |
9676 KB |
Output is correct |
15 |
Correct |
6 ms |
9676 KB |
Output is correct |
16 |
Correct |
6 ms |
9676 KB |
Output is correct |
17 |
Correct |
6 ms |
9676 KB |
Output is correct |
18 |
Correct |
7 ms |
9704 KB |
Output is correct |
19 |
Correct |
8 ms |
9676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
9676 KB |
Output is correct |
2 |
Correct |
8 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
7 ms |
9676 KB |
Output is correct |
5 |
Correct |
8 ms |
9676 KB |
Output is correct |
6 |
Correct |
6 ms |
9616 KB |
Output is correct |
7 |
Correct |
6 ms |
9704 KB |
Output is correct |
8 |
Correct |
8 ms |
9676 KB |
Output is correct |
9 |
Correct |
7 ms |
9644 KB |
Output is correct |
10 |
Correct |
7 ms |
9700 KB |
Output is correct |
11 |
Correct |
8 ms |
9676 KB |
Output is correct |
12 |
Correct |
7 ms |
9676 KB |
Output is correct |
13 |
Correct |
8 ms |
9640 KB |
Output is correct |
14 |
Correct |
7 ms |
9728 KB |
Output is correct |
15 |
Correct |
7 ms |
9676 KB |
Output is correct |
16 |
Correct |
6 ms |
9704 KB |
Output is correct |
17 |
Correct |
7 ms |
9612 KB |
Output is correct |
18 |
Correct |
8 ms |
9700 KB |
Output is correct |
19 |
Correct |
7 ms |
9676 KB |
Output is correct |
20 |
Correct |
7 ms |
9676 KB |
Output is correct |
21 |
Correct |
7 ms |
9676 KB |
Output is correct |
22 |
Correct |
7 ms |
9704 KB |
Output is correct |
23 |
Correct |
6 ms |
9676 KB |
Output is correct |
24 |
Correct |
6 ms |
9676 KB |
Output is correct |
25 |
Correct |
6 ms |
9676 KB |
Output is correct |
26 |
Correct |
6 ms |
9676 KB |
Output is correct |
27 |
Correct |
6 ms |
9676 KB |
Output is correct |
28 |
Correct |
7 ms |
9704 KB |
Output is correct |
29 |
Correct |
8 ms |
9676 KB |
Output is correct |
30 |
Correct |
8 ms |
9676 KB |
Output is correct |
31 |
Correct |
8 ms |
9676 KB |
Output is correct |
32 |
Correct |
8 ms |
9804 KB |
Output is correct |
33 |
Correct |
7 ms |
9800 KB |
Output is correct |
34 |
Correct |
7 ms |
9804 KB |
Output is correct |
35 |
Correct |
8 ms |
9932 KB |
Output is correct |
36 |
Correct |
8 ms |
9932 KB |
Output is correct |
37 |
Correct |
8 ms |
9932 KB |
Output is correct |
38 |
Correct |
9 ms |
10060 KB |
Output is correct |
39 |
Correct |
9 ms |
10060 KB |
Output is correct |
40 |
Correct |
8 ms |
9804 KB |
Output is correct |
41 |
Correct |
7 ms |
9856 KB |
Output is correct |
42 |
Correct |
8 ms |
9840 KB |
Output is correct |
43 |
Correct |
11 ms |
10016 KB |
Output is correct |
44 |
Correct |
12 ms |
9964 KB |
Output is correct |
45 |
Correct |
10 ms |
10048 KB |
Output is correct |
46 |
Correct |
10 ms |
10188 KB |
Output is correct |
47 |
Correct |
9 ms |
10220 KB |
Output is correct |
48 |
Correct |
9 ms |
10108 KB |
Output is correct |
49 |
Correct |
10 ms |
10188 KB |
Output is correct |
50 |
Correct |
10 ms |
9964 KB |
Output is correct |
51 |
Correct |
8 ms |
10060 KB |
Output is correct |
52 |
Correct |
8 ms |
10060 KB |
Output is correct |
53 |
Correct |
10 ms |
10060 KB |
Output is correct |
54 |
Correct |
10 ms |
10096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
9676 KB |
Output is correct |
2 |
Correct |
8 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
7 ms |
9676 KB |
Output is correct |
5 |
Correct |
8 ms |
9676 KB |
Output is correct |
6 |
Correct |
6 ms |
9616 KB |
Output is correct |
7 |
Correct |
6 ms |
9704 KB |
Output is correct |
8 |
Correct |
8 ms |
9676 KB |
Output is correct |
9 |
Correct |
7 ms |
9644 KB |
Output is correct |
10 |
Correct |
7 ms |
9700 KB |
Output is correct |
11 |
Correct |
8 ms |
9676 KB |
Output is correct |
12 |
Correct |
7 ms |
9676 KB |
Output is correct |
13 |
Correct |
8 ms |
9640 KB |
Output is correct |
14 |
Correct |
7 ms |
9728 KB |
Output is correct |
15 |
Correct |
7 ms |
9676 KB |
Output is correct |
16 |
Correct |
6 ms |
9704 KB |
Output is correct |
17 |
Correct |
7 ms |
9612 KB |
Output is correct |
18 |
Correct |
8 ms |
9700 KB |
Output is correct |
19 |
Correct |
7 ms |
9676 KB |
Output is correct |
20 |
Correct |
7 ms |
9676 KB |
Output is correct |
21 |
Correct |
7 ms |
9676 KB |
Output is correct |
22 |
Correct |
7 ms |
9704 KB |
Output is correct |
23 |
Correct |
6 ms |
9676 KB |
Output is correct |
24 |
Correct |
6 ms |
9676 KB |
Output is correct |
25 |
Correct |
6 ms |
9676 KB |
Output is correct |
26 |
Correct |
6 ms |
9676 KB |
Output is correct |
27 |
Correct |
6 ms |
9676 KB |
Output is correct |
28 |
Correct |
7 ms |
9704 KB |
Output is correct |
29 |
Correct |
8 ms |
9676 KB |
Output is correct |
30 |
Correct |
8 ms |
9676 KB |
Output is correct |
31 |
Correct |
8 ms |
9676 KB |
Output is correct |
32 |
Correct |
8 ms |
9804 KB |
Output is correct |
33 |
Correct |
7 ms |
9800 KB |
Output is correct |
34 |
Correct |
7 ms |
9804 KB |
Output is correct |
35 |
Correct |
8 ms |
9932 KB |
Output is correct |
36 |
Correct |
8 ms |
9932 KB |
Output is correct |
37 |
Correct |
8 ms |
9932 KB |
Output is correct |
38 |
Correct |
9 ms |
10060 KB |
Output is correct |
39 |
Correct |
9 ms |
10060 KB |
Output is correct |
40 |
Correct |
8 ms |
9804 KB |
Output is correct |
41 |
Correct |
7 ms |
9856 KB |
Output is correct |
42 |
Correct |
8 ms |
9840 KB |
Output is correct |
43 |
Correct |
11 ms |
10016 KB |
Output is correct |
44 |
Correct |
12 ms |
9964 KB |
Output is correct |
45 |
Correct |
10 ms |
10048 KB |
Output is correct |
46 |
Correct |
10 ms |
10188 KB |
Output is correct |
47 |
Correct |
9 ms |
10220 KB |
Output is correct |
48 |
Correct |
9 ms |
10108 KB |
Output is correct |
49 |
Correct |
10 ms |
10188 KB |
Output is correct |
50 |
Correct |
10 ms |
9964 KB |
Output is correct |
51 |
Correct |
8 ms |
10060 KB |
Output is correct |
52 |
Correct |
8 ms |
10060 KB |
Output is correct |
53 |
Correct |
10 ms |
10060 KB |
Output is correct |
54 |
Correct |
10 ms |
10096 KB |
Output is correct |
55 |
Correct |
13 ms |
10600 KB |
Output is correct |
56 |
Correct |
31 ms |
13512 KB |
Output is correct |
57 |
Correct |
50 ms |
16068 KB |
Output is correct |
58 |
Correct |
66 ms |
17836 KB |
Output is correct |
59 |
Correct |
88 ms |
20544 KB |
Output is correct |
60 |
Correct |
113 ms |
23144 KB |
Output is correct |
61 |
Correct |
145 ms |
25276 KB |
Output is correct |
62 |
Correct |
149 ms |
26772 KB |
Output is correct |
63 |
Correct |
202 ms |
30012 KB |
Output is correct |
64 |
Correct |
204 ms |
30840 KB |
Output is correct |
65 |
Correct |
92 ms |
21572 KB |
Output is correct |
66 |
Correct |
90 ms |
21596 KB |
Output is correct |
67 |
Correct |
90 ms |
21640 KB |
Output is correct |
68 |
Correct |
167 ms |
28308 KB |
Output is correct |
69 |
Correct |
176 ms |
28944 KB |
Output is correct |
70 |
Correct |
178 ms |
28892 KB |
Output is correct |
71 |
Correct |
214 ms |
43788 KB |
Output is correct |
72 |
Correct |
247 ms |
43860 KB |
Output is correct |
73 |
Correct |
182 ms |
40424 KB |
Output is correct |
74 |
Correct |
232 ms |
40756 KB |
Output is correct |
75 |
Correct |
225 ms |
39304 KB |
Output is correct |
76 |
Correct |
194 ms |
39220 KB |
Output is correct |
77 |
Correct |
261 ms |
38716 KB |
Output is correct |
78 |
Correct |
245 ms |
38192 KB |
Output is correct |
79 |
Correct |
191 ms |
36012 KB |
Output is correct |