#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;
tuple<multiset<ll>,ll> getCost(ll v){ //p1,p2,h
if(e[v].size()==0) return make_tuple(multiset<ll>({0,0}),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);
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()));
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});
}
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:62:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(allPoints.size()>-slope+1)
~~~~~~~~~~~~~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
356 KB |
Output is correct |
3 |
Correct |
3 ms |
432 KB |
Output is correct |
4 |
Correct |
3 ms |
448 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 |
3 ms |
592 KB |
Output is correct |
9 |
Correct |
3 ms |
700 KB |
Output is correct |
10 |
Correct |
3 ms |
700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
700 KB |
Output is correct |
2 |
Correct |
2 ms |
700 KB |
Output is correct |
3 |
Correct |
3 ms |
700 KB |
Output is correct |
4 |
Correct |
3 ms |
700 KB |
Output is correct |
5 |
Correct |
3 ms |
700 KB |
Output is correct |
6 |
Correct |
3 ms |
700 KB |
Output is correct |
7 |
Correct |
2 ms |
700 KB |
Output is correct |
8 |
Correct |
3 ms |
708 KB |
Output is correct |
9 |
Correct |
3 ms |
804 KB |
Output is correct |
10 |
Correct |
3 ms |
804 KB |
Output is correct |
11 |
Correct |
3 ms |
804 KB |
Output is correct |
12 |
Correct |
4 ms |
804 KB |
Output is correct |
13 |
Correct |
4 ms |
804 KB |
Output is correct |
14 |
Correct |
3 ms |
860 KB |
Output is correct |
15 |
Correct |
3 ms |
860 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
4 ms |
860 KB |
Output is correct |
18 |
Correct |
3 ms |
860 KB |
Output is correct |
19 |
Correct |
3 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
356 KB |
Output is correct |
3 |
Correct |
3 ms |
432 KB |
Output is correct |
4 |
Correct |
3 ms |
448 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 |
3 ms |
592 KB |
Output is correct |
9 |
Correct |
3 ms |
700 KB |
Output is correct |
10 |
Correct |
3 ms |
700 KB |
Output is correct |
11 |
Correct |
3 ms |
700 KB |
Output is correct |
12 |
Correct |
2 ms |
700 KB |
Output is correct |
13 |
Correct |
3 ms |
700 KB |
Output is correct |
14 |
Correct |
3 ms |
700 KB |
Output is correct |
15 |
Correct |
3 ms |
700 KB |
Output is correct |
16 |
Correct |
3 ms |
700 KB |
Output is correct |
17 |
Correct |
2 ms |
700 KB |
Output is correct |
18 |
Correct |
3 ms |
708 KB |
Output is correct |
19 |
Correct |
3 ms |
804 KB |
Output is correct |
20 |
Correct |
3 ms |
804 KB |
Output is correct |
21 |
Correct |
3 ms |
804 KB |
Output is correct |
22 |
Correct |
4 ms |
804 KB |
Output is correct |
23 |
Correct |
4 ms |
804 KB |
Output is correct |
24 |
Correct |
3 ms |
860 KB |
Output is correct |
25 |
Correct |
3 ms |
860 KB |
Output is correct |
26 |
Correct |
3 ms |
860 KB |
Output is correct |
27 |
Correct |
4 ms |
860 KB |
Output is correct |
28 |
Correct |
3 ms |
860 KB |
Output is correct |
29 |
Correct |
3 ms |
860 KB |
Output is correct |
30 |
Correct |
4 ms |
860 KB |
Output is correct |
31 |
Correct |
9 ms |
908 KB |
Output is correct |
32 |
Correct |
9 ms |
972 KB |
Output is correct |
33 |
Correct |
10 ms |
1108 KB |
Output is correct |
34 |
Correct |
14 ms |
1140 KB |
Output is correct |
35 |
Correct |
28 ms |
1328 KB |
Output is correct |
36 |
Correct |
25 ms |
1412 KB |
Output is correct |
37 |
Correct |
21 ms |
1472 KB |
Output is correct |
38 |
Correct |
68 ms |
1816 KB |
Output is correct |
39 |
Correct |
29 ms |
1888 KB |
Output is correct |
40 |
Correct |
10 ms |
2980 KB |
Output is correct |
41 |
Correct |
9 ms |
3056 KB |
Output is correct |
42 |
Correct |
11 ms |
3056 KB |
Output is correct |
43 |
Correct |
533 ms |
3056 KB |
Output is correct |
44 |
Correct |
439 ms |
3056 KB |
Output is correct |
45 |
Correct |
348 ms |
3056 KB |
Output is correct |
46 |
Correct |
9 ms |
3056 KB |
Output is correct |
47 |
Correct |
13 ms |
3056 KB |
Output is correct |
48 |
Correct |
9 ms |
3056 KB |
Output is correct |
49 |
Correct |
13 ms |
3056 KB |
Output is correct |
50 |
Correct |
9 ms |
3056 KB |
Output is correct |
51 |
Correct |
14 ms |
3056 KB |
Output is correct |
52 |
Correct |
21 ms |
3056 KB |
Output is correct |
53 |
Correct |
13 ms |
3056 KB |
Output is correct |
54 |
Correct |
14 ms |
3056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
356 KB |
Output is correct |
3 |
Correct |
3 ms |
432 KB |
Output is correct |
4 |
Correct |
3 ms |
448 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 |
3 ms |
592 KB |
Output is correct |
9 |
Correct |
3 ms |
700 KB |
Output is correct |
10 |
Correct |
3 ms |
700 KB |
Output is correct |
11 |
Correct |
3 ms |
700 KB |
Output is correct |
12 |
Correct |
2 ms |
700 KB |
Output is correct |
13 |
Correct |
3 ms |
700 KB |
Output is correct |
14 |
Correct |
3 ms |
700 KB |
Output is correct |
15 |
Correct |
3 ms |
700 KB |
Output is correct |
16 |
Correct |
3 ms |
700 KB |
Output is correct |
17 |
Correct |
2 ms |
700 KB |
Output is correct |
18 |
Correct |
3 ms |
708 KB |
Output is correct |
19 |
Correct |
3 ms |
804 KB |
Output is correct |
20 |
Correct |
3 ms |
804 KB |
Output is correct |
21 |
Correct |
3 ms |
804 KB |
Output is correct |
22 |
Correct |
4 ms |
804 KB |
Output is correct |
23 |
Correct |
4 ms |
804 KB |
Output is correct |
24 |
Correct |
3 ms |
860 KB |
Output is correct |
25 |
Correct |
3 ms |
860 KB |
Output is correct |
26 |
Correct |
3 ms |
860 KB |
Output is correct |
27 |
Correct |
4 ms |
860 KB |
Output is correct |
28 |
Correct |
3 ms |
860 KB |
Output is correct |
29 |
Correct |
3 ms |
860 KB |
Output is correct |
30 |
Correct |
4 ms |
860 KB |
Output is correct |
31 |
Correct |
9 ms |
908 KB |
Output is correct |
32 |
Correct |
9 ms |
972 KB |
Output is correct |
33 |
Correct |
10 ms |
1108 KB |
Output is correct |
34 |
Correct |
14 ms |
1140 KB |
Output is correct |
35 |
Correct |
28 ms |
1328 KB |
Output is correct |
36 |
Correct |
25 ms |
1412 KB |
Output is correct |
37 |
Correct |
21 ms |
1472 KB |
Output is correct |
38 |
Correct |
68 ms |
1816 KB |
Output is correct |
39 |
Correct |
29 ms |
1888 KB |
Output is correct |
40 |
Correct |
10 ms |
2980 KB |
Output is correct |
41 |
Correct |
9 ms |
3056 KB |
Output is correct |
42 |
Correct |
11 ms |
3056 KB |
Output is correct |
43 |
Correct |
533 ms |
3056 KB |
Output is correct |
44 |
Correct |
439 ms |
3056 KB |
Output is correct |
45 |
Correct |
348 ms |
3056 KB |
Output is correct |
46 |
Correct |
9 ms |
3056 KB |
Output is correct |
47 |
Correct |
13 ms |
3056 KB |
Output is correct |
48 |
Correct |
9 ms |
3056 KB |
Output is correct |
49 |
Correct |
13 ms |
3056 KB |
Output is correct |
50 |
Correct |
9 ms |
3056 KB |
Output is correct |
51 |
Correct |
14 ms |
3056 KB |
Output is correct |
52 |
Correct |
21 ms |
3056 KB |
Output is correct |
53 |
Correct |
13 ms |
3056 KB |
Output is correct |
54 |
Correct |
14 ms |
3056 KB |
Output is correct |
55 |
Correct |
158 ms |
3772 KB |
Output is correct |
56 |
Correct |
1293 ms |
7616 KB |
Output is correct |
57 |
Execution timed out |
2063 ms |
11736 KB |
Time limit exceeded |
58 |
Halted |
0 ms |
0 KB |
- |