#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=3e5+10;
int n, m;
priority_queue<int> pq[N];
int a[N], b[N], p[N], c[N];
void merge(int u, int v){
if (pq[u].size()<pq[v].size()) pq[u].swap(pq[v]), swap(a[u], a[v]), swap(b[u], b[v]);
while (pq[v].size()) pq[u].push(pq[v].top()), pq[v].pop();
a[u]+=a[v];
b[u]+=b[v];
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i=2; i<=m+n; ++i){
cin >> p[i] >> c[i];
}
for (int i=n+m; i>n; --i){
pq[i].push(c[i]);
pq[i].push(c[i]);
a[i]=1; b[i]=-c[i];
merge(p[i], i);
}
for (int i=n; i>=2; --i){
while (a[i]>1){
b[i]+=pq[i].top();
pq[i].pop();
--a[i];
}
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]>0){
b[1]+=pq[1].top();
pq[1].pop();
--a[1];
}
cout << b[1] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
4 ms |
16988 KB |
Output is correct |
3 |
Correct |
4 ms |
16988 KB |
Output is correct |
4 |
Correct |
4 ms |
16988 KB |
Output is correct |
5 |
Correct |
3 ms |
16988 KB |
Output is correct |
6 |
Correct |
4 ms |
16984 KB |
Output is correct |
7 |
Correct |
3 ms |
16988 KB |
Output is correct |
8 |
Correct |
3 ms |
17008 KB |
Output is correct |
9 |
Correct |
3 ms |
16988 KB |
Output is correct |
10 |
Correct |
5 ms |
16988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16984 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
3 ms |
16984 KB |
Output is correct |
4 |
Correct |
3 ms |
16988 KB |
Output is correct |
5 |
Correct |
3 ms |
17012 KB |
Output is correct |
6 |
Correct |
3 ms |
16988 KB |
Output is correct |
7 |
Correct |
3 ms |
17028 KB |
Output is correct |
8 |
Correct |
3 ms |
16984 KB |
Output is correct |
9 |
Correct |
3 ms |
16988 KB |
Output is correct |
10 |
Correct |
3 ms |
16988 KB |
Output is correct |
11 |
Correct |
3 ms |
16988 KB |
Output is correct |
12 |
Correct |
3 ms |
16988 KB |
Output is correct |
13 |
Correct |
3 ms |
16984 KB |
Output is correct |
14 |
Correct |
3 ms |
16988 KB |
Output is correct |
15 |
Correct |
3 ms |
16984 KB |
Output is correct |
16 |
Correct |
3 ms |
16988 KB |
Output is correct |
17 |
Correct |
3 ms |
16988 KB |
Output is correct |
18 |
Correct |
3 ms |
17044 KB |
Output is correct |
19 |
Correct |
3 ms |
16988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
4 ms |
16988 KB |
Output is correct |
3 |
Correct |
4 ms |
16988 KB |
Output is correct |
4 |
Correct |
4 ms |
16988 KB |
Output is correct |
5 |
Correct |
3 ms |
16988 KB |
Output is correct |
6 |
Correct |
4 ms |
16984 KB |
Output is correct |
7 |
Correct |
3 ms |
16988 KB |
Output is correct |
8 |
Correct |
3 ms |
17008 KB |
Output is correct |
9 |
Correct |
3 ms |
16988 KB |
Output is correct |
10 |
Correct |
5 ms |
16988 KB |
Output is correct |
11 |
Correct |
3 ms |
16984 KB |
Output is correct |
12 |
Correct |
3 ms |
16988 KB |
Output is correct |
13 |
Correct |
3 ms |
16984 KB |
Output is correct |
14 |
Correct |
3 ms |
16988 KB |
Output is correct |
15 |
Correct |
3 ms |
17012 KB |
Output is correct |
16 |
Correct |
3 ms |
16988 KB |
Output is correct |
17 |
Correct |
3 ms |
17028 KB |
Output is correct |
18 |
Correct |
3 ms |
16984 KB |
Output is correct |
19 |
Correct |
3 ms |
16988 KB |
Output is correct |
20 |
Correct |
3 ms |
16988 KB |
Output is correct |
21 |
Correct |
3 ms |
16988 KB |
Output is correct |
22 |
Correct |
3 ms |
16988 KB |
Output is correct |
23 |
Correct |
3 ms |
16984 KB |
Output is correct |
24 |
Correct |
3 ms |
16988 KB |
Output is correct |
25 |
Correct |
3 ms |
16984 KB |
Output is correct |
26 |
Correct |
3 ms |
16988 KB |
Output is correct |
27 |
Correct |
3 ms |
16988 KB |
Output is correct |
28 |
Correct |
3 ms |
17044 KB |
Output is correct |
29 |
Correct |
3 ms |
16988 KB |
Output is correct |
30 |
Correct |
3 ms |
16988 KB |
Output is correct |
31 |
Correct |
3 ms |
16988 KB |
Output is correct |
32 |
Correct |
3 ms |
16988 KB |
Output is correct |
33 |
Correct |
4 ms |
16988 KB |
Output is correct |
34 |
Correct |
4 ms |
16984 KB |
Output is correct |
35 |
Correct |
4 ms |
16988 KB |
Output is correct |
36 |
Correct |
4 ms |
17196 KB |
Output is correct |
37 |
Correct |
4 ms |
16988 KB |
Output is correct |
38 |
Correct |
5 ms |
17060 KB |
Output is correct |
39 |
Correct |
6 ms |
16988 KB |
Output is correct |
40 |
Correct |
4 ms |
16984 KB |
Output is correct |
41 |
Correct |
4 ms |
16988 KB |
Output is correct |
42 |
Correct |
4 ms |
16988 KB |
Output is correct |
43 |
Correct |
4 ms |
16988 KB |
Output is correct |
44 |
Correct |
5 ms |
16988 KB |
Output is correct |
45 |
Correct |
4 ms |
16988 KB |
Output is correct |
46 |
Correct |
6 ms |
17240 KB |
Output is correct |
47 |
Correct |
5 ms |
17244 KB |
Output is correct |
48 |
Correct |
5 ms |
17244 KB |
Output is correct |
49 |
Correct |
5 ms |
17244 KB |
Output is correct |
50 |
Correct |
4 ms |
17244 KB |
Output is correct |
51 |
Correct |
6 ms |
17244 KB |
Output is correct |
52 |
Correct |
5 ms |
17244 KB |
Output is correct |
53 |
Correct |
4 ms |
17248 KB |
Output is correct |
54 |
Correct |
5 ms |
17244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
4 ms |
16988 KB |
Output is correct |
3 |
Correct |
4 ms |
16988 KB |
Output is correct |
4 |
Correct |
4 ms |
16988 KB |
Output is correct |
5 |
Correct |
3 ms |
16988 KB |
Output is correct |
6 |
Correct |
4 ms |
16984 KB |
Output is correct |
7 |
Correct |
3 ms |
16988 KB |
Output is correct |
8 |
Correct |
3 ms |
17008 KB |
Output is correct |
9 |
Correct |
3 ms |
16988 KB |
Output is correct |
10 |
Correct |
5 ms |
16988 KB |
Output is correct |
11 |
Correct |
3 ms |
16984 KB |
Output is correct |
12 |
Correct |
3 ms |
16988 KB |
Output is correct |
13 |
Correct |
3 ms |
16984 KB |
Output is correct |
14 |
Correct |
3 ms |
16988 KB |
Output is correct |
15 |
Correct |
3 ms |
17012 KB |
Output is correct |
16 |
Correct |
3 ms |
16988 KB |
Output is correct |
17 |
Correct |
3 ms |
17028 KB |
Output is correct |
18 |
Correct |
3 ms |
16984 KB |
Output is correct |
19 |
Correct |
3 ms |
16988 KB |
Output is correct |
20 |
Correct |
3 ms |
16988 KB |
Output is correct |
21 |
Correct |
3 ms |
16988 KB |
Output is correct |
22 |
Correct |
3 ms |
16988 KB |
Output is correct |
23 |
Correct |
3 ms |
16984 KB |
Output is correct |
24 |
Correct |
3 ms |
16988 KB |
Output is correct |
25 |
Correct |
3 ms |
16984 KB |
Output is correct |
26 |
Correct |
3 ms |
16988 KB |
Output is correct |
27 |
Correct |
3 ms |
16988 KB |
Output is correct |
28 |
Correct |
3 ms |
17044 KB |
Output is correct |
29 |
Correct |
3 ms |
16988 KB |
Output is correct |
30 |
Correct |
3 ms |
16988 KB |
Output is correct |
31 |
Correct |
3 ms |
16988 KB |
Output is correct |
32 |
Correct |
3 ms |
16988 KB |
Output is correct |
33 |
Correct |
4 ms |
16988 KB |
Output is correct |
34 |
Correct |
4 ms |
16984 KB |
Output is correct |
35 |
Correct |
4 ms |
16988 KB |
Output is correct |
36 |
Correct |
4 ms |
17196 KB |
Output is correct |
37 |
Correct |
4 ms |
16988 KB |
Output is correct |
38 |
Correct |
5 ms |
17060 KB |
Output is correct |
39 |
Correct |
6 ms |
16988 KB |
Output is correct |
40 |
Correct |
4 ms |
16984 KB |
Output is correct |
41 |
Correct |
4 ms |
16988 KB |
Output is correct |
42 |
Correct |
4 ms |
16988 KB |
Output is correct |
43 |
Correct |
4 ms |
16988 KB |
Output is correct |
44 |
Correct |
5 ms |
16988 KB |
Output is correct |
45 |
Correct |
4 ms |
16988 KB |
Output is correct |
46 |
Correct |
6 ms |
17240 KB |
Output is correct |
47 |
Correct |
5 ms |
17244 KB |
Output is correct |
48 |
Correct |
5 ms |
17244 KB |
Output is correct |
49 |
Correct |
5 ms |
17244 KB |
Output is correct |
50 |
Correct |
4 ms |
17244 KB |
Output is correct |
51 |
Correct |
6 ms |
17244 KB |
Output is correct |
52 |
Correct |
5 ms |
17244 KB |
Output is correct |
53 |
Correct |
4 ms |
17248 KB |
Output is correct |
54 |
Correct |
5 ms |
17244 KB |
Output is correct |
55 |
Correct |
7 ms |
17496 KB |
Output is correct |
56 |
Correct |
18 ms |
21088 KB |
Output is correct |
57 |
Correct |
31 ms |
22300 KB |
Output is correct |
58 |
Correct |
41 ms |
23212 KB |
Output is correct |
59 |
Correct |
52 ms |
26520 KB |
Output is correct |
60 |
Correct |
63 ms |
27596 KB |
Output is correct |
61 |
Correct |
72 ms |
29448 KB |
Output is correct |
62 |
Correct |
85 ms |
30720 KB |
Output is correct |
63 |
Correct |
100 ms |
33272 KB |
Output is correct |
64 |
Correct |
132 ms |
33928 KB |
Output is correct |
65 |
Correct |
48 ms |
23936 KB |
Output is correct |
66 |
Correct |
47 ms |
24080 KB |
Output is correct |
67 |
Correct |
47 ms |
24144 KB |
Output is correct |
68 |
Correct |
96 ms |
31076 KB |
Output is correct |
69 |
Correct |
110 ms |
31428 KB |
Output is correct |
70 |
Correct |
104 ms |
31428 KB |
Output is correct |
71 |
Correct |
132 ms |
46560 KB |
Output is correct |
72 |
Correct |
140 ms |
47448 KB |
Output is correct |
73 |
Correct |
117 ms |
44020 KB |
Output is correct |
74 |
Correct |
120 ms |
45180 KB |
Output is correct |
75 |
Correct |
115 ms |
42992 KB |
Output is correct |
76 |
Correct |
124 ms |
43496 KB |
Output is correct |
77 |
Correct |
126 ms |
41448 KB |
Output is correct |
78 |
Correct |
123 ms |
42432 KB |
Output is correct |
79 |
Correct |
105 ms |
40892 KB |
Output is correct |