#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5;
priority_queue<long long> dp[maxn];
long long val[maxn];
int slope[maxn];
vector<int> g[maxn];
int par[maxn], c[maxn];
/*void print(int v) {
vector<long long> res;
{
auto tmp = down[v];
while (!tmp.empty()) {
res.emplace_back(tmp.top());
tmp.pop();
}
reverse(res.begin(), res.end());
}
{
auto tmp = up[v];
while (!tmp.empty()) {
res.emplace_back(tmp.top() + modUp[v]);
tmp.pop();
}
for (const int& x : res) {
cerr << x << ' ';
}
cerr << '\n';
}
}*/
void dfs(int v) {
if (g[v].empty()) {
val[v] = c[v];
slope[v] = -1;
dp[v].emplace(c[v]);
dp[v].emplace(c[v]);
return;
}
for (const int& u : g[v]) {
dfs(u);
if (dp[v].size() < dp[u].size()) {
swap(dp[v], dp[u]);
swap(val[v], val[u]);
swap(slope[v], slope[u]);
}
slope[v] += slope[u];
val[v] += val[u];
while (!dp[u].empty()) {
dp[v].emplace(dp[u].top());
dp[u].pop();
}
}
// cerr << "v: " << v + 1 << " before:\n";
// print(v);
if (par[v] != -1) {
val[v] += c[v];
while (slope[v] + (int)dp[v].size() > 1) {
dp[v].pop();
}
long long last = dp[v].top();
dp[v].pop();
long long prelast = dp[v].top();
dp[v].pop();
dp[v].emplace(prelast + c[v]);
dp[v].emplace(last + c[v]);
}
// cerr << "v: " << v + 1 << ", val: " << val[v] << ", slope: " << slope[v] << '\n';
// print(v);
}
void solve() {
int n, m;
cin >> n >> m;
par[0] = -1, c[0] = 0;
for (int i = 1; i < n + m; ++i) {
cin >> par[i] >> c[i];
--par[i];
g[par[i]].emplace_back(i);
}
dfs(0);
while (slope[0] + (int)dp[0].size() > 0) {
dp[0].pop();
}
long long ans = val[0];
vector<long long> leftPart;
while (!dp[0].empty()) {
leftPart.emplace_back(dp[0].top());
dp[0].pop();
}
leftPart.emplace_back(0);
reverse(leftPart.begin(), leftPart.end());
for (int i = 0; i < (int)leftPart.size() - 1; ++i) {
ans += (slope[0] + i) * (leftPart[i + 1] - leftPart[i]);
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int T = 1;
cin >> T;
while (T--) {
solve();
cerr << "-----------\n";
cout << "-----------\n";
}
#else
int T = 1;
// cin >> T;
while (T--) {
solve();
}
#endif
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
22360 KB |
Output is correct |
2 |
Correct |
5 ms |
22364 KB |
Output is correct |
3 |
Correct |
5 ms |
22460 KB |
Output is correct |
4 |
Correct |
5 ms |
22360 KB |
Output is correct |
5 |
Correct |
5 ms |
22548 KB |
Output is correct |
6 |
Correct |
5 ms |
22364 KB |
Output is correct |
7 |
Correct |
5 ms |
22364 KB |
Output is correct |
8 |
Correct |
6 ms |
22364 KB |
Output is correct |
9 |
Correct |
5 ms |
22364 KB |
Output is correct |
10 |
Correct |
5 ms |
22616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
22364 KB |
Output is correct |
2 |
Correct |
5 ms |
22364 KB |
Output is correct |
3 |
Correct |
5 ms |
22364 KB |
Output is correct |
4 |
Correct |
5 ms |
22364 KB |
Output is correct |
5 |
Correct |
6 ms |
22600 KB |
Output is correct |
6 |
Correct |
5 ms |
22364 KB |
Output is correct |
7 |
Correct |
5 ms |
22364 KB |
Output is correct |
8 |
Correct |
5 ms |
22552 KB |
Output is correct |
9 |
Correct |
5 ms |
22364 KB |
Output is correct |
10 |
Correct |
5 ms |
22364 KB |
Output is correct |
11 |
Correct |
5 ms |
22364 KB |
Output is correct |
12 |
Correct |
5 ms |
22360 KB |
Output is correct |
13 |
Correct |
5 ms |
22364 KB |
Output is correct |
14 |
Correct |
5 ms |
22620 KB |
Output is correct |
15 |
Correct |
5 ms |
22364 KB |
Output is correct |
16 |
Correct |
5 ms |
22468 KB |
Output is correct |
17 |
Correct |
5 ms |
22364 KB |
Output is correct |
18 |
Correct |
6 ms |
22364 KB |
Output is correct |
19 |
Correct |
5 ms |
22364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
22360 KB |
Output is correct |
2 |
Correct |
5 ms |
22364 KB |
Output is correct |
3 |
Correct |
5 ms |
22460 KB |
Output is correct |
4 |
Correct |
5 ms |
22360 KB |
Output is correct |
5 |
Correct |
5 ms |
22548 KB |
Output is correct |
6 |
Correct |
5 ms |
22364 KB |
Output is correct |
7 |
Correct |
5 ms |
22364 KB |
Output is correct |
8 |
Correct |
6 ms |
22364 KB |
Output is correct |
9 |
Correct |
5 ms |
22364 KB |
Output is correct |
10 |
Correct |
5 ms |
22616 KB |
Output is correct |
11 |
Correct |
5 ms |
22364 KB |
Output is correct |
12 |
Correct |
5 ms |
22364 KB |
Output is correct |
13 |
Correct |
5 ms |
22364 KB |
Output is correct |
14 |
Correct |
5 ms |
22364 KB |
Output is correct |
15 |
Correct |
6 ms |
22600 KB |
Output is correct |
16 |
Correct |
5 ms |
22364 KB |
Output is correct |
17 |
Correct |
5 ms |
22364 KB |
Output is correct |
18 |
Correct |
5 ms |
22552 KB |
Output is correct |
19 |
Correct |
5 ms |
22364 KB |
Output is correct |
20 |
Correct |
5 ms |
22364 KB |
Output is correct |
21 |
Correct |
5 ms |
22364 KB |
Output is correct |
22 |
Correct |
5 ms |
22360 KB |
Output is correct |
23 |
Correct |
5 ms |
22364 KB |
Output is correct |
24 |
Correct |
5 ms |
22620 KB |
Output is correct |
25 |
Correct |
5 ms |
22364 KB |
Output is correct |
26 |
Correct |
5 ms |
22468 KB |
Output is correct |
27 |
Correct |
5 ms |
22364 KB |
Output is correct |
28 |
Correct |
6 ms |
22364 KB |
Output is correct |
29 |
Correct |
5 ms |
22364 KB |
Output is correct |
30 |
Correct |
6 ms |
22620 KB |
Output is correct |
31 |
Correct |
6 ms |
22616 KB |
Output is correct |
32 |
Correct |
6 ms |
22620 KB |
Output is correct |
33 |
Correct |
6 ms |
22616 KB |
Output is correct |
34 |
Correct |
8 ms |
22620 KB |
Output is correct |
35 |
Correct |
6 ms |
22680 KB |
Output is correct |
36 |
Correct |
7 ms |
22872 KB |
Output is correct |
37 |
Correct |
7 ms |
22700 KB |
Output is correct |
38 |
Correct |
7 ms |
22840 KB |
Output is correct |
39 |
Correct |
7 ms |
22876 KB |
Output is correct |
40 |
Correct |
6 ms |
23384 KB |
Output is correct |
41 |
Correct |
7 ms |
23384 KB |
Output is correct |
42 |
Correct |
6 ms |
22620 KB |
Output is correct |
43 |
Correct |
8 ms |
23132 KB |
Output is correct |
44 |
Correct |
9 ms |
23060 KB |
Output is correct |
45 |
Correct |
7 ms |
22956 KB |
Output is correct |
46 |
Correct |
8 ms |
22876 KB |
Output is correct |
47 |
Correct |
8 ms |
22904 KB |
Output is correct |
48 |
Correct |
7 ms |
22876 KB |
Output is correct |
49 |
Correct |
7 ms |
23128 KB |
Output is correct |
50 |
Correct |
7 ms |
22876 KB |
Output is correct |
51 |
Correct |
7 ms |
22876 KB |
Output is correct |
52 |
Correct |
6 ms |
22976 KB |
Output is correct |
53 |
Correct |
7 ms |
22876 KB |
Output is correct |
54 |
Correct |
7 ms |
22976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
22360 KB |
Output is correct |
2 |
Correct |
5 ms |
22364 KB |
Output is correct |
3 |
Correct |
5 ms |
22460 KB |
Output is correct |
4 |
Correct |
5 ms |
22360 KB |
Output is correct |
5 |
Correct |
5 ms |
22548 KB |
Output is correct |
6 |
Correct |
5 ms |
22364 KB |
Output is correct |
7 |
Correct |
5 ms |
22364 KB |
Output is correct |
8 |
Correct |
6 ms |
22364 KB |
Output is correct |
9 |
Correct |
5 ms |
22364 KB |
Output is correct |
10 |
Correct |
5 ms |
22616 KB |
Output is correct |
11 |
Correct |
5 ms |
22364 KB |
Output is correct |
12 |
Correct |
5 ms |
22364 KB |
Output is correct |
13 |
Correct |
5 ms |
22364 KB |
Output is correct |
14 |
Correct |
5 ms |
22364 KB |
Output is correct |
15 |
Correct |
6 ms |
22600 KB |
Output is correct |
16 |
Correct |
5 ms |
22364 KB |
Output is correct |
17 |
Correct |
5 ms |
22364 KB |
Output is correct |
18 |
Correct |
5 ms |
22552 KB |
Output is correct |
19 |
Correct |
5 ms |
22364 KB |
Output is correct |
20 |
Correct |
5 ms |
22364 KB |
Output is correct |
21 |
Correct |
5 ms |
22364 KB |
Output is correct |
22 |
Correct |
5 ms |
22360 KB |
Output is correct |
23 |
Correct |
5 ms |
22364 KB |
Output is correct |
24 |
Correct |
5 ms |
22620 KB |
Output is correct |
25 |
Correct |
5 ms |
22364 KB |
Output is correct |
26 |
Correct |
5 ms |
22468 KB |
Output is correct |
27 |
Correct |
5 ms |
22364 KB |
Output is correct |
28 |
Correct |
6 ms |
22364 KB |
Output is correct |
29 |
Correct |
5 ms |
22364 KB |
Output is correct |
30 |
Correct |
6 ms |
22620 KB |
Output is correct |
31 |
Correct |
6 ms |
22616 KB |
Output is correct |
32 |
Correct |
6 ms |
22620 KB |
Output is correct |
33 |
Correct |
6 ms |
22616 KB |
Output is correct |
34 |
Correct |
8 ms |
22620 KB |
Output is correct |
35 |
Correct |
6 ms |
22680 KB |
Output is correct |
36 |
Correct |
7 ms |
22872 KB |
Output is correct |
37 |
Correct |
7 ms |
22700 KB |
Output is correct |
38 |
Correct |
7 ms |
22840 KB |
Output is correct |
39 |
Correct |
7 ms |
22876 KB |
Output is correct |
40 |
Correct |
6 ms |
23384 KB |
Output is correct |
41 |
Correct |
7 ms |
23384 KB |
Output is correct |
42 |
Correct |
6 ms |
22620 KB |
Output is correct |
43 |
Correct |
8 ms |
23132 KB |
Output is correct |
44 |
Correct |
9 ms |
23060 KB |
Output is correct |
45 |
Correct |
7 ms |
22956 KB |
Output is correct |
46 |
Correct |
8 ms |
22876 KB |
Output is correct |
47 |
Correct |
8 ms |
22904 KB |
Output is correct |
48 |
Correct |
7 ms |
22876 KB |
Output is correct |
49 |
Correct |
7 ms |
23128 KB |
Output is correct |
50 |
Correct |
7 ms |
22876 KB |
Output is correct |
51 |
Correct |
7 ms |
22876 KB |
Output is correct |
52 |
Correct |
6 ms |
22976 KB |
Output is correct |
53 |
Correct |
7 ms |
22876 KB |
Output is correct |
54 |
Correct |
7 ms |
22976 KB |
Output is correct |
55 |
Correct |
11 ms |
23384 KB |
Output is correct |
56 |
Correct |
39 ms |
26064 KB |
Output is correct |
57 |
Correct |
54 ms |
28296 KB |
Output is correct |
58 |
Correct |
55 ms |
30216 KB |
Output is correct |
59 |
Correct |
85 ms |
32452 KB |
Output is correct |
60 |
Correct |
120 ms |
34512 KB |
Output is correct |
61 |
Correct |
172 ms |
37160 KB |
Output is correct |
62 |
Correct |
139 ms |
38284 KB |
Output is correct |
63 |
Correct |
197 ms |
41212 KB |
Output is correct |
64 |
Correct |
210 ms |
41792 KB |
Output is correct |
65 |
Correct |
106 ms |
79184 KB |
Output is correct |
66 |
Correct |
87 ms |
79180 KB |
Output is correct |
67 |
Correct |
100 ms |
37120 KB |
Output is correct |
68 |
Correct |
144 ms |
62144 KB |
Output is correct |
69 |
Correct |
166 ms |
58612 KB |
Output is correct |
70 |
Correct |
160 ms |
58560 KB |
Output is correct |
71 |
Correct |
176 ms |
59016 KB |
Output is correct |
72 |
Correct |
169 ms |
57908 KB |
Output is correct |
73 |
Correct |
156 ms |
54984 KB |
Output is correct |
74 |
Correct |
162 ms |
55216 KB |
Output is correct |
75 |
Correct |
207 ms |
51636 KB |
Output is correct |
76 |
Correct |
151 ms |
51636 KB |
Output is correct |
77 |
Correct |
160 ms |
53436 KB |
Output is correct |
78 |
Correct |
153 ms |
50240 KB |
Output is correct |
79 |
Correct |
148 ms |
48548 KB |
Output is correct |