#include <bits/stdc++.h>
using namespace std;
struct Node {
long long m = 0, c = 0;
priority_queue<long long> pq;
void Merge(Node &o) {
m += o.m;
c += o.c;
if (pq.size() < o.pq.size()) {
swap(pq, o.pq);
}
while (!o.pq.empty()) {
pq.emplace(o.pq.top());
o.pq.pop();
}
}
void Process(long long C) {
while (m > 1) {
m--;
c += pq.top();
pq.pop();
}
long long u = pq.top(); pq.pop();
long long v = pq.top(); pq.pop();
c -= C;
pq.emplace(v + C);
pq.emplace(u + C);
}
long long Evaluate() {
return m * pq.top() + c;
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int N, M;
cin >> N >> M;
vector<int> P(N + M), C(N + M);
for (int i = 1; i < N + M; i++) {
cin >> P[i] >> C[i];
}
vector<Node> node(N + M);
for (int i = N + M - 1; i >= 0; i--) {
if (i >= N) {
node[i].m = 1;
node[i].c = -C[i];
node[i].pq.emplace(C[i]);
node[i].pq.emplace(C[i]);
} else {
node[i].Process(C[i]);
}
if (i > 0) {
node[--P[i]].Merge(node[i]);
}
}
cout << node[0].Evaluate() << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
256 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
256 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
7 ms |
512 KB |
Output is correct |
33 |
Correct |
6 ms |
512 KB |
Output is correct |
34 |
Correct |
6 ms |
640 KB |
Output is correct |
35 |
Correct |
7 ms |
768 KB |
Output is correct |
36 |
Correct |
7 ms |
768 KB |
Output is correct |
37 |
Correct |
7 ms |
896 KB |
Output is correct |
38 |
Correct |
7 ms |
896 KB |
Output is correct |
39 |
Correct |
8 ms |
896 KB |
Output is correct |
40 |
Correct |
7 ms |
640 KB |
Output is correct |
41 |
Correct |
7 ms |
640 KB |
Output is correct |
42 |
Correct |
6 ms |
768 KB |
Output is correct |
43 |
Correct |
7 ms |
768 KB |
Output is correct |
44 |
Correct |
7 ms |
896 KB |
Output is correct |
45 |
Correct |
7 ms |
896 KB |
Output is correct |
46 |
Correct |
8 ms |
1024 KB |
Output is correct |
47 |
Correct |
8 ms |
1024 KB |
Output is correct |
48 |
Correct |
7 ms |
896 KB |
Output is correct |
49 |
Correct |
7 ms |
1024 KB |
Output is correct |
50 |
Correct |
7 ms |
896 KB |
Output is correct |
51 |
Correct |
7 ms |
896 KB |
Output is correct |
52 |
Correct |
7 ms |
896 KB |
Output is correct |
53 |
Correct |
7 ms |
896 KB |
Output is correct |
54 |
Correct |
7 ms |
896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
256 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
7 ms |
512 KB |
Output is correct |
33 |
Correct |
6 ms |
512 KB |
Output is correct |
34 |
Correct |
6 ms |
640 KB |
Output is correct |
35 |
Correct |
7 ms |
768 KB |
Output is correct |
36 |
Correct |
7 ms |
768 KB |
Output is correct |
37 |
Correct |
7 ms |
896 KB |
Output is correct |
38 |
Correct |
7 ms |
896 KB |
Output is correct |
39 |
Correct |
8 ms |
896 KB |
Output is correct |
40 |
Correct |
7 ms |
640 KB |
Output is correct |
41 |
Correct |
7 ms |
640 KB |
Output is correct |
42 |
Correct |
6 ms |
768 KB |
Output is correct |
43 |
Correct |
7 ms |
768 KB |
Output is correct |
44 |
Correct |
7 ms |
896 KB |
Output is correct |
45 |
Correct |
7 ms |
896 KB |
Output is correct |
46 |
Correct |
8 ms |
1024 KB |
Output is correct |
47 |
Correct |
8 ms |
1024 KB |
Output is correct |
48 |
Correct |
7 ms |
896 KB |
Output is correct |
49 |
Correct |
7 ms |
1024 KB |
Output is correct |
50 |
Correct |
7 ms |
896 KB |
Output is correct |
51 |
Correct |
7 ms |
896 KB |
Output is correct |
52 |
Correct |
7 ms |
896 KB |
Output is correct |
53 |
Correct |
7 ms |
896 KB |
Output is correct |
54 |
Correct |
7 ms |
896 KB |
Output is correct |
55 |
Correct |
12 ms |
1664 KB |
Output is correct |
56 |
Correct |
33 ms |
5880 KB |
Output is correct |
57 |
Correct |
52 ms |
9340 KB |
Output is correct |
58 |
Correct |
63 ms |
11892 KB |
Output is correct |
59 |
Correct |
84 ms |
15600 KB |
Output is correct |
60 |
Correct |
108 ms |
19316 KB |
Output is correct |
61 |
Correct |
121 ms |
22384 KB |
Output is correct |
62 |
Correct |
153 ms |
24428 KB |
Output is correct |
63 |
Correct |
165 ms |
29156 KB |
Output is correct |
64 |
Correct |
175 ms |
30448 KB |
Output is correct |
65 |
Correct |
99 ms |
21624 KB |
Output is correct |
66 |
Correct |
104 ms |
21624 KB |
Output is correct |
67 |
Correct |
101 ms |
21752 KB |
Output is correct |
68 |
Correct |
153 ms |
28372 KB |
Output is correct |
69 |
Correct |
165 ms |
29036 KB |
Output is correct |
70 |
Correct |
164 ms |
29036 KB |
Output is correct |
71 |
Correct |
208 ms |
43908 KB |
Output is correct |
72 |
Correct |
221 ms |
44136 KB |
Output is correct |
73 |
Correct |
186 ms |
40460 KB |
Output is correct |
74 |
Correct |
190 ms |
40940 KB |
Output is correct |
75 |
Correct |
191 ms |
39404 KB |
Output is correct |
76 |
Correct |
188 ms |
39272 KB |
Output is correct |
77 |
Correct |
195 ms |
38612 KB |
Output is correct |
78 |
Correct |
199 ms |
38120 KB |
Output is correct |
79 |
Correct |
187 ms |
36704 KB |
Output is correct |