#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i = a; i<int(b);++i)
#define all(v) v.begin(),v.end()
#define sz(v) v.size()
#define trav(a,c) for(auto a: c)
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll,ll> pii;
vector<vector<pii> > e;
vector<multiset<ll>> ms;
tuple<multiset<ll>*,ll> getCost(ll v){ //p1,p2,h
if(e[v].size()==0) {
ms.push_back(multiset<ll>({0,0}));
return make_tuple(&ms[ms.size()-1],0);
}
//ll slope = 0;
// vector<pii> events;
multiset<ll> allPoints;
vector<tuple<multiset<ll>,ll> > allChildcosts;
ll slope = 0;
ll costAllZero = 0;
rep(i,0,e[v].size()){
multiset<ll> *points;
ll costZero;
tie(points,costZero) = getCost(e[v][i].first);
//cout<<e[v][i].first<<": "<<endl;
//cout<<ms[ms.size()-1].size()<<endl;
//cout<<points->size()<<endl;
ll l1 = *prev(points->end());
ll l2 = *prev(prev(points->end()));
points->erase(prev(points->end()));
points->erase(prev(points->end()));
l1+=e[v][i].second;
l2+=e[v][i].second;
points->insert(l1);
points->insert(l2);
costZero += e[v][i].second;
trav(p,*points) allPoints.insert(p);
slope -= points->size()-1;
costAllZero += costZero;
/*if(v==1){
cout<<e[v][i].first<<": "<<endl;
cout<<"costZero: "<<costZero<<endl;
trav(p,points){
cout<<p<<" ";
}
cout<<endl;
}*/
//allChildcosts.emplace(points,h);
}
while(allPoints.size()>-slope+1)
allPoints.erase(prev(allPoints.end()));
ms.push_back(allPoints);
return make_tuple(&ms[ms.size()-1],costAllZero);
}
int main(){
cin.sync_with_stdio(false);
ll n,m;
cin>>n>>m;
n+=m;
e.resize(n);
rep(i,1,n){
ll a = i;
ll b,c; cin>>b>>c;
e[b-1].push_back({a,c});
}
multiset<ll> *points;
ll costZero;
tie(points,costZero) = getCost(0);
ll last = 0;
ll ans = 1e18;
ll cost = costZero;
ll slope = -(points->size()-1);
//cout<<"costZero: "<<costZero<<endl;
trav(p,*points){
//cout<<p<<" ";
cost += slope*(p-last);
ans = min(ans,cost);
slope++;
last = p;
}
//cout<<endl;
cout<<ans<<endl;
}
Compilation message
fireworks.cpp: In function 'std::tuple<std::multiset<long long int, std::less<long long int>, std::allocator<long long int> >*, long long int> getCost(ll)':
fireworks.cpp:70:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(allPoints.size()>-slope+1)
~~~~~~~~~~~~~~~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
412 KB |
Output is correct |
4 |
Correct |
3 ms |
488 KB |
Output is correct |
5 |
Correct |
2 ms |
520 KB |
Output is correct |
6 |
Correct |
3 ms |
520 KB |
Output is correct |
7 |
Correct |
3 ms |
520 KB |
Output is correct |
8 |
Correct |
3 ms |
520 KB |
Output is correct |
9 |
Correct |
3 ms |
520 KB |
Output is correct |
10 |
Correct |
2 ms |
520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
520 KB |
Output is correct |
2 |
Correct |
3 ms |
520 KB |
Output is correct |
3 |
Correct |
2 ms |
544 KB |
Output is correct |
4 |
Correct |
2 ms |
544 KB |
Output is correct |
5 |
Correct |
3 ms |
560 KB |
Output is correct |
6 |
Correct |
3 ms |
560 KB |
Output is correct |
7 |
Correct |
3 ms |
560 KB |
Output is correct |
8 |
Correct |
4 ms |
688 KB |
Output is correct |
9 |
Correct |
3 ms |
756 KB |
Output is correct |
10 |
Correct |
3 ms |
756 KB |
Output is correct |
11 |
Correct |
3 ms |
756 KB |
Output is correct |
12 |
Correct |
3 ms |
756 KB |
Output is correct |
13 |
Correct |
4 ms |
756 KB |
Output is correct |
14 |
Correct |
3 ms |
764 KB |
Output is correct |
15 |
Correct |
3 ms |
764 KB |
Output is correct |
16 |
Correct |
3 ms |
764 KB |
Output is correct |
17 |
Correct |
3 ms |
764 KB |
Output is correct |
18 |
Correct |
3 ms |
764 KB |
Output is correct |
19 |
Correct |
4 ms |
764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
412 KB |
Output is correct |
4 |
Correct |
3 ms |
488 KB |
Output is correct |
5 |
Correct |
2 ms |
520 KB |
Output is correct |
6 |
Correct |
3 ms |
520 KB |
Output is correct |
7 |
Correct |
3 ms |
520 KB |
Output is correct |
8 |
Correct |
3 ms |
520 KB |
Output is correct |
9 |
Correct |
3 ms |
520 KB |
Output is correct |
10 |
Correct |
2 ms |
520 KB |
Output is correct |
11 |
Correct |
3 ms |
520 KB |
Output is correct |
12 |
Correct |
3 ms |
520 KB |
Output is correct |
13 |
Correct |
2 ms |
544 KB |
Output is correct |
14 |
Correct |
2 ms |
544 KB |
Output is correct |
15 |
Correct |
3 ms |
560 KB |
Output is correct |
16 |
Correct |
3 ms |
560 KB |
Output is correct |
17 |
Correct |
3 ms |
560 KB |
Output is correct |
18 |
Correct |
4 ms |
688 KB |
Output is correct |
19 |
Correct |
3 ms |
756 KB |
Output is correct |
20 |
Correct |
3 ms |
756 KB |
Output is correct |
21 |
Correct |
3 ms |
756 KB |
Output is correct |
22 |
Correct |
3 ms |
756 KB |
Output is correct |
23 |
Correct |
4 ms |
756 KB |
Output is correct |
24 |
Correct |
3 ms |
764 KB |
Output is correct |
25 |
Correct |
3 ms |
764 KB |
Output is correct |
26 |
Correct |
3 ms |
764 KB |
Output is correct |
27 |
Correct |
3 ms |
764 KB |
Output is correct |
28 |
Correct |
3 ms |
764 KB |
Output is correct |
29 |
Correct |
4 ms |
764 KB |
Output is correct |
30 |
Correct |
3 ms |
892 KB |
Output is correct |
31 |
Correct |
10 ms |
1660 KB |
Output is correct |
32 |
Correct |
10 ms |
1916 KB |
Output is correct |
33 |
Correct |
16 ms |
2684 KB |
Output is correct |
34 |
Correct |
23 ms |
3452 KB |
Output is correct |
35 |
Correct |
46 ms |
6524 KB |
Output is correct |
36 |
Correct |
43 ms |
6524 KB |
Output is correct |
37 |
Correct |
29 ms |
6524 KB |
Output is correct |
38 |
Correct |
92 ms |
12908 KB |
Output is correct |
39 |
Correct |
40 ms |
12908 KB |
Output is correct |
40 |
Correct |
11 ms |
12908 KB |
Output is correct |
41 |
Correct |
11 ms |
12908 KB |
Output is correct |
42 |
Correct |
11 ms |
12908 KB |
Output is correct |
43 |
Correct |
839 ms |
148928 KB |
Output is correct |
44 |
Correct |
697 ms |
148928 KB |
Output is correct |
45 |
Correct |
712 ms |
148928 KB |
Output is correct |
46 |
Correct |
11 ms |
148928 KB |
Output is correct |
47 |
Correct |
11 ms |
148928 KB |
Output is correct |
48 |
Correct |
12 ms |
148928 KB |
Output is correct |
49 |
Correct |
14 ms |
148928 KB |
Output is correct |
50 |
Correct |
11 ms |
148928 KB |
Output is correct |
51 |
Correct |
11 ms |
148928 KB |
Output is correct |
52 |
Correct |
30 ms |
148928 KB |
Output is correct |
53 |
Correct |
14 ms |
148928 KB |
Output is correct |
54 |
Correct |
14 ms |
148928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
412 KB |
Output is correct |
4 |
Correct |
3 ms |
488 KB |
Output is correct |
5 |
Correct |
2 ms |
520 KB |
Output is correct |
6 |
Correct |
3 ms |
520 KB |
Output is correct |
7 |
Correct |
3 ms |
520 KB |
Output is correct |
8 |
Correct |
3 ms |
520 KB |
Output is correct |
9 |
Correct |
3 ms |
520 KB |
Output is correct |
10 |
Correct |
2 ms |
520 KB |
Output is correct |
11 |
Correct |
3 ms |
520 KB |
Output is correct |
12 |
Correct |
3 ms |
520 KB |
Output is correct |
13 |
Correct |
2 ms |
544 KB |
Output is correct |
14 |
Correct |
2 ms |
544 KB |
Output is correct |
15 |
Correct |
3 ms |
560 KB |
Output is correct |
16 |
Correct |
3 ms |
560 KB |
Output is correct |
17 |
Correct |
3 ms |
560 KB |
Output is correct |
18 |
Correct |
4 ms |
688 KB |
Output is correct |
19 |
Correct |
3 ms |
756 KB |
Output is correct |
20 |
Correct |
3 ms |
756 KB |
Output is correct |
21 |
Correct |
3 ms |
756 KB |
Output is correct |
22 |
Correct |
3 ms |
756 KB |
Output is correct |
23 |
Correct |
4 ms |
756 KB |
Output is correct |
24 |
Correct |
3 ms |
764 KB |
Output is correct |
25 |
Correct |
3 ms |
764 KB |
Output is correct |
26 |
Correct |
3 ms |
764 KB |
Output is correct |
27 |
Correct |
3 ms |
764 KB |
Output is correct |
28 |
Correct |
3 ms |
764 KB |
Output is correct |
29 |
Correct |
4 ms |
764 KB |
Output is correct |
30 |
Correct |
3 ms |
892 KB |
Output is correct |
31 |
Correct |
10 ms |
1660 KB |
Output is correct |
32 |
Correct |
10 ms |
1916 KB |
Output is correct |
33 |
Correct |
16 ms |
2684 KB |
Output is correct |
34 |
Correct |
23 ms |
3452 KB |
Output is correct |
35 |
Correct |
46 ms |
6524 KB |
Output is correct |
36 |
Correct |
43 ms |
6524 KB |
Output is correct |
37 |
Correct |
29 ms |
6524 KB |
Output is correct |
38 |
Correct |
92 ms |
12908 KB |
Output is correct |
39 |
Correct |
40 ms |
12908 KB |
Output is correct |
40 |
Correct |
11 ms |
12908 KB |
Output is correct |
41 |
Correct |
11 ms |
12908 KB |
Output is correct |
42 |
Correct |
11 ms |
12908 KB |
Output is correct |
43 |
Correct |
839 ms |
148928 KB |
Output is correct |
44 |
Correct |
697 ms |
148928 KB |
Output is correct |
45 |
Correct |
712 ms |
148928 KB |
Output is correct |
46 |
Correct |
11 ms |
148928 KB |
Output is correct |
47 |
Correct |
11 ms |
148928 KB |
Output is correct |
48 |
Correct |
12 ms |
148928 KB |
Output is correct |
49 |
Correct |
14 ms |
148928 KB |
Output is correct |
50 |
Correct |
11 ms |
148928 KB |
Output is correct |
51 |
Correct |
11 ms |
148928 KB |
Output is correct |
52 |
Correct |
30 ms |
148928 KB |
Output is correct |
53 |
Correct |
14 ms |
148928 KB |
Output is correct |
54 |
Correct |
14 ms |
148928 KB |
Output is correct |
55 |
Correct |
148 ms |
148928 KB |
Output is correct |
56 |
Execution timed out |
2045 ms |
258332 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |