#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define LL long long
#define st first
#define nd second
#define endl '\n'
using namespace std;
const int MAXN=300005;
int n,m,p[MAXN],c[MAXN];
LL a[MAXN],b[MAXN];
priority_queue<LL> pq[MAXN];
void swap2(int x,int y) {
if(pq[x].size()<pq[y].size()) {
pq[x].swap(pq[y]);
swap(a[x],a[y]);
swap(b[x],b[y]);
}
}
void merge(int x,int y) {
swap2(x,y);
a[x]+=a[y];
b[x]+=b[y];
while(!pq[y].empty()) {
pq[x].push(pq[y].top());
pq[y].pop();
}
}
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];
if(i>n) {
a[i]=1;
b[i]=-c[i];
pq[i].push(c[i]);
pq[i].push(c[i]);
merge(p[i],i);
}
}
for(int i=n;i>1;--i) {
while(a[i]>1) {
--a[i];
b[i]+=pq[i].top();
pq[i].pop();
}
int t1=pq[i].top();
pq[i].pop();
int t2=pq[i].top();
pq[i].pop();
pq[i].push(t2+c[i]);
pq[i].push(t1+c[i]);
b[i]-=c[i];
merge(p[i],i);
}
while(a[1]>1) {
--a[1];
b[1]+=pq[1].top();
pq[1].pop();
}
cout<<pq[1].top()+b[1]<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
18584 KB |
Output is correct |
2 |
Correct |
0 ms |
18584 KB |
Output is correct |
3 |
Correct |
0 ms |
18584 KB |
Output is correct |
4 |
Correct |
0 ms |
18584 KB |
Output is correct |
5 |
Correct |
0 ms |
18584 KB |
Output is correct |
6 |
Correct |
0 ms |
18584 KB |
Output is correct |
7 |
Correct |
3 ms |
18584 KB |
Output is correct |
8 |
Correct |
0 ms |
18584 KB |
Output is correct |
9 |
Correct |
0 ms |
18584 KB |
Output is correct |
10 |
Correct |
3 ms |
18584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
18584 KB |
Output is correct |
2 |
Correct |
6 ms |
18584 KB |
Output is correct |
3 |
Correct |
6 ms |
18584 KB |
Output is correct |
4 |
Correct |
6 ms |
18584 KB |
Output is correct |
5 |
Correct |
3 ms |
18584 KB |
Output is correct |
6 |
Correct |
0 ms |
18584 KB |
Output is correct |
7 |
Correct |
0 ms |
18584 KB |
Output is correct |
8 |
Correct |
6 ms |
18584 KB |
Output is correct |
9 |
Correct |
3 ms |
18584 KB |
Output is correct |
10 |
Correct |
0 ms |
18584 KB |
Output is correct |
11 |
Correct |
0 ms |
18584 KB |
Output is correct |
12 |
Correct |
3 ms |
18584 KB |
Output is correct |
13 |
Correct |
3 ms |
18584 KB |
Output is correct |
14 |
Correct |
3 ms |
18584 KB |
Output is correct |
15 |
Correct |
3 ms |
18584 KB |
Output is correct |
16 |
Correct |
0 ms |
18584 KB |
Output is correct |
17 |
Correct |
3 ms |
18584 KB |
Output is correct |
18 |
Correct |
6 ms |
18584 KB |
Output is correct |
19 |
Correct |
6 ms |
18584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
18584 KB |
Output is correct |
2 |
Correct |
0 ms |
18584 KB |
Output is correct |
3 |
Correct |
0 ms |
18584 KB |
Output is correct |
4 |
Correct |
0 ms |
18584 KB |
Output is correct |
5 |
Correct |
0 ms |
18584 KB |
Output is correct |
6 |
Correct |
0 ms |
18584 KB |
Output is correct |
7 |
Correct |
3 ms |
18584 KB |
Output is correct |
8 |
Correct |
0 ms |
18584 KB |
Output is correct |
9 |
Correct |
0 ms |
18584 KB |
Output is correct |
10 |
Correct |
3 ms |
18584 KB |
Output is correct |
11 |
Correct |
3 ms |
18584 KB |
Output is correct |
12 |
Correct |
6 ms |
18584 KB |
Output is correct |
13 |
Correct |
6 ms |
18584 KB |
Output is correct |
14 |
Correct |
6 ms |
18584 KB |
Output is correct |
15 |
Correct |
3 ms |
18584 KB |
Output is correct |
16 |
Correct |
0 ms |
18584 KB |
Output is correct |
17 |
Correct |
0 ms |
18584 KB |
Output is correct |
18 |
Correct |
6 ms |
18584 KB |
Output is correct |
19 |
Correct |
3 ms |
18584 KB |
Output is correct |
20 |
Correct |
0 ms |
18584 KB |
Output is correct |
21 |
Correct |
0 ms |
18584 KB |
Output is correct |
22 |
Correct |
3 ms |
18584 KB |
Output is correct |
23 |
Correct |
3 ms |
18584 KB |
Output is correct |
24 |
Correct |
3 ms |
18584 KB |
Output is correct |
25 |
Correct |
3 ms |
18584 KB |
Output is correct |
26 |
Correct |
0 ms |
18584 KB |
Output is correct |
27 |
Correct |
3 ms |
18584 KB |
Output is correct |
28 |
Correct |
6 ms |
18584 KB |
Output is correct |
29 |
Correct |
6 ms |
18584 KB |
Output is correct |
30 |
Incorrect |
3 ms |
18584 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
18584 KB |
Output is correct |
2 |
Correct |
0 ms |
18584 KB |
Output is correct |
3 |
Correct |
0 ms |
18584 KB |
Output is correct |
4 |
Correct |
0 ms |
18584 KB |
Output is correct |
5 |
Correct |
0 ms |
18584 KB |
Output is correct |
6 |
Correct |
0 ms |
18584 KB |
Output is correct |
7 |
Correct |
3 ms |
18584 KB |
Output is correct |
8 |
Correct |
0 ms |
18584 KB |
Output is correct |
9 |
Correct |
0 ms |
18584 KB |
Output is correct |
10 |
Correct |
3 ms |
18584 KB |
Output is correct |
11 |
Correct |
3 ms |
18584 KB |
Output is correct |
12 |
Correct |
6 ms |
18584 KB |
Output is correct |
13 |
Correct |
6 ms |
18584 KB |
Output is correct |
14 |
Correct |
6 ms |
18584 KB |
Output is correct |
15 |
Correct |
3 ms |
18584 KB |
Output is correct |
16 |
Correct |
0 ms |
18584 KB |
Output is correct |
17 |
Correct |
0 ms |
18584 KB |
Output is correct |
18 |
Correct |
6 ms |
18584 KB |
Output is correct |
19 |
Correct |
3 ms |
18584 KB |
Output is correct |
20 |
Correct |
0 ms |
18584 KB |
Output is correct |
21 |
Correct |
0 ms |
18584 KB |
Output is correct |
22 |
Correct |
3 ms |
18584 KB |
Output is correct |
23 |
Correct |
3 ms |
18584 KB |
Output is correct |
24 |
Correct |
3 ms |
18584 KB |
Output is correct |
25 |
Correct |
3 ms |
18584 KB |
Output is correct |
26 |
Correct |
0 ms |
18584 KB |
Output is correct |
27 |
Correct |
3 ms |
18584 KB |
Output is correct |
28 |
Correct |
6 ms |
18584 KB |
Output is correct |
29 |
Correct |
6 ms |
18584 KB |
Output is correct |
30 |
Incorrect |
3 ms |
18584 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |