#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define sz(x) ((int)x.size())
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
#define mset(x,y) memset(x,y,sizeof(x))
#define rep(i,a,b) for(int i(a);i<=(b);i++)
#define repr(i,a,b) for(int i(a);i>=(b);i--)
using namespace std;
#ifdef LOCAL
#define dmp(...) _dmp(#__VA_ARGS__, __VA_ARGS__)
#else
#define dmp(...) (__VA_ARGS__)
#endif
template<class TH> void _dmp(const char *sdbg, TH h){cout<<sdbg<<"="<<h<<endl;}
template<class TH, class... TA> void _dmp(const char *sdbg, TH h, TA...a){
while(*sdbg!=',')cout<<*sdbg++;cout<<"="<<h<<","; _dmp(sdbg+1, a...);
}
using namespace __gnu_pbds;
using ordered_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
int n,m;
int p[300010],c[300010];
ll ans[300010],up[300010];
vi adj[300010];
priority_queue<ll>* pq[300010];
void merge(int u,int v) {
while(!pq[v]->empty()) {
pq[u]->push(pq[v]->top());
pq[v]->pop();
}
ans[u]+=ans[v];
}
void adjust(int u) {
up[u]+=c[u];
ll top=pq[u]->top();
pq[u]->pop();
pq[u]->push(top+c[u]);
}
void debug(int u) {
priority_queue<ll> b;
cout<<"u:"<<u<<",ans:"<<ans[u]<<",up:"<<up[u]<<",pq:";
while(!pq[u]->empty())b.push(pq[u]->top()),pq[u]->pop();
while(!b.empty()) {
ll it=b.top();b.pop();
cout<<it<<' ';
pq[u]->push(it);
}cout<<endl;
}
void solve(int u) {
if(adj[u].empty()) {
pq[u]=new priority_queue<ll>();
pq[u]->push(0);
up[u]=0;
return;
}
int mx=-1,id=0;
vector<ll> ups;
for(auto& it:adj[u]) {
solve(it);
adjust(it);
if(mx<sz((*pq[it]))) {
mx=sz((*pq[it]));
id=it;
}
ups.pb(up[it]);
}
for(auto& it:adj[u]) {
if(it==id)continue;
merge(id,it);
}
pq[u]=pq[id];
ans[u]=ans[id];
up[u]=up[id];
sort(all(ups));reverse(all(ups));
up[u]=1e18;
for(auto& it:ups) {
if(it>=pq[id]->top()) {
up[u]=it;
} else {
ans[u]+=pq[id]->top()-it;
up[u]=pq[id]->top();
pq[id]->pop();
pq[id]->push(it);
}
}
//debug(u);
}
void MAIN() {
int i,j;
cin>>n>>m;
for(i=2;i<=n+m;i++) {
cin>>p[i]>>c[i];
adj[p[i]].pb(i);
}
solve(1);
cout<<ans[1];
}
int main() {
ios::sync_with_stdio(false);cin.tie(0);
int T=1;
//cin>>T;
for(int tt=1;tt<=T;tt++) {
MAIN();
}
return 0;
}
Compilation message
fireworks.cpp: In function 'void _dmp(const char*, TH, TA ...)':
fireworks.cpp:20:1: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
while(*sdbg!=',')cout<<*sdbg++;cout<<"="<<h<<","; _dmp(sdbg+1, a...);
^~~~~
fireworks.cpp:20:32: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
while(*sdbg!=',')cout<<*sdbg++;cout<<"="<<h<<","; _dmp(sdbg+1, a...);
^~~~
fireworks.cpp: In function 'void MAIN()':
fireworks.cpp:97:8: warning: unused variable 'j' [-Wunused-variable]
int i,j;
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7416 KB |
Output is correct |
2 |
Correct |
9 ms |
7416 KB |
Output is correct |
3 |
Correct |
9 ms |
7416 KB |
Output is correct |
4 |
Correct |
9 ms |
7416 KB |
Output is correct |
5 |
Correct |
9 ms |
7416 KB |
Output is correct |
6 |
Correct |
9 ms |
7420 KB |
Output is correct |
7 |
Correct |
9 ms |
7416 KB |
Output is correct |
8 |
Correct |
9 ms |
7416 KB |
Output is correct |
9 |
Correct |
9 ms |
7416 KB |
Output is correct |
10 |
Correct |
9 ms |
7416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7416 KB |
Output is correct |
2 |
Correct |
9 ms |
7416 KB |
Output is correct |
3 |
Correct |
9 ms |
7416 KB |
Output is correct |
4 |
Correct |
9 ms |
7416 KB |
Output is correct |
5 |
Correct |
11 ms |
7420 KB |
Output is correct |
6 |
Correct |
9 ms |
7416 KB |
Output is correct |
7 |
Correct |
9 ms |
7416 KB |
Output is correct |
8 |
Correct |
9 ms |
7416 KB |
Output is correct |
9 |
Correct |
9 ms |
7416 KB |
Output is correct |
10 |
Correct |
10 ms |
7416 KB |
Output is correct |
11 |
Correct |
9 ms |
7416 KB |
Output is correct |
12 |
Correct |
9 ms |
7416 KB |
Output is correct |
13 |
Correct |
9 ms |
7416 KB |
Output is correct |
14 |
Correct |
9 ms |
7416 KB |
Output is correct |
15 |
Correct |
9 ms |
7420 KB |
Output is correct |
16 |
Correct |
9 ms |
7416 KB |
Output is correct |
17 |
Correct |
9 ms |
7416 KB |
Output is correct |
18 |
Correct |
9 ms |
7416 KB |
Output is correct |
19 |
Correct |
9 ms |
7416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7416 KB |
Output is correct |
2 |
Correct |
9 ms |
7416 KB |
Output is correct |
3 |
Correct |
9 ms |
7416 KB |
Output is correct |
4 |
Correct |
9 ms |
7416 KB |
Output is correct |
5 |
Correct |
9 ms |
7416 KB |
Output is correct |
6 |
Correct |
9 ms |
7420 KB |
Output is correct |
7 |
Correct |
9 ms |
7416 KB |
Output is correct |
8 |
Correct |
9 ms |
7416 KB |
Output is correct |
9 |
Correct |
9 ms |
7416 KB |
Output is correct |
10 |
Correct |
9 ms |
7416 KB |
Output is correct |
11 |
Correct |
9 ms |
7416 KB |
Output is correct |
12 |
Correct |
9 ms |
7416 KB |
Output is correct |
13 |
Correct |
9 ms |
7416 KB |
Output is correct |
14 |
Correct |
9 ms |
7416 KB |
Output is correct |
15 |
Correct |
11 ms |
7420 KB |
Output is correct |
16 |
Correct |
9 ms |
7416 KB |
Output is correct |
17 |
Correct |
9 ms |
7416 KB |
Output is correct |
18 |
Correct |
9 ms |
7416 KB |
Output is correct |
19 |
Correct |
9 ms |
7416 KB |
Output is correct |
20 |
Correct |
10 ms |
7416 KB |
Output is correct |
21 |
Correct |
9 ms |
7416 KB |
Output is correct |
22 |
Correct |
9 ms |
7416 KB |
Output is correct |
23 |
Correct |
9 ms |
7416 KB |
Output is correct |
24 |
Correct |
9 ms |
7416 KB |
Output is correct |
25 |
Correct |
9 ms |
7420 KB |
Output is correct |
26 |
Correct |
9 ms |
7416 KB |
Output is correct |
27 |
Correct |
9 ms |
7416 KB |
Output is correct |
28 |
Correct |
9 ms |
7416 KB |
Output is correct |
29 |
Correct |
9 ms |
7416 KB |
Output is correct |
30 |
Correct |
11 ms |
7416 KB |
Output is correct |
31 |
Correct |
12 ms |
7544 KB |
Output is correct |
32 |
Correct |
10 ms |
7672 KB |
Output is correct |
33 |
Correct |
11 ms |
7672 KB |
Output is correct |
34 |
Correct |
11 ms |
7672 KB |
Output is correct |
35 |
Correct |
12 ms |
7800 KB |
Output is correct |
36 |
Correct |
10 ms |
7800 KB |
Output is correct |
37 |
Correct |
12 ms |
7928 KB |
Output is correct |
38 |
Correct |
12 ms |
7928 KB |
Output is correct |
39 |
Correct |
12 ms |
7928 KB |
Output is correct |
40 |
Correct |
11 ms |
8316 KB |
Output is correct |
41 |
Correct |
11 ms |
8312 KB |
Output is correct |
42 |
Correct |
11 ms |
7804 KB |
Output is correct |
43 |
Correct |
12 ms |
8184 KB |
Output is correct |
44 |
Correct |
13 ms |
8184 KB |
Output is correct |
45 |
Correct |
12 ms |
8176 KB |
Output is correct |
46 |
Correct |
12 ms |
8184 KB |
Output is correct |
47 |
Correct |
12 ms |
8184 KB |
Output is correct |
48 |
Correct |
12 ms |
8056 KB |
Output is correct |
49 |
Correct |
12 ms |
8184 KB |
Output is correct |
50 |
Correct |
12 ms |
8056 KB |
Output is correct |
51 |
Correct |
12 ms |
8056 KB |
Output is correct |
52 |
Correct |
12 ms |
8060 KB |
Output is correct |
53 |
Correct |
12 ms |
8056 KB |
Output is correct |
54 |
Correct |
12 ms |
8184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7416 KB |
Output is correct |
2 |
Correct |
9 ms |
7416 KB |
Output is correct |
3 |
Correct |
9 ms |
7416 KB |
Output is correct |
4 |
Correct |
9 ms |
7416 KB |
Output is correct |
5 |
Correct |
9 ms |
7416 KB |
Output is correct |
6 |
Correct |
9 ms |
7420 KB |
Output is correct |
7 |
Correct |
9 ms |
7416 KB |
Output is correct |
8 |
Correct |
9 ms |
7416 KB |
Output is correct |
9 |
Correct |
9 ms |
7416 KB |
Output is correct |
10 |
Correct |
9 ms |
7416 KB |
Output is correct |
11 |
Correct |
9 ms |
7416 KB |
Output is correct |
12 |
Correct |
9 ms |
7416 KB |
Output is correct |
13 |
Correct |
9 ms |
7416 KB |
Output is correct |
14 |
Correct |
9 ms |
7416 KB |
Output is correct |
15 |
Correct |
11 ms |
7420 KB |
Output is correct |
16 |
Correct |
9 ms |
7416 KB |
Output is correct |
17 |
Correct |
9 ms |
7416 KB |
Output is correct |
18 |
Correct |
9 ms |
7416 KB |
Output is correct |
19 |
Correct |
9 ms |
7416 KB |
Output is correct |
20 |
Correct |
10 ms |
7416 KB |
Output is correct |
21 |
Correct |
9 ms |
7416 KB |
Output is correct |
22 |
Correct |
9 ms |
7416 KB |
Output is correct |
23 |
Correct |
9 ms |
7416 KB |
Output is correct |
24 |
Correct |
9 ms |
7416 KB |
Output is correct |
25 |
Correct |
9 ms |
7420 KB |
Output is correct |
26 |
Correct |
9 ms |
7416 KB |
Output is correct |
27 |
Correct |
9 ms |
7416 KB |
Output is correct |
28 |
Correct |
9 ms |
7416 KB |
Output is correct |
29 |
Correct |
9 ms |
7416 KB |
Output is correct |
30 |
Correct |
11 ms |
7416 KB |
Output is correct |
31 |
Correct |
12 ms |
7544 KB |
Output is correct |
32 |
Correct |
10 ms |
7672 KB |
Output is correct |
33 |
Correct |
11 ms |
7672 KB |
Output is correct |
34 |
Correct |
11 ms |
7672 KB |
Output is correct |
35 |
Correct |
12 ms |
7800 KB |
Output is correct |
36 |
Correct |
10 ms |
7800 KB |
Output is correct |
37 |
Correct |
12 ms |
7928 KB |
Output is correct |
38 |
Correct |
12 ms |
7928 KB |
Output is correct |
39 |
Correct |
12 ms |
7928 KB |
Output is correct |
40 |
Correct |
11 ms |
8316 KB |
Output is correct |
41 |
Correct |
11 ms |
8312 KB |
Output is correct |
42 |
Correct |
11 ms |
7804 KB |
Output is correct |
43 |
Correct |
12 ms |
8184 KB |
Output is correct |
44 |
Correct |
13 ms |
8184 KB |
Output is correct |
45 |
Correct |
12 ms |
8176 KB |
Output is correct |
46 |
Correct |
12 ms |
8184 KB |
Output is correct |
47 |
Correct |
12 ms |
8184 KB |
Output is correct |
48 |
Correct |
12 ms |
8056 KB |
Output is correct |
49 |
Correct |
12 ms |
8184 KB |
Output is correct |
50 |
Correct |
12 ms |
8056 KB |
Output is correct |
51 |
Correct |
12 ms |
8056 KB |
Output is correct |
52 |
Correct |
12 ms |
8060 KB |
Output is correct |
53 |
Correct |
12 ms |
8056 KB |
Output is correct |
54 |
Correct |
12 ms |
8184 KB |
Output is correct |
55 |
Correct |
17 ms |
8812 KB |
Output is correct |
56 |
Correct |
43 ms |
12792 KB |
Output is correct |
57 |
Correct |
78 ms |
16608 KB |
Output is correct |
58 |
Correct |
100 ms |
19184 KB |
Output is correct |
59 |
Correct |
139 ms |
23024 KB |
Output is correct |
60 |
Correct |
181 ms |
26960 KB |
Output is correct |
61 |
Correct |
209 ms |
29788 KB |
Output is correct |
62 |
Correct |
233 ms |
31980 KB |
Output is correct |
63 |
Correct |
288 ms |
36876 KB |
Output is correct |
64 |
Correct |
295 ms |
38252 KB |
Output is correct |
65 |
Correct |
156 ms |
63992 KB |
Output is correct |
66 |
Correct |
154 ms |
63964 KB |
Output is correct |
67 |
Correct |
177 ms |
31224 KB |
Output is correct |
68 |
Correct |
190 ms |
54372 KB |
Output is correct |
69 |
Correct |
255 ms |
52196 KB |
Output is correct |
70 |
Correct |
235 ms |
52068 KB |
Output is correct |
71 |
Correct |
219 ms |
55788 KB |
Output is correct |
72 |
Correct |
237 ms |
55792 KB |
Output is correct |
73 |
Correct |
194 ms |
52600 KB |
Output is correct |
74 |
Correct |
200 ms |
52640 KB |
Output is correct |
75 |
Correct |
199 ms |
50856 KB |
Output is correct |
76 |
Correct |
229 ms |
50920 KB |
Output is correct |
77 |
Correct |
223 ms |
49468 KB |
Output is correct |
78 |
Correct |
202 ms |
46928 KB |
Output is correct |
79 |
Correct |
202 ms |
51172 KB |
Output is correct |