#include<bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector,Ofast")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll MOD=1e9+7;
const ll MOD2=998244353;
const ll N=1e6+5;
const ld pi=acos(-1);
const ll INF=(1LL<<60);
#define SQ(i) ((i)*(i))
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(_a) (ll)_a.size()
struct data{
ll a,b;
priority_queue<ll> *pq;
data operator+(data B){
data s;
s.a=a+B.a;
s.b=b+B.b;
if(pq->size()>B.pq->size()){
s.pq=pq;
while(!B.pq->empty()){
s.pq->push(B.pq->top());
B.pq->pop();
}
}else{
s.pq=B.pq;
while(!pq->empty()){
s.pq->push(pq->top());
pq->pop();
}
}
return s;
}
}d[N];
ll n,m,p[N],c[N];
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>m;
for(int i=2;i<=n+m;i++){
cin>>p[i]>>c[i];
}
REP1(i,n+m)d[i].a=d[i].b=0,d[i].pq=new priority_queue<ll>;
for(int i=n+m;i>n;i--){
d[i].a=1;
d[i].b=-c[i];
d[i].pq->push(c[i]);
d[i].pq->push(c[i]);
d[p[i]]=d[p[i]]+d[i];
}
for(int i=n;i>1;i--){
while(d[i].a>1){
--d[i].a;
d[i].b+=d[i].pq->top();
d[i].pq->pop();
}
ll tmpa=d[i].pq->top();d[i].pq->pop();
ll tmpb=d[i].pq->top();d[i].pq->pop();
d[i].pq->push(tmpa+c[i]);
d[i].pq->push(tmpb+c[i]);
d[i].b-=c[i];
d[p[i]]=d[p[i]]+d[i];
}
while(d[1].a>0){
--d[1].a;
d[1].b+=d[1].pq->top();
d[1].pq->pop();
}
cout<<d[1].b<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
384 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 |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 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 |
512 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 |
384 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 |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 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 |
512 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 |
492 KB |
Output is correct |
32 |
Correct |
1 ms |
620 KB |
Output is correct |
33 |
Correct |
2 ms |
620 KB |
Output is correct |
34 |
Correct |
2 ms |
748 KB |
Output is correct |
35 |
Correct |
4 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 |
1004 KB |
Output is correct |
39 |
Correct |
3 ms |
1004 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 |
4 ms |
1004 KB |
Output is correct |
46 |
Correct |
3 ms |
1260 KB |
Output is correct |
47 |
Correct |
4 ms |
1388 KB |
Output is correct |
48 |
Correct |
3 ms |
1132 KB |
Output is correct |
49 |
Correct |
3 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 |
1132 KB |
Output is correct |
54 |
Correct |
4 ms |
1132 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 |
384 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 |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 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 |
512 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 |
492 KB |
Output is correct |
32 |
Correct |
1 ms |
620 KB |
Output is correct |
33 |
Correct |
2 ms |
620 KB |
Output is correct |
34 |
Correct |
2 ms |
748 KB |
Output is correct |
35 |
Correct |
4 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 |
1004 KB |
Output is correct |
39 |
Correct |
3 ms |
1004 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 |
4 ms |
1004 KB |
Output is correct |
46 |
Correct |
3 ms |
1260 KB |
Output is correct |
47 |
Correct |
4 ms |
1388 KB |
Output is correct |
48 |
Correct |
3 ms |
1132 KB |
Output is correct |
49 |
Correct |
3 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 |
1132 KB |
Output is correct |
54 |
Correct |
4 ms |
1132 KB |
Output is correct |
55 |
Correct |
9 ms |
2156 KB |
Output is correct |
56 |
Correct |
38 ms |
7276 KB |
Output is correct |
57 |
Correct |
59 ms |
12140 KB |
Output is correct |
58 |
Correct |
68 ms |
15336 KB |
Output is correct |
59 |
Correct |
90 ms |
20200 KB |
Output is correct |
60 |
Correct |
133 ms |
24808 KB |
Output is correct |
61 |
Correct |
145 ms |
28724 KB |
Output is correct |
62 |
Correct |
154 ms |
31492 KB |
Output is correct |
63 |
Correct |
189 ms |
37464 KB |
Output is correct |
64 |
Correct |
236 ms |
39136 KB |
Output is correct |
65 |
Correct |
95 ms |
31084 KB |
Output is correct |
66 |
Correct |
98 ms |
31012 KB |
Output is correct |
67 |
Correct |
102 ms |
31212 KB |
Output is correct |
68 |
Correct |
156 ms |
37860 KB |
Output is correct |
69 |
Correct |
176 ms |
38504 KB |
Output is correct |
70 |
Correct |
176 ms |
38376 KB |
Output is correct |
71 |
Correct |
210 ms |
53104 KB |
Output is correct |
72 |
Correct |
204 ms |
53208 KB |
Output is correct |
73 |
Correct |
173 ms |
49792 KB |
Output is correct |
74 |
Correct |
198 ms |
50476 KB |
Output is correct |
75 |
Correct |
179 ms |
48660 KB |
Output is correct |
76 |
Correct |
184 ms |
48732 KB |
Output is correct |
77 |
Correct |
189 ms |
47704 KB |
Output is correct |
78 |
Correct |
213 ms |
47324 KB |
Output is correct |
79 |
Correct |
176 ms |
46168 KB |
Output is correct |