#include <iostream>
#include <vector>
#include <set>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <fstream>
#include <complex>
#include <random>
#include <stack>
#include <chrono>
#include <set>
#define FOR(i,n) for(int i=0;i<n;i++)
#define FORE(i,a,b) for(int i=a;i<=b;i++)
#define ll long long int
#define vi vector<int>
#define ii pair<int,int>
#define pb push_back
#define mp make_pair
#define ff first
#define il pair<int,ll>
#define li pair<ll,int>
#define ss second
#define pll pair<ll,ll>
#define cd complex<double>
#define ld long double
#define pld pair<ld,ld>
#define plli pair<pll,ll>
#define vv vector
using namespace std;
const int MAXN = 3e5 + 10;
ll pe[MAXN];
vi g[MAXN];
ll a[MAXN];
ll b[MAXN];
multiset<ll,greater<ll> >* sp[MAXN];
void dfs(int node){
if(g[node].size() == 0){
// this is a leaf node;
a[node] = 1;
b[node] = -pe[node];
sp[node] = new multiset<ll,greater<ll> >();
sp[node]->insert(pe[node]);
sp[node]->insert(pe[node]);
}else{
ll mx = 0;
int ind = 0;
for(auto e : g[node]){
dfs(e);
if(sp[e]->size() > mx){
ind = e;
mx = sp[e]->size();
}
}
sp[node] = sp[ind];
for(auto e : g[node]){
a[node] += a[e];
b[node] += b[e];
if(e == ind)continue;
for(auto x : *sp[e]){
sp[node]->insert(x);
}
}
while(a[node] > 1){
ll pt = *(sp[node]->begin());
sp[node]->erase(sp[node]->begin());
a[node]--;b[node] += pt;
}
ll a1 = *(sp[node]->begin());sp[node]->erase(sp[node]->begin());
ll b1 = *(sp[node]->begin());sp[node]->erase(sp[node]->begin());
sp[node]->insert(a1 + pe[node]);
sp[node]->insert(b1 + pe[node]);
b[node] -= pe[node];
}
}
int main(){
int n,m;
cin >> n >> m;
FOR(i,n+m-1){
int a,b;
cin >> a >> b;
a--;
pe[i+1] = b;
g[a].pb(i+1);
}
dfs(0);
//FOR(i,n)cout << a[i] << " " << b[i] << endl;
while(a[0] > 0){
ll pt = *(sp[0]->begin());
sp[0]->erase(sp[0]->begin());
a[0]--;b[0] += pt;
}
cout << b[0] << endl;
return 0;
}
Compilation message
fireworks.cpp: In function 'void dfs(int)':
fireworks.cpp:59:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(sp[e]->size() > mx){
~~~~~~~~~~~~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
7544 KB |
Output is correct |
2 |
Correct |
8 ms |
7416 KB |
Output is correct |
3 |
Correct |
8 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 |
7416 KB |
Output is correct |
7 |
Correct |
8 ms |
7416 KB |
Output is correct |
8 |
Correct |
8 ms |
7416 KB |
Output is correct |
9 |
Correct |
9 ms |
7416 KB |
Output is correct |
10 |
Correct |
8 ms |
7420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
7416 KB |
Output is correct |
2 |
Correct |
8 ms |
7416 KB |
Output is correct |
3 |
Correct |
8 ms |
7416 KB |
Output is correct |
4 |
Correct |
8 ms |
7416 KB |
Output is correct |
5 |
Correct |
8 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 |
8 ms |
7416 KB |
Output is correct |
9 |
Correct |
9 ms |
7416 KB |
Output is correct |
10 |
Correct |
8 ms |
7416 KB |
Output is correct |
11 |
Correct |
10 ms |
7416 KB |
Output is correct |
12 |
Correct |
8 ms |
7416 KB |
Output is correct |
13 |
Correct |
10 ms |
7420 KB |
Output is correct |
14 |
Correct |
8 ms |
7388 KB |
Output is correct |
15 |
Correct |
9 ms |
7416 KB |
Output is correct |
16 |
Correct |
9 ms |
7416 KB |
Output is correct |
17 |
Correct |
9 ms |
7516 KB |
Output is correct |
18 |
Correct |
8 ms |
7416 KB |
Output is correct |
19 |
Correct |
9 ms |
7416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
7544 KB |
Output is correct |
2 |
Correct |
8 ms |
7416 KB |
Output is correct |
3 |
Correct |
8 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 |
7416 KB |
Output is correct |
7 |
Correct |
8 ms |
7416 KB |
Output is correct |
8 |
Correct |
8 ms |
7416 KB |
Output is correct |
9 |
Correct |
9 ms |
7416 KB |
Output is correct |
10 |
Correct |
8 ms |
7420 KB |
Output is correct |
11 |
Correct |
8 ms |
7416 KB |
Output is correct |
12 |
Correct |
8 ms |
7416 KB |
Output is correct |
13 |
Correct |
8 ms |
7416 KB |
Output is correct |
14 |
Correct |
8 ms |
7416 KB |
Output is correct |
15 |
Correct |
8 ms |
7416 KB |
Output is correct |
16 |
Correct |
9 ms |
7420 KB |
Output is correct |
17 |
Correct |
9 ms |
7416 KB |
Output is correct |
18 |
Correct |
8 ms |
7416 KB |
Output is correct |
19 |
Correct |
9 ms |
7416 KB |
Output is correct |
20 |
Correct |
8 ms |
7416 KB |
Output is correct |
21 |
Correct |
10 ms |
7416 KB |
Output is correct |
22 |
Correct |
8 ms |
7416 KB |
Output is correct |
23 |
Correct |
10 ms |
7420 KB |
Output is correct |
24 |
Correct |
8 ms |
7388 KB |
Output is correct |
25 |
Correct |
9 ms |
7416 KB |
Output is correct |
26 |
Correct |
9 ms |
7416 KB |
Output is correct |
27 |
Correct |
9 ms |
7516 KB |
Output is correct |
28 |
Correct |
8 ms |
7416 KB |
Output is correct |
29 |
Correct |
9 ms |
7416 KB |
Output is correct |
30 |
Correct |
9 ms |
7416 KB |
Output is correct |
31 |
Correct |
10 ms |
7544 KB |
Output is correct |
32 |
Correct |
11 ms |
7676 KB |
Output is correct |
33 |
Correct |
13 ms |
7852 KB |
Output is correct |
34 |
Correct |
13 ms |
7928 KB |
Output is correct |
35 |
Correct |
15 ms |
7928 KB |
Output is correct |
36 |
Correct |
15 ms |
8056 KB |
Output is correct |
37 |
Correct |
17 ms |
8312 KB |
Output is correct |
38 |
Correct |
17 ms |
8184 KB |
Output is correct |
39 |
Correct |
17 ms |
8312 KB |
Output is correct |
40 |
Correct |
16 ms |
8184 KB |
Output is correct |
41 |
Correct |
16 ms |
8312 KB |
Output is correct |
42 |
Correct |
16 ms |
7800 KB |
Output is correct |
43 |
Correct |
17 ms |
8440 KB |
Output is correct |
44 |
Correct |
17 ms |
8440 KB |
Output is correct |
45 |
Correct |
17 ms |
8440 KB |
Output is correct |
46 |
Correct |
17 ms |
8952 KB |
Output is correct |
47 |
Correct |
20 ms |
8824 KB |
Output is correct |
48 |
Correct |
17 ms |
8696 KB |
Output is correct |
49 |
Correct |
19 ms |
8828 KB |
Output is correct |
50 |
Correct |
16 ms |
8568 KB |
Output is correct |
51 |
Correct |
16 ms |
8568 KB |
Output is correct |
52 |
Correct |
17 ms |
8668 KB |
Output is correct |
53 |
Correct |
17 ms |
8668 KB |
Output is correct |
54 |
Correct |
17 ms |
8952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
7544 KB |
Output is correct |
2 |
Correct |
8 ms |
7416 KB |
Output is correct |
3 |
Correct |
8 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 |
7416 KB |
Output is correct |
7 |
Correct |
8 ms |
7416 KB |
Output is correct |
8 |
Correct |
8 ms |
7416 KB |
Output is correct |
9 |
Correct |
9 ms |
7416 KB |
Output is correct |
10 |
Correct |
8 ms |
7420 KB |
Output is correct |
11 |
Correct |
8 ms |
7416 KB |
Output is correct |
12 |
Correct |
8 ms |
7416 KB |
Output is correct |
13 |
Correct |
8 ms |
7416 KB |
Output is correct |
14 |
Correct |
8 ms |
7416 KB |
Output is correct |
15 |
Correct |
8 ms |
7416 KB |
Output is correct |
16 |
Correct |
9 ms |
7420 KB |
Output is correct |
17 |
Correct |
9 ms |
7416 KB |
Output is correct |
18 |
Correct |
8 ms |
7416 KB |
Output is correct |
19 |
Correct |
9 ms |
7416 KB |
Output is correct |
20 |
Correct |
8 ms |
7416 KB |
Output is correct |
21 |
Correct |
10 ms |
7416 KB |
Output is correct |
22 |
Correct |
8 ms |
7416 KB |
Output is correct |
23 |
Correct |
10 ms |
7420 KB |
Output is correct |
24 |
Correct |
8 ms |
7388 KB |
Output is correct |
25 |
Correct |
9 ms |
7416 KB |
Output is correct |
26 |
Correct |
9 ms |
7416 KB |
Output is correct |
27 |
Correct |
9 ms |
7516 KB |
Output is correct |
28 |
Correct |
8 ms |
7416 KB |
Output is correct |
29 |
Correct |
9 ms |
7416 KB |
Output is correct |
30 |
Correct |
9 ms |
7416 KB |
Output is correct |
31 |
Correct |
10 ms |
7544 KB |
Output is correct |
32 |
Correct |
11 ms |
7676 KB |
Output is correct |
33 |
Correct |
13 ms |
7852 KB |
Output is correct |
34 |
Correct |
13 ms |
7928 KB |
Output is correct |
35 |
Correct |
15 ms |
7928 KB |
Output is correct |
36 |
Correct |
15 ms |
8056 KB |
Output is correct |
37 |
Correct |
17 ms |
8312 KB |
Output is correct |
38 |
Correct |
17 ms |
8184 KB |
Output is correct |
39 |
Correct |
17 ms |
8312 KB |
Output is correct |
40 |
Correct |
16 ms |
8184 KB |
Output is correct |
41 |
Correct |
16 ms |
8312 KB |
Output is correct |
42 |
Correct |
16 ms |
7800 KB |
Output is correct |
43 |
Correct |
17 ms |
8440 KB |
Output is correct |
44 |
Correct |
17 ms |
8440 KB |
Output is correct |
45 |
Correct |
17 ms |
8440 KB |
Output is correct |
46 |
Correct |
17 ms |
8952 KB |
Output is correct |
47 |
Correct |
20 ms |
8824 KB |
Output is correct |
48 |
Correct |
17 ms |
8696 KB |
Output is correct |
49 |
Correct |
19 ms |
8828 KB |
Output is correct |
50 |
Correct |
16 ms |
8568 KB |
Output is correct |
51 |
Correct |
16 ms |
8568 KB |
Output is correct |
52 |
Correct |
17 ms |
8668 KB |
Output is correct |
53 |
Correct |
17 ms |
8668 KB |
Output is correct |
54 |
Correct |
17 ms |
8952 KB |
Output is correct |
55 |
Correct |
32 ms |
9692 KB |
Output is correct |
56 |
Correct |
122 ms |
16504 KB |
Output is correct |
57 |
Correct |
195 ms |
23204 KB |
Output is correct |
58 |
Correct |
246 ms |
27896 KB |
Output is correct |
59 |
Correct |
342 ms |
34820 KB |
Output is correct |
60 |
Correct |
455 ms |
41720 KB |
Output is correct |
61 |
Correct |
544 ms |
46544 KB |
Output is correct |
62 |
Correct |
635 ms |
50980 KB |
Output is correct |
63 |
Correct |
658 ms |
60396 KB |
Output is correct |
64 |
Correct |
714 ms |
61688 KB |
Output is correct |
65 |
Correct |
505 ms |
59260 KB |
Output is correct |
66 |
Correct |
492 ms |
59256 KB |
Output is correct |
67 |
Correct |
513 ms |
31352 KB |
Output is correct |
68 |
Correct |
609 ms |
70952 KB |
Output is correct |
69 |
Correct |
651 ms |
69060 KB |
Output is correct |
70 |
Correct |
659 ms |
68976 KB |
Output is correct |
71 |
Correct |
759 ms |
109740 KB |
Output is correct |
72 |
Correct |
700 ms |
109692 KB |
Output is correct |
73 |
Correct |
687 ms |
97528 KB |
Output is correct |
74 |
Correct |
679 ms |
97552 KB |
Output is correct |
75 |
Correct |
656 ms |
97064 KB |
Output is correct |
76 |
Correct |
784 ms |
97264 KB |
Output is correct |
77 |
Correct |
752 ms |
95588 KB |
Output is correct |
78 |
Correct |
764 ms |
92588 KB |
Output is correct |
79 |
Correct |
774 ms |
96612 KB |
Output is correct |