#include<bits/stdc++.h>
#define fi first
#define se second
#define pitem item*
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
const int N=1e6+10;
const int SS=1<<19;
const int INFi=2e9;
const ll INFl=1e16;
const ll mod2=998244353;
const ll mod=1e9+7;
const ll mod3=2027865967;
const ll p=70032301;
const ull p2=913;
const int L=20;
int n,m;
ll t[N],t2[N];
vi graf[N];
multiset<ll>* dfs(int v){
if(v>n){
multiset<ll>* xd= new multiset<ll>;
xd->insert(t[v]);
xd->insert(t[v]);
t2[v]=-t[v];
return xd;
}
multiset<ll>* c=nullptr;
for(auto u:graf[v]){
multiset<ll>* x=dfs(u);
t2[v]+=t2[u];
if(!c or c->size()<x->size()) swap(c,x);
while(x and x->size()){
c->insert(*(x->begin()));
x->erase(x->begin());
}
delete x;
}
for(int i=0;i<graf[v].size()-1;i++){
auto it=c->end();
it--;
t2[v]+=*it;
c->erase(it);
}
t2[v]-=t[v];
auto it=c->end();
it--;
auto it2=c->end();
it2--,it2--;
ll v1=*it+t[v],v2=*it2+t[v];
c->erase(it),c->erase(it2);
c->insert(v1),c->insert(v2);
return c;
}
void solve(){
cin>>n>>m;
for(int i=2;i<=n+m;i++){
int a;
cin>>a>>t[i];
graf[a].push_back(i);
}
multiset<ll>* r=dfs(1);
auto it=r->end();
it--;
cout<<t2[1]+*it<<"\n";
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0);
int tt=1;
while(tt--) solve();
}
Compilation message
fireworks.cpp: In function 'std::multiset<long long int>* dfs(int)':
fireworks.cpp:42:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for(int i=0;i<graf[v].size()-1;i++){
| ~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
27228 KB |
Output is correct |
2 |
Correct |
6 ms |
27228 KB |
Output is correct |
3 |
Correct |
6 ms |
27228 KB |
Output is correct |
4 |
Correct |
6 ms |
27316 KB |
Output is correct |
5 |
Correct |
6 ms |
27228 KB |
Output is correct |
6 |
Correct |
8 ms |
27484 KB |
Output is correct |
7 |
Correct |
6 ms |
27228 KB |
Output is correct |
8 |
Correct |
6 ms |
27228 KB |
Output is correct |
9 |
Correct |
7 ms |
27228 KB |
Output is correct |
10 |
Correct |
7 ms |
27228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
27236 KB |
Output is correct |
2 |
Correct |
7 ms |
27228 KB |
Output is correct |
3 |
Correct |
6 ms |
27228 KB |
Output is correct |
4 |
Correct |
6 ms |
27228 KB |
Output is correct |
5 |
Correct |
6 ms |
27228 KB |
Output is correct |
6 |
Correct |
6 ms |
27228 KB |
Output is correct |
7 |
Correct |
6 ms |
27228 KB |
Output is correct |
8 |
Correct |
7 ms |
27740 KB |
Output is correct |
9 |
Correct |
7 ms |
27264 KB |
Output is correct |
10 |
Correct |
6 ms |
27228 KB |
Output is correct |
11 |
Correct |
6 ms |
27228 KB |
Output is correct |
12 |
Correct |
7 ms |
27228 KB |
Output is correct |
13 |
Correct |
7 ms |
27308 KB |
Output is correct |
14 |
Correct |
6 ms |
27228 KB |
Output is correct |
15 |
Correct |
6 ms |
27228 KB |
Output is correct |
16 |
Correct |
7 ms |
27228 KB |
Output is correct |
17 |
Correct |
6 ms |
27228 KB |
Output is correct |
18 |
Correct |
6 ms |
27228 KB |
Output is correct |
19 |
Correct |
6 ms |
27228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
27228 KB |
Output is correct |
2 |
Correct |
6 ms |
27228 KB |
Output is correct |
3 |
Correct |
6 ms |
27228 KB |
Output is correct |
4 |
Correct |
6 ms |
27316 KB |
Output is correct |
5 |
Correct |
6 ms |
27228 KB |
Output is correct |
6 |
Correct |
8 ms |
27484 KB |
Output is correct |
7 |
Correct |
6 ms |
27228 KB |
Output is correct |
8 |
Correct |
6 ms |
27228 KB |
Output is correct |
9 |
Correct |
7 ms |
27228 KB |
Output is correct |
10 |
Correct |
7 ms |
27228 KB |
Output is correct |
11 |
Correct |
6 ms |
27236 KB |
Output is correct |
12 |
Correct |
7 ms |
27228 KB |
Output is correct |
13 |
Correct |
6 ms |
27228 KB |
Output is correct |
14 |
Correct |
6 ms |
27228 KB |
Output is correct |
15 |
Correct |
6 ms |
27228 KB |
Output is correct |
16 |
Correct |
6 ms |
27228 KB |
Output is correct |
17 |
Correct |
6 ms |
27228 KB |
Output is correct |
18 |
Correct |
7 ms |
27740 KB |
Output is correct |
19 |
Correct |
7 ms |
27264 KB |
Output is correct |
20 |
Correct |
6 ms |
27228 KB |
Output is correct |
21 |
Correct |
6 ms |
27228 KB |
Output is correct |
22 |
Correct |
7 ms |
27228 KB |
Output is correct |
23 |
Correct |
7 ms |
27308 KB |
Output is correct |
24 |
Correct |
6 ms |
27228 KB |
Output is correct |
25 |
Correct |
6 ms |
27228 KB |
Output is correct |
26 |
Correct |
7 ms |
27228 KB |
Output is correct |
27 |
Correct |
6 ms |
27228 KB |
Output is correct |
28 |
Correct |
6 ms |
27228 KB |
Output is correct |
29 |
Correct |
6 ms |
27228 KB |
Output is correct |
30 |
Correct |
6 ms |
27480 KB |
Output is correct |
31 |
Correct |
7 ms |
27236 KB |
Output is correct |
32 |
Correct |
6 ms |
27480 KB |
Output is correct |
33 |
Correct |
7 ms |
27228 KB |
Output is correct |
34 |
Correct |
7 ms |
27228 KB |
Output is correct |
35 |
Correct |
8 ms |
27480 KB |
Output is correct |
36 |
Correct |
9 ms |
27484 KB |
Output is correct |
37 |
Correct |
8 ms |
27484 KB |
Output is correct |
38 |
Correct |
8 ms |
27484 KB |
Output is correct |
39 |
Correct |
8 ms |
27484 KB |
Output is correct |
40 |
Correct |
7 ms |
27996 KB |
Output is correct |
41 |
Correct |
8 ms |
27996 KB |
Output is correct |
42 |
Correct |
7 ms |
27556 KB |
Output is correct |
43 |
Correct |
8 ms |
27652 KB |
Output is correct |
44 |
Correct |
8 ms |
27736 KB |
Output is correct |
45 |
Correct |
8 ms |
27740 KB |
Output is correct |
46 |
Correct |
8 ms |
27484 KB |
Output is correct |
47 |
Correct |
8 ms |
27640 KB |
Output is correct |
48 |
Correct |
8 ms |
27484 KB |
Output is correct |
49 |
Correct |
8 ms |
27676 KB |
Output is correct |
50 |
Correct |
7 ms |
27484 KB |
Output is correct |
51 |
Correct |
7 ms |
27484 KB |
Output is correct |
52 |
Correct |
8 ms |
27484 KB |
Output is correct |
53 |
Correct |
10 ms |
27480 KB |
Output is correct |
54 |
Correct |
8 ms |
27736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
27228 KB |
Output is correct |
2 |
Correct |
6 ms |
27228 KB |
Output is correct |
3 |
Correct |
6 ms |
27228 KB |
Output is correct |
4 |
Correct |
6 ms |
27316 KB |
Output is correct |
5 |
Correct |
6 ms |
27228 KB |
Output is correct |
6 |
Correct |
8 ms |
27484 KB |
Output is correct |
7 |
Correct |
6 ms |
27228 KB |
Output is correct |
8 |
Correct |
6 ms |
27228 KB |
Output is correct |
9 |
Correct |
7 ms |
27228 KB |
Output is correct |
10 |
Correct |
7 ms |
27228 KB |
Output is correct |
11 |
Correct |
6 ms |
27236 KB |
Output is correct |
12 |
Correct |
7 ms |
27228 KB |
Output is correct |
13 |
Correct |
6 ms |
27228 KB |
Output is correct |
14 |
Correct |
6 ms |
27228 KB |
Output is correct |
15 |
Correct |
6 ms |
27228 KB |
Output is correct |
16 |
Correct |
6 ms |
27228 KB |
Output is correct |
17 |
Correct |
6 ms |
27228 KB |
Output is correct |
18 |
Correct |
7 ms |
27740 KB |
Output is correct |
19 |
Correct |
7 ms |
27264 KB |
Output is correct |
20 |
Correct |
6 ms |
27228 KB |
Output is correct |
21 |
Correct |
6 ms |
27228 KB |
Output is correct |
22 |
Correct |
7 ms |
27228 KB |
Output is correct |
23 |
Correct |
7 ms |
27308 KB |
Output is correct |
24 |
Correct |
6 ms |
27228 KB |
Output is correct |
25 |
Correct |
6 ms |
27228 KB |
Output is correct |
26 |
Correct |
7 ms |
27228 KB |
Output is correct |
27 |
Correct |
6 ms |
27228 KB |
Output is correct |
28 |
Correct |
6 ms |
27228 KB |
Output is correct |
29 |
Correct |
6 ms |
27228 KB |
Output is correct |
30 |
Correct |
6 ms |
27480 KB |
Output is correct |
31 |
Correct |
7 ms |
27236 KB |
Output is correct |
32 |
Correct |
6 ms |
27480 KB |
Output is correct |
33 |
Correct |
7 ms |
27228 KB |
Output is correct |
34 |
Correct |
7 ms |
27228 KB |
Output is correct |
35 |
Correct |
8 ms |
27480 KB |
Output is correct |
36 |
Correct |
9 ms |
27484 KB |
Output is correct |
37 |
Correct |
8 ms |
27484 KB |
Output is correct |
38 |
Correct |
8 ms |
27484 KB |
Output is correct |
39 |
Correct |
8 ms |
27484 KB |
Output is correct |
40 |
Correct |
7 ms |
27996 KB |
Output is correct |
41 |
Correct |
8 ms |
27996 KB |
Output is correct |
42 |
Correct |
7 ms |
27556 KB |
Output is correct |
43 |
Correct |
8 ms |
27652 KB |
Output is correct |
44 |
Correct |
8 ms |
27736 KB |
Output is correct |
45 |
Correct |
8 ms |
27740 KB |
Output is correct |
46 |
Correct |
8 ms |
27484 KB |
Output is correct |
47 |
Correct |
8 ms |
27640 KB |
Output is correct |
48 |
Correct |
8 ms |
27484 KB |
Output is correct |
49 |
Correct |
8 ms |
27676 KB |
Output is correct |
50 |
Correct |
7 ms |
27484 KB |
Output is correct |
51 |
Correct |
7 ms |
27484 KB |
Output is correct |
52 |
Correct |
8 ms |
27484 KB |
Output is correct |
53 |
Correct |
10 ms |
27480 KB |
Output is correct |
54 |
Correct |
8 ms |
27736 KB |
Output is correct |
55 |
Correct |
12 ms |
28012 KB |
Output is correct |
56 |
Correct |
28 ms |
29832 KB |
Output is correct |
57 |
Correct |
44 ms |
31572 KB |
Output is correct |
58 |
Correct |
57 ms |
32848 KB |
Output is correct |
59 |
Correct |
83 ms |
34896 KB |
Output is correct |
60 |
Correct |
99 ms |
38736 KB |
Output is correct |
61 |
Correct |
107 ms |
40016 KB |
Output is correct |
62 |
Correct |
135 ms |
43176 KB |
Output is correct |
63 |
Correct |
203 ms |
45724 KB |
Output is correct |
64 |
Correct |
168 ms |
46456 KB |
Output is correct |
65 |
Correct |
94 ms |
78420 KB |
Output is correct |
66 |
Correct |
96 ms |
78512 KB |
Output is correct |
67 |
Correct |
88 ms |
45904 KB |
Output is correct |
68 |
Correct |
133 ms |
64296 KB |
Output is correct |
69 |
Correct |
159 ms |
61416 KB |
Output is correct |
70 |
Correct |
163 ms |
61520 KB |
Output is correct |
71 |
Correct |
191 ms |
51292 KB |
Output is correct |
72 |
Correct |
190 ms |
51224 KB |
Output is correct |
73 |
Correct |
194 ms |
50892 KB |
Output is correct |
74 |
Correct |
172 ms |
50940 KB |
Output is correct |
75 |
Correct |
185 ms |
50772 KB |
Output is correct |
76 |
Correct |
171 ms |
50912 KB |
Output is correct |
77 |
Correct |
182 ms |
50772 KB |
Output is correct |
78 |
Correct |
230 ms |
51376 KB |
Output is correct |
79 |
Correct |
296 ms |
64184 KB |
Output is correct |