//
// main.cpp
// fireworks
//
// Created by Trần Nam Khánh on 11/21/20.
//
#include <iostream>
#include <queue>
using namespace std;
const long long nax=3e5+1;
struct data
{
long long a,b,p,c;
priority_queue<long long>*q;
data operator +(data tmp)
{
data s;
s.p=p;
s.c=c;
s.a=a+tmp.a;
s.b=b+tmp.b;
if(q->size()<tmp.q->size())swap(q,tmp.q);
s.q=q;
while(!tmp.q->empty())
{
s.q->push(tmp.q->top());
tmp.q->pop();
}
return s;
}
}d[nax];
long long n,m;
int main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>m;
for(long long i=2;i<=n+m;i++)
{
cin>>d[i].p>>d[i].c;
}
for(long long i=1;i<=n+m;i++)
{
d[i].a=0;
d[i].b=0;
d[i].q=new priority_queue<long long>;
}
for(long long i=n+m;i>n;i--)
{
d[i].a=1;
d[i].b=0;
d[i].q->push(0);
d[i].q->push(0);
}
for(long long i=n+m;i>=2;i--)
{
long long p=d[i].p;
while(d[i].a>1)
{
d[i].a--;
d[i].b+=d[i].q->top();
d[i].q->pop();
}
d[i].b-=d[i].c;
long long t1=d[i].q->top();d[i].q->pop();
long long t0=d[i].q->top();d[i].q->pop();
d[i].q->push(t0+d[i].c);
d[i].q->push(t1+d[i].c);
d[p]=d[p]+d[i];
}
while(d[1].a>0)
{
d[1].a--;
d[1].b+=d[1].q->top();
d[1].q->pop();
}
cout<<d[1].b<<endl;
return 0;
}
/*
4 6
1 5
2 5
2 8
3 3
3 2
3 3
2 9
4 4
4 3
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
620 KB |
Output is correct |
32 |
Correct |
2 ms |
640 KB |
Output is correct |
33 |
Correct |
2 ms |
620 KB |
Output is correct |
34 |
Correct |
2 ms |
748 KB |
Output is correct |
35 |
Correct |
3 ms |
876 KB |
Output is correct |
36 |
Correct |
3 ms |
876 KB |
Output is correct |
37 |
Correct |
3 ms |
1004 KB |
Output is correct |
38 |
Correct |
3 ms |
1132 KB |
Output is correct |
39 |
Correct |
3 ms |
1040 KB |
Output is correct |
40 |
Correct |
2 ms |
876 KB |
Output is correct |
41 |
Correct |
2 ms |
876 KB |
Output is correct |
42 |
Correct |
2 ms |
876 KB |
Output is correct |
43 |
Correct |
3 ms |
1004 KB |
Output is correct |
44 |
Correct |
3 ms |
1004 KB |
Output is correct |
45 |
Correct |
3 ms |
1004 KB |
Output is correct |
46 |
Correct |
3 ms |
1260 KB |
Output is correct |
47 |
Correct |
3 ms |
1260 KB |
Output is correct |
48 |
Correct |
4 ms |
1132 KB |
Output is correct |
49 |
Correct |
4 ms |
1132 KB |
Output is correct |
50 |
Correct |
3 ms |
1004 KB |
Output is correct |
51 |
Correct |
3 ms |
1004 KB |
Output is correct |
52 |
Correct |
3 ms |
1132 KB |
Output is correct |
53 |
Correct |
3 ms |
1152 KB |
Output is correct |
54 |
Correct |
3 ms |
1260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
620 KB |
Output is correct |
32 |
Correct |
2 ms |
640 KB |
Output is correct |
33 |
Correct |
2 ms |
620 KB |
Output is correct |
34 |
Correct |
2 ms |
748 KB |
Output is correct |
35 |
Correct |
3 ms |
876 KB |
Output is correct |
36 |
Correct |
3 ms |
876 KB |
Output is correct |
37 |
Correct |
3 ms |
1004 KB |
Output is correct |
38 |
Correct |
3 ms |
1132 KB |
Output is correct |
39 |
Correct |
3 ms |
1040 KB |
Output is correct |
40 |
Correct |
2 ms |
876 KB |
Output is correct |
41 |
Correct |
2 ms |
876 KB |
Output is correct |
42 |
Correct |
2 ms |
876 KB |
Output is correct |
43 |
Correct |
3 ms |
1004 KB |
Output is correct |
44 |
Correct |
3 ms |
1004 KB |
Output is correct |
45 |
Correct |
3 ms |
1004 KB |
Output is correct |
46 |
Correct |
3 ms |
1260 KB |
Output is correct |
47 |
Correct |
3 ms |
1260 KB |
Output is correct |
48 |
Correct |
4 ms |
1132 KB |
Output is correct |
49 |
Correct |
4 ms |
1132 KB |
Output is correct |
50 |
Correct |
3 ms |
1004 KB |
Output is correct |
51 |
Correct |
3 ms |
1004 KB |
Output is correct |
52 |
Correct |
3 ms |
1132 KB |
Output is correct |
53 |
Correct |
3 ms |
1152 KB |
Output is correct |
54 |
Correct |
3 ms |
1260 KB |
Output is correct |
55 |
Correct |
7 ms |
2184 KB |
Output is correct |
56 |
Correct |
29 ms |
7276 KB |
Output is correct |
57 |
Correct |
58 ms |
12268 KB |
Output is correct |
58 |
Correct |
67 ms |
15336 KB |
Output is correct |
59 |
Correct |
93 ms |
20324 KB |
Output is correct |
60 |
Correct |
117 ms |
25192 KB |
Output is correct |
61 |
Correct |
138 ms |
28772 KB |
Output is correct |
62 |
Correct |
158 ms |
31672 KB |
Output is correct |
63 |
Correct |
190 ms |
37720 KB |
Output is correct |
64 |
Correct |
200 ms |
39652 KB |
Output is correct |
65 |
Correct |
96 ms |
31084 KB |
Output is correct |
66 |
Correct |
98 ms |
31084 KB |
Output is correct |
67 |
Correct |
93 ms |
31084 KB |
Output is correct |
68 |
Correct |
161 ms |
38116 KB |
Output is correct |
69 |
Correct |
176 ms |
38880 KB |
Output is correct |
70 |
Correct |
175 ms |
38880 KB |
Output is correct |
71 |
Correct |
213 ms |
53368 KB |
Output is correct |
72 |
Correct |
203 ms |
53336 KB |
Output is correct |
73 |
Correct |
188 ms |
49884 KB |
Output is correct |
74 |
Correct |
191 ms |
50556 KB |
Output is correct |
75 |
Correct |
182 ms |
48604 KB |
Output is correct |
76 |
Correct |
182 ms |
48992 KB |
Output is correct |
77 |
Correct |
198 ms |
48280 KB |
Output is correct |
78 |
Correct |
198 ms |
48092 KB |
Output is correct |
79 |
Correct |
173 ms |
47448 KB |
Output is correct |