#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 42, INF = 1e18 + 42;
int n, h[N], cost[N], par[N];
bool vu[N], root[N], act[N];
vector<int> adj[N], path, id[N];
multiset<pair<int, int>> DFS(int i, int pre) {
vu[i] = true;
multiset<pair<int, int>> ans;
for(int j : adj[i])
if(j != pre) {
multiset<pair<int, int>> fils = DFS(j, i);
if(ans.size() < fils.size())
swap(ans, fils);
for(auto p : fils)
ans.insert(p);
}
for(int j : id[i]) {
ans.insert({0, cost[j]});
ans.insert({h[j]+1, cost[j]});
}
for(int j : id[i]) {
int suppr = cost[j];
while(suppr) {
auto it = ans.lower_bound({h[j]+1, 0});
it--;
if((*it).second <= suppr) {
suppr -= (*it).second;
ans.erase(it);
} else {
auto copy = (*it);
ans.erase(it);
ans.insert({copy.first, copy.second - suppr});
suppr = 0;
}
}
}
return ans;
}
void dfs(int i) {
if(act[i]) {
root[i] = true;
int j = path.size();
while(path[j-1] != i) {
id[i].push_back(j);
j--;
}
return;
}
if(vu[i])
return;
path.push_back(i);
vu[i] = act[i] = true;
dfs(par[i]);
act[i] = false;
path.pop_back();
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 0; i < n; i++) {
cin >> par[i] >> h[i] >> cost[i];
par[i]--;
if(i != par[i])
adj[par[i]].push_back(i);
}
for(int i = 0; i < n; i++)
id[i].push_back(i);
for(int i = 0; i < n; i++)
dfs(i);
int ans = 0;
for(int i = 0; i < n; i++)
if(root[i]) {
multiset<pair<int, int>> slope = DFS(i, i);
for(auto p : slope)
if(p.first == 0)
ans += p.second;
else
break;
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9716 KB |
Output is correct |
3 |
Correct |
7 ms |
9684 KB |
Output is correct |
4 |
Correct |
6 ms |
9712 KB |
Output is correct |
5 |
Correct |
12 ms |
10516 KB |
Output is correct |
6 |
Correct |
12 ms |
10512 KB |
Output is correct |
7 |
Correct |
11 ms |
10512 KB |
Output is correct |
8 |
Correct |
11 ms |
10580 KB |
Output is correct |
9 |
Correct |
11 ms |
10580 KB |
Output is correct |
10 |
Correct |
11 ms |
10632 KB |
Output is correct |
11 |
Correct |
11 ms |
10576 KB |
Output is correct |
12 |
Correct |
13 ms |
11532 KB |
Output is correct |
13 |
Correct |
12 ms |
11536 KB |
Output is correct |
14 |
Correct |
12 ms |
11148 KB |
Output is correct |
15 |
Correct |
12 ms |
11272 KB |
Output is correct |
16 |
Correct |
13 ms |
10672 KB |
Output is correct |
17 |
Correct |
12 ms |
10708 KB |
Output is correct |
18 |
Correct |
10 ms |
10580 KB |
Output is correct |
19 |
Correct |
11 ms |
10964 KB |
Output is correct |
20 |
Correct |
10 ms |
11016 KB |
Output is correct |
21 |
Correct |
10 ms |
11092 KB |
Output is correct |
22 |
Correct |
11 ms |
10496 KB |
Output is correct |
23 |
Correct |
8 ms |
10368 KB |
Output is correct |
24 |
Correct |
10 ms |
11024 KB |
Output is correct |
25 |
Correct |
10 ms |
11028 KB |
Output is correct |
26 |
Correct |
10 ms |
11528 KB |
Output is correct |
27 |
Correct |
11 ms |
11016 KB |
Output is correct |
28 |
Correct |
10 ms |
11092 KB |
Output is correct |
29 |
Correct |
11 ms |
11396 KB |
Output is correct |
30 |
Correct |
10 ms |
11656 KB |
Output is correct |
31 |
Correct |
10 ms |
11604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9716 KB |
Output is correct |
3 |
Correct |
7 ms |
9684 KB |
Output is correct |
4 |
Correct |
6 ms |
9712 KB |
Output is correct |
5 |
Correct |
12 ms |
10516 KB |
Output is correct |
6 |
Correct |
12 ms |
10512 KB |
Output is correct |
7 |
Correct |
11 ms |
10512 KB |
Output is correct |
8 |
Correct |
11 ms |
10580 KB |
Output is correct |
9 |
Correct |
11 ms |
10580 KB |
Output is correct |
10 |
Correct |
11 ms |
10632 KB |
Output is correct |
11 |
Correct |
11 ms |
10576 KB |
Output is correct |
12 |
Correct |
13 ms |
11532 KB |
Output is correct |
13 |
Correct |
12 ms |
11536 KB |
Output is correct |
14 |
Correct |
12 ms |
11148 KB |
Output is correct |
15 |
Correct |
12 ms |
11272 KB |
Output is correct |
16 |
Correct |
13 ms |
10672 KB |
Output is correct |
17 |
Correct |
12 ms |
10708 KB |
Output is correct |
18 |
Correct |
10 ms |
10580 KB |
Output is correct |
19 |
Correct |
11 ms |
10964 KB |
Output is correct |
20 |
Correct |
10 ms |
11016 KB |
Output is correct |
21 |
Correct |
10 ms |
11092 KB |
Output is correct |
22 |
Correct |
11 ms |
10496 KB |
Output is correct |
23 |
Correct |
8 ms |
10368 KB |
Output is correct |
24 |
Correct |
10 ms |
11024 KB |
Output is correct |
25 |
Correct |
10 ms |
11028 KB |
Output is correct |
26 |
Correct |
10 ms |
11528 KB |
Output is correct |
27 |
Correct |
11 ms |
11016 KB |
Output is correct |
28 |
Correct |
10 ms |
11092 KB |
Output is correct |
29 |
Correct |
11 ms |
11396 KB |
Output is correct |
30 |
Correct |
10 ms |
11656 KB |
Output is correct |
31 |
Correct |
10 ms |
11604 KB |
Output is correct |
32 |
Correct |
10 ms |
10604 KB |
Output is correct |
33 |
Correct |
450 ms |
47868 KB |
Output is correct |
34 |
Correct |
425 ms |
46184 KB |
Output is correct |
35 |
Correct |
402 ms |
44876 KB |
Output is correct |
36 |
Correct |
442 ms |
46628 KB |
Output is correct |
37 |
Correct |
454 ms |
48508 KB |
Output is correct |
38 |
Correct |
392 ms |
42284 KB |
Output is correct |
39 |
Correct |
372 ms |
82612 KB |
Output is correct |
40 |
Correct |
356 ms |
82740 KB |
Output is correct |
41 |
Correct |
397 ms |
85380 KB |
Output is correct |
42 |
Correct |
338 ms |
70180 KB |
Output is correct |
43 |
Correct |
323 ms |
70232 KB |
Output is correct |
44 |
Correct |
404 ms |
48452 KB |
Output is correct |
45 |
Correct |
399 ms |
48348 KB |
Output is correct |
46 |
Correct |
389 ms |
45988 KB |
Output is correct |
47 |
Correct |
327 ms |
61260 KB |
Output is correct |
48 |
Correct |
402 ms |
65072 KB |
Output is correct |
49 |
Correct |
333 ms |
65524 KB |
Output is correct |
50 |
Correct |
180 ms |
39384 KB |
Output is correct |
51 |
Correct |
213 ms |
39400 KB |
Output is correct |
52 |
Correct |
368 ms |
62296 KB |
Output is correct |
53 |
Correct |
389 ms |
66348 KB |
Output is correct |
54 |
Correct |
276 ms |
82756 KB |
Output is correct |
55 |
Correct |
353 ms |
63164 KB |
Output is correct |
56 |
Correct |
336 ms |
77376 KB |
Output is correct |
57 |
Correct |
358 ms |
83848 KB |
Output is correct |
58 |
Correct |
358 ms |
88020 KB |
Output is correct |
59 |
Correct |
407 ms |
88008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9716 KB |
Output is correct |
3 |
Correct |
7 ms |
9684 KB |
Output is correct |
4 |
Correct |
6 ms |
9712 KB |
Output is correct |
5 |
Correct |
12 ms |
10516 KB |
Output is correct |
6 |
Correct |
12 ms |
10512 KB |
Output is correct |
7 |
Correct |
11 ms |
10512 KB |
Output is correct |
8 |
Correct |
11 ms |
10580 KB |
Output is correct |
9 |
Correct |
11 ms |
10580 KB |
Output is correct |
10 |
Correct |
11 ms |
10632 KB |
Output is correct |
11 |
Correct |
11 ms |
10576 KB |
Output is correct |
12 |
Correct |
13 ms |
11532 KB |
Output is correct |
13 |
Correct |
12 ms |
11536 KB |
Output is correct |
14 |
Correct |
12 ms |
11148 KB |
Output is correct |
15 |
Correct |
12 ms |
11272 KB |
Output is correct |
16 |
Correct |
13 ms |
10672 KB |
Output is correct |
17 |
Correct |
12 ms |
10708 KB |
Output is correct |
18 |
Correct |
10 ms |
10580 KB |
Output is correct |
19 |
Correct |
11 ms |
10964 KB |
Output is correct |
20 |
Correct |
10 ms |
11016 KB |
Output is correct |
21 |
Correct |
10 ms |
11092 KB |
Output is correct |
22 |
Correct |
11 ms |
10496 KB |
Output is correct |
23 |
Correct |
8 ms |
10368 KB |
Output is correct |
24 |
Correct |
10 ms |
11024 KB |
Output is correct |
25 |
Correct |
10 ms |
11028 KB |
Output is correct |
26 |
Correct |
10 ms |
11528 KB |
Output is correct |
27 |
Correct |
11 ms |
11016 KB |
Output is correct |
28 |
Correct |
10 ms |
11092 KB |
Output is correct |
29 |
Correct |
11 ms |
11396 KB |
Output is correct |
30 |
Correct |
10 ms |
11656 KB |
Output is correct |
31 |
Correct |
10 ms |
11604 KB |
Output is correct |
32 |
Correct |
10 ms |
10604 KB |
Output is correct |
33 |
Correct |
450 ms |
47868 KB |
Output is correct |
34 |
Correct |
425 ms |
46184 KB |
Output is correct |
35 |
Correct |
402 ms |
44876 KB |
Output is correct |
36 |
Correct |
442 ms |
46628 KB |
Output is correct |
37 |
Correct |
454 ms |
48508 KB |
Output is correct |
38 |
Correct |
392 ms |
42284 KB |
Output is correct |
39 |
Correct |
372 ms |
82612 KB |
Output is correct |
40 |
Correct |
356 ms |
82740 KB |
Output is correct |
41 |
Correct |
397 ms |
85380 KB |
Output is correct |
42 |
Correct |
338 ms |
70180 KB |
Output is correct |
43 |
Correct |
323 ms |
70232 KB |
Output is correct |
44 |
Correct |
404 ms |
48452 KB |
Output is correct |
45 |
Correct |
399 ms |
48348 KB |
Output is correct |
46 |
Correct |
389 ms |
45988 KB |
Output is correct |
47 |
Correct |
327 ms |
61260 KB |
Output is correct |
48 |
Correct |
402 ms |
65072 KB |
Output is correct |
49 |
Correct |
333 ms |
65524 KB |
Output is correct |
50 |
Correct |
180 ms |
39384 KB |
Output is correct |
51 |
Correct |
213 ms |
39400 KB |
Output is correct |
52 |
Correct |
368 ms |
62296 KB |
Output is correct |
53 |
Correct |
389 ms |
66348 KB |
Output is correct |
54 |
Correct |
276 ms |
82756 KB |
Output is correct |
55 |
Correct |
353 ms |
63164 KB |
Output is correct |
56 |
Correct |
336 ms |
77376 KB |
Output is correct |
57 |
Correct |
358 ms |
83848 KB |
Output is correct |
58 |
Correct |
358 ms |
88020 KB |
Output is correct |
59 |
Correct |
407 ms |
88008 KB |
Output is correct |
60 |
Correct |
5 ms |
9684 KB |
Output is correct |
61 |
Correct |
5 ms |
9696 KB |
Output is correct |
62 |
Correct |
5 ms |
9684 KB |
Output is correct |
63 |
Incorrect |
6 ms |
9684 KB |
Output isn't correct |
64 |
Halted |
0 ms |
0 KB |
- |