#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<ll,ll> getCost(ll v){ //p1,p2,h
if(e[v].size()==0) {
ms.push_back(multiset<ll>({0,0}));
return make_tuple(ms.size()-1,0);
}
//ll slope = 0;
// vector<pii> events;
ll allPoints;
vector<ll> allChildpoints;
ll slope = 0;
ll costAllZero = 0;
rep(i,0,e[v].size()){
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(ms[points].end());
ll l2 = *prev(prev(ms[points].end()));
ms[points].erase(prev(ms[points].end()));
ms[points].erase(prev(ms[points].end()));
l1+=e[v][i].second;
l2+=e[v][i].second;
ms[points].insert(l1);
ms[points].insert(l2);
costZero += e[v][i].second;
slope -= ms[points].size()-1;
costAllZero += costZero;
allChildpoints.push_back(points);
/*cout<<"A"<<e[v][i].first<<endl;
cout<<allChildpoints.size()<<endl;
rep(i,0,allChildpoints.size()){
cout<<i<<": "<<endl;
cout<<ms[allChildpoints[0]].size()<<endl;
}*/
/*if(v==1){
cout<<e[v][i].first<<": "<<endl;
cout<<"costZero: "<<costZero<<endl;
trav(p,points){
cout<<p<<" ";
}
cout<<endl;
}*/
//allChildcosts.emplace(points,h);
}
ll mx = 0;
ll mxIndex = 0;
rep(i,0,allChildpoints.size()){
//cout<<i<<" "<<endl;
//cout<<allChildpoints.size()<<" "<<endl;
//cout<<ms[allChildpoints[i]].size()<<endl;
if(ms[allChildpoints[i]].size()>mx){
mx = ms[allChildpoints[i]].size();
mxIndex = i;
}
}
allPoints = allChildpoints[mxIndex];
rep(i,0,allChildpoints.size()){
if(i==mxIndex) continue;
trav(p,ms[allChildpoints[i]]) ms[allPoints].insert(p);
}
while(ms[allPoints].size()>-slope+1)
ms[allPoints].erase(prev(ms[allPoints].end()));
return make_tuple(allPoints,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});
}
ll points;
ll costZero;
tie(points,costZero) = getCost(0);
ll last = 0;
ll ans = 1e18;
ll cost = costZero;
ll slope = -(ms[points].size()-1);
//cout<<"costZero: "<<costZero<<endl;
trav(p,ms[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<long long int, long long int> getCost(ll)':
fireworks.cpp:82:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(ms[allChildpoints[i]].size()>mx){
~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
fireworks.cpp:95:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(ms[allPoints].size()>-slope+1)
~~~~~~~~~~~~~~~~~~~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
496 KB |
Output is correct |
3 |
Correct |
3 ms |
496 KB |
Output is correct |
4 |
Correct |
3 ms |
496 KB |
Output is correct |
5 |
Correct |
3 ms |
496 KB |
Output is correct |
6 |
Correct |
2 ms |
496 KB |
Output is correct |
7 |
Correct |
3 ms |
672 KB |
Output is correct |
8 |
Correct |
3 ms |
672 KB |
Output is correct |
9 |
Correct |
3 ms |
672 KB |
Output is correct |
10 |
Correct |
2 ms |
672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
672 KB |
Output is correct |
2 |
Correct |
2 ms |
672 KB |
Output is correct |
3 |
Correct |
3 ms |
672 KB |
Output is correct |
4 |
Correct |
3 ms |
672 KB |
Output is correct |
5 |
Correct |
2 ms |
672 KB |
Output is correct |
6 |
Correct |
3 ms |
672 KB |
Output is correct |
7 |
Correct |
4 ms |
672 KB |
Output is correct |
8 |
Correct |
2 ms |
672 KB |
Output is correct |
9 |
Correct |
2 ms |
672 KB |
Output is correct |
10 |
Correct |
3 ms |
672 KB |
Output is correct |
11 |
Correct |
4 ms |
672 KB |
Output is correct |
12 |
Correct |
2 ms |
672 KB |
Output is correct |
13 |
Correct |
3 ms |
672 KB |
Output is correct |
14 |
Correct |
3 ms |
672 KB |
Output is correct |
15 |
Correct |
4 ms |
676 KB |
Output is correct |
16 |
Correct |
3 ms |
676 KB |
Output is correct |
17 |
Correct |
3 ms |
676 KB |
Output is correct |
18 |
Correct |
3 ms |
676 KB |
Output is correct |
19 |
Correct |
4 ms |
676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
496 KB |
Output is correct |
3 |
Correct |
3 ms |
496 KB |
Output is correct |
4 |
Correct |
3 ms |
496 KB |
Output is correct |
5 |
Correct |
3 ms |
496 KB |
Output is correct |
6 |
Correct |
2 ms |
496 KB |
Output is correct |
7 |
Correct |
3 ms |
672 KB |
Output is correct |
8 |
Correct |
3 ms |
672 KB |
Output is correct |
9 |
Correct |
3 ms |
672 KB |
Output is correct |
10 |
Correct |
2 ms |
672 KB |
Output is correct |
11 |
Correct |
3 ms |
672 KB |
Output is correct |
12 |
Correct |
2 ms |
672 KB |
Output is correct |
13 |
Correct |
3 ms |
672 KB |
Output is correct |
14 |
Correct |
3 ms |
672 KB |
Output is correct |
15 |
Correct |
2 ms |
672 KB |
Output is correct |
16 |
Correct |
3 ms |
672 KB |
Output is correct |
17 |
Correct |
4 ms |
672 KB |
Output is correct |
18 |
Correct |
2 ms |
672 KB |
Output is correct |
19 |
Correct |
2 ms |
672 KB |
Output is correct |
20 |
Correct |
3 ms |
672 KB |
Output is correct |
21 |
Correct |
4 ms |
672 KB |
Output is correct |
22 |
Correct |
2 ms |
672 KB |
Output is correct |
23 |
Correct |
3 ms |
672 KB |
Output is correct |
24 |
Correct |
3 ms |
672 KB |
Output is correct |
25 |
Correct |
4 ms |
676 KB |
Output is correct |
26 |
Correct |
3 ms |
676 KB |
Output is correct |
27 |
Correct |
3 ms |
676 KB |
Output is correct |
28 |
Correct |
3 ms |
676 KB |
Output is correct |
29 |
Correct |
4 ms |
676 KB |
Output is correct |
30 |
Correct |
3 ms |
676 KB |
Output is correct |
31 |
Correct |
3 ms |
748 KB |
Output is correct |
32 |
Correct |
4 ms |
748 KB |
Output is correct |
33 |
Correct |
4 ms |
880 KB |
Output is correct |
34 |
Correct |
6 ms |
1004 KB |
Output is correct |
35 |
Correct |
9 ms |
1132 KB |
Output is correct |
36 |
Correct |
9 ms |
1260 KB |
Output is correct |
37 |
Correct |
10 ms |
1260 KB |
Output is correct |
38 |
Correct |
10 ms |
1388 KB |
Output is correct |
39 |
Correct |
9 ms |
1388 KB |
Output is correct |
40 |
Correct |
5 ms |
1772 KB |
Output is correct |
41 |
Correct |
6 ms |
1772 KB |
Output is correct |
42 |
Correct |
6 ms |
1772 KB |
Output is correct |
43 |
Correct |
7 ms |
1772 KB |
Output is correct |
44 |
Correct |
10 ms |
1772 KB |
Output is correct |
45 |
Correct |
9 ms |
1772 KB |
Output is correct |
46 |
Correct |
12 ms |
1976 KB |
Output is correct |
47 |
Correct |
11 ms |
1976 KB |
Output is correct |
48 |
Correct |
10 ms |
1976 KB |
Output is correct |
49 |
Correct |
11 ms |
1976 KB |
Output is correct |
50 |
Correct |
8 ms |
1976 KB |
Output is correct |
51 |
Correct |
9 ms |
1976 KB |
Output is correct |
52 |
Correct |
9 ms |
1976 KB |
Output is correct |
53 |
Correct |
12 ms |
1976 KB |
Output is correct |
54 |
Correct |
10 ms |
1980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
496 KB |
Output is correct |
3 |
Correct |
3 ms |
496 KB |
Output is correct |
4 |
Correct |
3 ms |
496 KB |
Output is correct |
5 |
Correct |
3 ms |
496 KB |
Output is correct |
6 |
Correct |
2 ms |
496 KB |
Output is correct |
7 |
Correct |
3 ms |
672 KB |
Output is correct |
8 |
Correct |
3 ms |
672 KB |
Output is correct |
9 |
Correct |
3 ms |
672 KB |
Output is correct |
10 |
Correct |
2 ms |
672 KB |
Output is correct |
11 |
Correct |
3 ms |
672 KB |
Output is correct |
12 |
Correct |
2 ms |
672 KB |
Output is correct |
13 |
Correct |
3 ms |
672 KB |
Output is correct |
14 |
Correct |
3 ms |
672 KB |
Output is correct |
15 |
Correct |
2 ms |
672 KB |
Output is correct |
16 |
Correct |
3 ms |
672 KB |
Output is correct |
17 |
Correct |
4 ms |
672 KB |
Output is correct |
18 |
Correct |
2 ms |
672 KB |
Output is correct |
19 |
Correct |
2 ms |
672 KB |
Output is correct |
20 |
Correct |
3 ms |
672 KB |
Output is correct |
21 |
Correct |
4 ms |
672 KB |
Output is correct |
22 |
Correct |
2 ms |
672 KB |
Output is correct |
23 |
Correct |
3 ms |
672 KB |
Output is correct |
24 |
Correct |
3 ms |
672 KB |
Output is correct |
25 |
Correct |
4 ms |
676 KB |
Output is correct |
26 |
Correct |
3 ms |
676 KB |
Output is correct |
27 |
Correct |
3 ms |
676 KB |
Output is correct |
28 |
Correct |
3 ms |
676 KB |
Output is correct |
29 |
Correct |
4 ms |
676 KB |
Output is correct |
30 |
Correct |
3 ms |
676 KB |
Output is correct |
31 |
Correct |
3 ms |
748 KB |
Output is correct |
32 |
Correct |
4 ms |
748 KB |
Output is correct |
33 |
Correct |
4 ms |
880 KB |
Output is correct |
34 |
Correct |
6 ms |
1004 KB |
Output is correct |
35 |
Correct |
9 ms |
1132 KB |
Output is correct |
36 |
Correct |
9 ms |
1260 KB |
Output is correct |
37 |
Correct |
10 ms |
1260 KB |
Output is correct |
38 |
Correct |
10 ms |
1388 KB |
Output is correct |
39 |
Correct |
9 ms |
1388 KB |
Output is correct |
40 |
Correct |
5 ms |
1772 KB |
Output is correct |
41 |
Correct |
6 ms |
1772 KB |
Output is correct |
42 |
Correct |
6 ms |
1772 KB |
Output is correct |
43 |
Correct |
7 ms |
1772 KB |
Output is correct |
44 |
Correct |
10 ms |
1772 KB |
Output is correct |
45 |
Correct |
9 ms |
1772 KB |
Output is correct |
46 |
Correct |
12 ms |
1976 KB |
Output is correct |
47 |
Correct |
11 ms |
1976 KB |
Output is correct |
48 |
Correct |
10 ms |
1976 KB |
Output is correct |
49 |
Correct |
11 ms |
1976 KB |
Output is correct |
50 |
Correct |
8 ms |
1976 KB |
Output is correct |
51 |
Correct |
9 ms |
1976 KB |
Output is correct |
52 |
Correct |
9 ms |
1976 KB |
Output is correct |
53 |
Correct |
12 ms |
1976 KB |
Output is correct |
54 |
Correct |
10 ms |
1980 KB |
Output is correct |
55 |
Correct |
25 ms |
2724 KB |
Output is correct |
56 |
Correct |
81 ms |
9012 KB |
Output is correct |
57 |
Correct |
161 ms |
14768 KB |
Output is correct |
58 |
Correct |
238 ms |
20624 KB |
Output is correct |
59 |
Correct |
292 ms |
28968 KB |
Output is correct |
60 |
Correct |
443 ms |
37844 KB |
Output is correct |
61 |
Correct |
443 ms |
45108 KB |
Output is correct |
62 |
Correct |
451 ms |
52796 KB |
Output is correct |
63 |
Correct |
502 ms |
65228 KB |
Output is correct |
64 |
Correct |
594 ms |
70664 KB |
Output is correct |
65 |
Correct |
304 ms |
100408 KB |
Output is correct |
66 |
Correct |
249 ms |
105296 KB |
Output is correct |
67 |
Correct |
291 ms |
105296 KB |
Output is correct |
68 |
Correct |
487 ms |
115236 KB |
Output is correct |
69 |
Correct |
651 ms |
115908 KB |
Output is correct |
70 |
Correct |
700 ms |
120732 KB |
Output is correct |
71 |
Correct |
905 ms |
152048 KB |
Output is correct |
72 |
Correct |
962 ms |
156468 KB |
Output is correct |
73 |
Correct |
811 ms |
156468 KB |
Output is correct |
74 |
Correct |
866 ms |
156468 KB |
Output is correct |
75 |
Correct |
888 ms |
156468 KB |
Output is correct |
76 |
Correct |
912 ms |
158372 KB |
Output is correct |
77 |
Correct |
1022 ms |
159288 KB |
Output is correct |
78 |
Correct |
935 ms |
163148 KB |
Output is correct |
79 |
Correct |
1101 ms |
172120 KB |
Output is correct |