//Challenge: Accepted
#include <bits/stdc++.h>
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T,class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
while (l != r) cout << *l << " ", l++;
cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 300005
#define pii pair<ll, ll>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
#define se multiset<ll>
vector<pii> adj[maxn];
void merge(se &a, se &b) {
if (b.size() > a.size()) swap(a, b);
for (auto i:b) a.insert(i);
b.clear();
}
ll vy[maxn]; //cost at 0
se dfs(int n) {
se ret;
int chi = 0;
for (auto [v, e]:adj[n]) {
chi++;
se tmp = dfs(v);
ll pr = *prev(tmp.end());
tmp.erase(prev(tmp.end()));
ll pl = *prev(tmp.end());
tmp.erase(prev(tmp.end()));
tmp.insert(pl + e), tmp.insert(pr + e);
vy[n] += vy[v] + e;
merge(ret, tmp);
}
for (int i = 0;i < chi - 1;i++) {
ret.erase(prev(ret.end()));
}
if (chi == 0) {
ret.insert(0);
ret.insert(0);
}
return ret;
}
int main() {
io
int n, m;
cin >> n >> m;
for (int i = 2;i <= n + m;i++) {
int pi, ci;
cin >> pi >> ci;
adj[pi].push_back({i, ci});
}
se s = dfs(1);
ll ans = vy[1], slope = -s.size() + 1, curx = 0;
for (auto i:s) {
ans += slope * (i - curx);
curx = i;
slope++;
}
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
3 ms |
7368 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7372 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7372 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7372 KB |
Output is correct |
8 |
Correct |
4 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
4 ms |
7372 KB |
Output is correct |
14 |
Correct |
4 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
4 ms |
7380 KB |
Output is correct |
17 |
Correct |
4 ms |
7376 KB |
Output is correct |
18 |
Correct |
4 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
3 ms |
7368 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7372 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7252 KB |
Output is correct |
11 |
Correct |
3 ms |
7252 KB |
Output is correct |
12 |
Correct |
4 ms |
7252 KB |
Output is correct |
13 |
Correct |
4 ms |
7252 KB |
Output is correct |
14 |
Correct |
4 ms |
7372 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
4 ms |
7380 KB |
Output is correct |
17 |
Correct |
4 ms |
7372 KB |
Output is correct |
18 |
Correct |
4 ms |
7252 KB |
Output is correct |
19 |
Correct |
4 ms |
7252 KB |
Output is correct |
20 |
Correct |
4 ms |
7380 KB |
Output is correct |
21 |
Correct |
4 ms |
7380 KB |
Output is correct |
22 |
Correct |
4 ms |
7380 KB |
Output is correct |
23 |
Correct |
4 ms |
7372 KB |
Output is correct |
24 |
Correct |
4 ms |
7380 KB |
Output is correct |
25 |
Correct |
4 ms |
7380 KB |
Output is correct |
26 |
Correct |
4 ms |
7380 KB |
Output is correct |
27 |
Correct |
4 ms |
7376 KB |
Output is correct |
28 |
Correct |
4 ms |
7380 KB |
Output is correct |
29 |
Correct |
4 ms |
7372 KB |
Output is correct |
30 |
Correct |
4 ms |
7380 KB |
Output is correct |
31 |
Correct |
5 ms |
7380 KB |
Output is correct |
32 |
Correct |
5 ms |
7380 KB |
Output is correct |
33 |
Correct |
6 ms |
7512 KB |
Output is correct |
34 |
Correct |
6 ms |
7560 KB |
Output is correct |
35 |
Correct |
6 ms |
7508 KB |
Output is correct |
36 |
Correct |
6 ms |
7580 KB |
Output is correct |
37 |
Correct |
6 ms |
7636 KB |
Output is correct |
38 |
Correct |
7 ms |
7656 KB |
Output is correct |
39 |
Correct |
6 ms |
7644 KB |
Output is correct |
40 |
Correct |
6 ms |
8532 KB |
Output is correct |
41 |
Correct |
6 ms |
8532 KB |
Output is correct |
42 |
Correct |
5 ms |
7636 KB |
Output is correct |
43 |
Correct |
6 ms |
8148 KB |
Output is correct |
44 |
Correct |
7 ms |
8020 KB |
Output is correct |
45 |
Correct |
7 ms |
8028 KB |
Output is correct |
46 |
Correct |
7 ms |
7720 KB |
Output is correct |
47 |
Correct |
7 ms |
7764 KB |
Output is correct |
48 |
Correct |
7 ms |
7716 KB |
Output is correct |
49 |
Correct |
7 ms |
7768 KB |
Output is correct |
50 |
Correct |
6 ms |
7764 KB |
Output is correct |
51 |
Correct |
6 ms |
7764 KB |
Output is correct |
52 |
Correct |
7 ms |
7764 KB |
Output is correct |
53 |
Correct |
7 ms |
7764 KB |
Output is correct |
54 |
Correct |
7 ms |
7892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
3 ms |
7368 KB |
Output is correct |
5 |
Correct |
4 ms |
7380 KB |
Output is correct |
6 |
Correct |
4 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7372 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7252 KB |
Output is correct |
11 |
Correct |
3 ms |
7252 KB |
Output is correct |
12 |
Correct |
4 ms |
7252 KB |
Output is correct |
13 |
Correct |
4 ms |
7252 KB |
Output is correct |
14 |
Correct |
4 ms |
7372 KB |
Output is correct |
15 |
Correct |
4 ms |
7380 KB |
Output is correct |
16 |
Correct |
4 ms |
7380 KB |
Output is correct |
17 |
Correct |
4 ms |
7372 KB |
Output is correct |
18 |
Correct |
4 ms |
7252 KB |
Output is correct |
19 |
Correct |
4 ms |
7252 KB |
Output is correct |
20 |
Correct |
4 ms |
7380 KB |
Output is correct |
21 |
Correct |
4 ms |
7380 KB |
Output is correct |
22 |
Correct |
4 ms |
7380 KB |
Output is correct |
23 |
Correct |
4 ms |
7372 KB |
Output is correct |
24 |
Correct |
4 ms |
7380 KB |
Output is correct |
25 |
Correct |
4 ms |
7380 KB |
Output is correct |
26 |
Correct |
4 ms |
7380 KB |
Output is correct |
27 |
Correct |
4 ms |
7376 KB |
Output is correct |
28 |
Correct |
4 ms |
7380 KB |
Output is correct |
29 |
Correct |
4 ms |
7372 KB |
Output is correct |
30 |
Correct |
4 ms |
7380 KB |
Output is correct |
31 |
Correct |
5 ms |
7380 KB |
Output is correct |
32 |
Correct |
5 ms |
7380 KB |
Output is correct |
33 |
Correct |
6 ms |
7512 KB |
Output is correct |
34 |
Correct |
6 ms |
7560 KB |
Output is correct |
35 |
Correct |
6 ms |
7508 KB |
Output is correct |
36 |
Correct |
6 ms |
7580 KB |
Output is correct |
37 |
Correct |
6 ms |
7636 KB |
Output is correct |
38 |
Correct |
7 ms |
7656 KB |
Output is correct |
39 |
Correct |
6 ms |
7644 KB |
Output is correct |
40 |
Correct |
6 ms |
8532 KB |
Output is correct |
41 |
Correct |
6 ms |
8532 KB |
Output is correct |
42 |
Correct |
5 ms |
7636 KB |
Output is correct |
43 |
Correct |
6 ms |
8148 KB |
Output is correct |
44 |
Correct |
7 ms |
8020 KB |
Output is correct |
45 |
Correct |
7 ms |
8028 KB |
Output is correct |
46 |
Correct |
7 ms |
7720 KB |
Output is correct |
47 |
Correct |
7 ms |
7764 KB |
Output is correct |
48 |
Correct |
7 ms |
7716 KB |
Output is correct |
49 |
Correct |
7 ms |
7768 KB |
Output is correct |
50 |
Correct |
6 ms |
7764 KB |
Output is correct |
51 |
Correct |
6 ms |
7764 KB |
Output is correct |
52 |
Correct |
7 ms |
7764 KB |
Output is correct |
53 |
Correct |
7 ms |
7764 KB |
Output is correct |
54 |
Correct |
7 ms |
7892 KB |
Output is correct |
55 |
Correct |
11 ms |
8268 KB |
Output is correct |
56 |
Correct |
34 ms |
10580 KB |
Output is correct |
57 |
Correct |
56 ms |
12904 KB |
Output is correct |
58 |
Correct |
76 ms |
14724 KB |
Output is correct |
59 |
Correct |
114 ms |
17312 KB |
Output is correct |
60 |
Correct |
135 ms |
19376 KB |
Output is correct |
61 |
Correct |
152 ms |
21320 KB |
Output is correct |
62 |
Correct |
172 ms |
23296 KB |
Output is correct |
63 |
Correct |
209 ms |
26032 KB |
Output is correct |
64 |
Correct |
225 ms |
26316 KB |
Output is correct |
65 |
Correct |
133 ms |
80284 KB |
Output is correct |
66 |
Correct |
131 ms |
80204 KB |
Output is correct |
67 |
Correct |
113 ms |
24164 KB |
Output is correct |
68 |
Correct |
188 ms |
55464 KB |
Output is correct |
69 |
Correct |
227 ms |
51488 KB |
Output is correct |
70 |
Correct |
217 ms |
51532 KB |
Output is correct |
71 |
Correct |
267 ms |
32656 KB |
Output is correct |
72 |
Correct |
280 ms |
32808 KB |
Output is correct |
73 |
Correct |
285 ms |
31336 KB |
Output is correct |
74 |
Correct |
302 ms |
31624 KB |
Output is correct |
75 |
Correct |
275 ms |
30612 KB |
Output is correct |
76 |
Correct |
281 ms |
30744 KB |
Output is correct |
77 |
Correct |
313 ms |
30608 KB |
Output is correct |
78 |
Correct |
301 ms |
31024 KB |
Output is correct |
79 |
Correct |
334 ms |
43700 KB |
Output is correct |