#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 200200;
int n;
int p[N];
int h[N];
int c[N];
map<int, ll> mp[N];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> p[i] >> h[i] >> c[i];
for (int i = 1; i <= n; i++)
mp[i][0] = 0;
auto join = [&](int u, int v) {
if ((int)mp[u].size() < (int)mp[v].size()) swap(mp[u], mp[v]);
for (auto c : mp[v])
mp[u][c.first] += c.second;
};
for (int v = n; v >= 1; v--) {
int s = c[v];
auto it = mp[v].lower_bound(h[v]);
while (it != mp[v].begin()) {
--it;
if (s >= it->second) {
s -= it->second;
it=mp[v].erase(it);
if(it==mp[v].begin())break;
} else {
mp[v][it->first] = it->second - s;
s = 0;
break;
}
}
mp[v][0] += c[v] - s;
mp[v][h[v]] += c[v];
if (v != 1) join(p[v], v);
}
cout << mp[1][0];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
3 ms |
9820 KB |
Output is correct |
3 |
Correct |
5 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9880 KB |
Output is correct |
5 |
Correct |
8 ms |
11356 KB |
Output is correct |
6 |
Correct |
8 ms |
10844 KB |
Output is correct |
7 |
Correct |
7 ms |
10656 KB |
Output is correct |
8 |
Correct |
8 ms |
11356 KB |
Output is correct |
9 |
Correct |
9 ms |
10840 KB |
Output is correct |
10 |
Correct |
7 ms |
10588 KB |
Output is correct |
11 |
Correct |
7 ms |
10420 KB |
Output is correct |
12 |
Correct |
7 ms |
10332 KB |
Output is correct |
13 |
Correct |
5 ms |
10324 KB |
Output is correct |
14 |
Correct |
6 ms |
10332 KB |
Output is correct |
15 |
Correct |
7 ms |
10332 KB |
Output is correct |
16 |
Correct |
9 ms |
11612 KB |
Output is correct |
17 |
Correct |
8 ms |
11024 KB |
Output is correct |
18 |
Correct |
9 ms |
10328 KB |
Output is correct |
19 |
Correct |
7 ms |
10584 KB |
Output is correct |
20 |
Correct |
6 ms |
10400 KB |
Output is correct |
21 |
Correct |
6 ms |
10396 KB |
Output is correct |
22 |
Correct |
7 ms |
10844 KB |
Output is correct |
23 |
Correct |
6 ms |
10588 KB |
Output is correct |
24 |
Correct |
7 ms |
10588 KB |
Output is correct |
25 |
Correct |
7 ms |
10332 KB |
Output is correct |
26 |
Correct |
5 ms |
10332 KB |
Output is correct |
27 |
Correct |
7 ms |
10840 KB |
Output is correct |
28 |
Correct |
7 ms |
10636 KB |
Output is correct |
29 |
Correct |
7 ms |
10588 KB |
Output is correct |
30 |
Correct |
6 ms |
10772 KB |
Output is correct |
31 |
Correct |
7 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
3 ms |
9820 KB |
Output is correct |
3 |
Correct |
5 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9880 KB |
Output is correct |
5 |
Correct |
8 ms |
11356 KB |
Output is correct |
6 |
Correct |
8 ms |
10844 KB |
Output is correct |
7 |
Correct |
7 ms |
10656 KB |
Output is correct |
8 |
Correct |
8 ms |
11356 KB |
Output is correct |
9 |
Correct |
9 ms |
10840 KB |
Output is correct |
10 |
Correct |
7 ms |
10588 KB |
Output is correct |
11 |
Correct |
7 ms |
10420 KB |
Output is correct |
12 |
Correct |
7 ms |
10332 KB |
Output is correct |
13 |
Correct |
5 ms |
10324 KB |
Output is correct |
14 |
Correct |
6 ms |
10332 KB |
Output is correct |
15 |
Correct |
7 ms |
10332 KB |
Output is correct |
16 |
Correct |
9 ms |
11612 KB |
Output is correct |
17 |
Correct |
8 ms |
11024 KB |
Output is correct |
18 |
Correct |
9 ms |
10328 KB |
Output is correct |
19 |
Correct |
7 ms |
10584 KB |
Output is correct |
20 |
Correct |
6 ms |
10400 KB |
Output is correct |
21 |
Correct |
6 ms |
10396 KB |
Output is correct |
22 |
Correct |
7 ms |
10844 KB |
Output is correct |
23 |
Correct |
6 ms |
10588 KB |
Output is correct |
24 |
Correct |
7 ms |
10588 KB |
Output is correct |
25 |
Correct |
7 ms |
10332 KB |
Output is correct |
26 |
Correct |
5 ms |
10332 KB |
Output is correct |
27 |
Correct |
7 ms |
10840 KB |
Output is correct |
28 |
Correct |
7 ms |
10636 KB |
Output is correct |
29 |
Correct |
7 ms |
10588 KB |
Output is correct |
30 |
Correct |
6 ms |
10772 KB |
Output is correct |
31 |
Correct |
7 ms |
10588 KB |
Output is correct |
32 |
Correct |
10 ms |
11356 KB |
Output is correct |
33 |
Correct |
319 ms |
95812 KB |
Output is correct |
34 |
Correct |
224 ms |
65620 KB |
Output is correct |
35 |
Correct |
301 ms |
94300 KB |
Output is correct |
36 |
Correct |
241 ms |
65620 KB |
Output is correct |
37 |
Correct |
141 ms |
41044 KB |
Output is correct |
38 |
Correct |
122 ms |
35920 KB |
Output is correct |
39 |
Correct |
111 ms |
29780 KB |
Output is correct |
40 |
Correct |
100 ms |
29780 KB |
Output is correct |
41 |
Correct |
61 ms |
29776 KB |
Output is correct |
42 |
Correct |
102 ms |
29832 KB |
Output is correct |
43 |
Correct |
95 ms |
29776 KB |
Output is correct |
44 |
Correct |
245 ms |
109136 KB |
Output is correct |
45 |
Correct |
157 ms |
73380 KB |
Output is correct |
46 |
Correct |
71 ms |
35924 KB |
Output is correct |
47 |
Correct |
151 ms |
43108 KB |
Output is correct |
48 |
Correct |
81 ms |
35980 KB |
Output is correct |
49 |
Correct |
68 ms |
35924 KB |
Output is correct |
50 |
Correct |
182 ms |
53776 KB |
Output is correct |
51 |
Correct |
85 ms |
41552 KB |
Output is correct |
52 |
Correct |
157 ms |
43092 KB |
Output is correct |
53 |
Correct |
81 ms |
35924 KB |
Output is correct |
54 |
Correct |
62 ms |
30036 KB |
Output is correct |
55 |
Correct |
134 ms |
46916 KB |
Output is correct |
56 |
Correct |
105 ms |
42068 KB |
Output is correct |
57 |
Correct |
106 ms |
41296 KB |
Output is correct |
58 |
Correct |
124 ms |
47732 KB |
Output is correct |
59 |
Correct |
136 ms |
47700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
3 ms |
9820 KB |
Output is correct |
3 |
Correct |
5 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9880 KB |
Output is correct |
5 |
Correct |
8 ms |
11356 KB |
Output is correct |
6 |
Correct |
8 ms |
10844 KB |
Output is correct |
7 |
Correct |
7 ms |
10656 KB |
Output is correct |
8 |
Correct |
8 ms |
11356 KB |
Output is correct |
9 |
Correct |
9 ms |
10840 KB |
Output is correct |
10 |
Correct |
7 ms |
10588 KB |
Output is correct |
11 |
Correct |
7 ms |
10420 KB |
Output is correct |
12 |
Correct |
7 ms |
10332 KB |
Output is correct |
13 |
Correct |
5 ms |
10324 KB |
Output is correct |
14 |
Correct |
6 ms |
10332 KB |
Output is correct |
15 |
Correct |
7 ms |
10332 KB |
Output is correct |
16 |
Correct |
9 ms |
11612 KB |
Output is correct |
17 |
Correct |
8 ms |
11024 KB |
Output is correct |
18 |
Correct |
9 ms |
10328 KB |
Output is correct |
19 |
Correct |
7 ms |
10584 KB |
Output is correct |
20 |
Correct |
6 ms |
10400 KB |
Output is correct |
21 |
Correct |
6 ms |
10396 KB |
Output is correct |
22 |
Correct |
7 ms |
10844 KB |
Output is correct |
23 |
Correct |
6 ms |
10588 KB |
Output is correct |
24 |
Correct |
7 ms |
10588 KB |
Output is correct |
25 |
Correct |
7 ms |
10332 KB |
Output is correct |
26 |
Correct |
5 ms |
10332 KB |
Output is correct |
27 |
Correct |
7 ms |
10840 KB |
Output is correct |
28 |
Correct |
7 ms |
10636 KB |
Output is correct |
29 |
Correct |
7 ms |
10588 KB |
Output is correct |
30 |
Correct |
6 ms |
10772 KB |
Output is correct |
31 |
Correct |
7 ms |
10588 KB |
Output is correct |
32 |
Correct |
10 ms |
11356 KB |
Output is correct |
33 |
Correct |
319 ms |
95812 KB |
Output is correct |
34 |
Correct |
224 ms |
65620 KB |
Output is correct |
35 |
Correct |
301 ms |
94300 KB |
Output is correct |
36 |
Correct |
241 ms |
65620 KB |
Output is correct |
37 |
Correct |
141 ms |
41044 KB |
Output is correct |
38 |
Correct |
122 ms |
35920 KB |
Output is correct |
39 |
Correct |
111 ms |
29780 KB |
Output is correct |
40 |
Correct |
100 ms |
29780 KB |
Output is correct |
41 |
Correct |
61 ms |
29776 KB |
Output is correct |
42 |
Correct |
102 ms |
29832 KB |
Output is correct |
43 |
Correct |
95 ms |
29776 KB |
Output is correct |
44 |
Correct |
245 ms |
109136 KB |
Output is correct |
45 |
Correct |
157 ms |
73380 KB |
Output is correct |
46 |
Correct |
71 ms |
35924 KB |
Output is correct |
47 |
Correct |
151 ms |
43108 KB |
Output is correct |
48 |
Correct |
81 ms |
35980 KB |
Output is correct |
49 |
Correct |
68 ms |
35924 KB |
Output is correct |
50 |
Correct |
182 ms |
53776 KB |
Output is correct |
51 |
Correct |
85 ms |
41552 KB |
Output is correct |
52 |
Correct |
157 ms |
43092 KB |
Output is correct |
53 |
Correct |
81 ms |
35924 KB |
Output is correct |
54 |
Correct |
62 ms |
30036 KB |
Output is correct |
55 |
Correct |
134 ms |
46916 KB |
Output is correct |
56 |
Correct |
105 ms |
42068 KB |
Output is correct |
57 |
Correct |
106 ms |
41296 KB |
Output is correct |
58 |
Correct |
124 ms |
47732 KB |
Output is correct |
59 |
Correct |
136 ms |
47700 KB |
Output is correct |
60 |
Correct |
4 ms |
10072 KB |
Output is correct |
61 |
Incorrect |
4 ms |
9820 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |