#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+10;
map<int, int> mp[N];
int n, a[N], h[N], c[N], sz[N];
vector<int> g[N];
void dfs_sz(int u){
sz[u]=1;
for (int &v:g[u]){
dfs_sz(v);
sz[u]+=sz[v];
if (sz[v]>sz[g[u][0]]) swap(v, g[u][0]);
}
}
void dfs_merge(int u){
if (g[u].empty()){
mp[u][1]=c[u];
mp[u][h[u]+1]=-c[u];
return;
}
for (int &v:g[u]){
dfs_merge(v);
if (v==g[u][0]) mp[u].swap(mp[v]);
else{
for (auto &i:mp[v]){
mp[u][i.first]+=i.second;
}
}
}
mp[u][h[u]+1]-=c[u];
auto it=mp[u].find(h[u]+1);
int cur=c[u];
while (it!=mp[u].begin()){
--it;
if (it->second<0 && cur>=-it->second){
cur-=-it->second;
it=mp[u].erase(it);
}else{
it->second+=cur;
cur=0;
break;
}
}
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
vector<int> vals{-1};
for (int i=1; i<=n; ++i){
cin >> a[i] >> h[i] >> c[i];
vals.push_back(h[i]);
if (a[i]!=i) g[a[i]].push_back(i);
}
sort(vals.begin(), vals.end());
vals.resize(unique(vals.begin(), vals.end())-vals.begin());
for (int i=1; i<=n; ++i) h[i]=lower_bound(vals.begin(), vals.end(), h[i])-vals.begin();
dfs_sz(1);
dfs_merge(1);
cout << accumulate(c+1, c+n+1, 0ll)-mp[1][1] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20572 KB |
Output is correct |
2 |
Correct |
4 ms |
20572 KB |
Output is correct |
3 |
Correct |
5 ms |
20572 KB |
Output is correct |
4 |
Correct |
4 ms |
20572 KB |
Output is correct |
5 |
Correct |
10 ms |
22108 KB |
Output is correct |
6 |
Correct |
8 ms |
21596 KB |
Output is correct |
7 |
Correct |
7 ms |
21540 KB |
Output is correct |
8 |
Correct |
10 ms |
22096 KB |
Output is correct |
9 |
Correct |
8 ms |
21596 KB |
Output is correct |
10 |
Correct |
7 ms |
21340 KB |
Output is correct |
11 |
Correct |
7 ms |
21340 KB |
Output is correct |
12 |
Correct |
9 ms |
21920 KB |
Output is correct |
13 |
Correct |
7 ms |
22104 KB |
Output is correct |
14 |
Correct |
7 ms |
21340 KB |
Output is correct |
15 |
Correct |
7 ms |
21340 KB |
Output is correct |
16 |
Correct |
10 ms |
22412 KB |
Output is correct |
17 |
Correct |
9 ms |
21848 KB |
Output is correct |
18 |
Correct |
6 ms |
21340 KB |
Output is correct |
19 |
Correct |
8 ms |
21852 KB |
Output is correct |
20 |
Correct |
7 ms |
21588 KB |
Output is correct |
21 |
Correct |
7 ms |
21608 KB |
Output is correct |
22 |
Correct |
9 ms |
21860 KB |
Output is correct |
23 |
Correct |
7 ms |
21544 KB |
Output is correct |
24 |
Correct |
8 ms |
21860 KB |
Output is correct |
25 |
Correct |
8 ms |
21796 KB |
Output is correct |
26 |
Correct |
7 ms |
21860 KB |
Output is correct |
27 |
Correct |
10 ms |
21856 KB |
Output is correct |
28 |
Correct |
8 ms |
22024 KB |
Output is correct |
29 |
Correct |
8 ms |
21860 KB |
Output is correct |
30 |
Correct |
8 ms |
21804 KB |
Output is correct |
31 |
Correct |
8 ms |
21860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20572 KB |
Output is correct |
2 |
Correct |
4 ms |
20572 KB |
Output is correct |
3 |
Correct |
5 ms |
20572 KB |
Output is correct |
4 |
Correct |
4 ms |
20572 KB |
Output is correct |
5 |
Correct |
10 ms |
22108 KB |
Output is correct |
6 |
Correct |
8 ms |
21596 KB |
Output is correct |
7 |
Correct |
7 ms |
21540 KB |
Output is correct |
8 |
Correct |
10 ms |
22096 KB |
Output is correct |
9 |
Correct |
8 ms |
21596 KB |
Output is correct |
10 |
Correct |
7 ms |
21340 KB |
Output is correct |
11 |
Correct |
7 ms |
21340 KB |
Output is correct |
12 |
Correct |
9 ms |
21920 KB |
Output is correct |
13 |
Correct |
7 ms |
22104 KB |
Output is correct |
14 |
Correct |
7 ms |
21340 KB |
Output is correct |
15 |
Correct |
7 ms |
21340 KB |
Output is correct |
16 |
Correct |
10 ms |
22412 KB |
Output is correct |
17 |
Correct |
9 ms |
21848 KB |
Output is correct |
18 |
Correct |
6 ms |
21340 KB |
Output is correct |
19 |
Correct |
8 ms |
21852 KB |
Output is correct |
20 |
Correct |
7 ms |
21588 KB |
Output is correct |
21 |
Correct |
7 ms |
21608 KB |
Output is correct |
22 |
Correct |
9 ms |
21860 KB |
Output is correct |
23 |
Correct |
7 ms |
21544 KB |
Output is correct |
24 |
Correct |
8 ms |
21860 KB |
Output is correct |
25 |
Correct |
8 ms |
21796 KB |
Output is correct |
26 |
Correct |
7 ms |
21860 KB |
Output is correct |
27 |
Correct |
10 ms |
21856 KB |
Output is correct |
28 |
Correct |
8 ms |
22024 KB |
Output is correct |
29 |
Correct |
8 ms |
21860 KB |
Output is correct |
30 |
Correct |
8 ms |
21804 KB |
Output is correct |
31 |
Correct |
8 ms |
21860 KB |
Output is correct |
32 |
Correct |
9 ms |
22120 KB |
Output is correct |
33 |
Correct |
363 ms |
90960 KB |
Output is correct |
34 |
Correct |
249 ms |
66316 KB |
Output is correct |
35 |
Correct |
352 ms |
87904 KB |
Output is correct |
36 |
Correct |
238 ms |
66496 KB |
Output is correct |
37 |
Correct |
154 ms |
47896 KB |
Output is correct |
38 |
Correct |
170 ms |
43616 KB |
Output is correct |
39 |
Correct |
156 ms |
64956 KB |
Output is correct |
40 |
Correct |
130 ms |
64960 KB |
Output is correct |
41 |
Correct |
91 ms |
64864 KB |
Output is correct |
42 |
Correct |
156 ms |
49484 KB |
Output is correct |
43 |
Correct |
122 ms |
49448 KB |
Output is correct |
44 |
Correct |
322 ms |
118464 KB |
Output is correct |
45 |
Correct |
231 ms |
82592 KB |
Output is correct |
46 |
Correct |
94 ms |
42944 KB |
Output is correct |
47 |
Correct |
213 ms |
65468 KB |
Output is correct |
48 |
Correct |
131 ms |
58696 KB |
Output is correct |
49 |
Correct |
89 ms |
58756 KB |
Output is correct |
50 |
Correct |
247 ms |
65708 KB |
Output is correct |
51 |
Correct |
111 ms |
53396 KB |
Output is correct |
52 |
Correct |
221 ms |
66752 KB |
Output is correct |
53 |
Correct |
119 ms |
59584 KB |
Output is correct |
54 |
Correct |
107 ms |
65164 KB |
Output is correct |
55 |
Correct |
203 ms |
67880 KB |
Output is correct |
56 |
Correct |
171 ms |
70880 KB |
Output is correct |
57 |
Correct |
166 ms |
72908 KB |
Output is correct |
58 |
Correct |
178 ms |
67456 KB |
Output is correct |
59 |
Correct |
176 ms |
67260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20572 KB |
Output is correct |
2 |
Correct |
4 ms |
20572 KB |
Output is correct |
3 |
Correct |
5 ms |
20572 KB |
Output is correct |
4 |
Correct |
4 ms |
20572 KB |
Output is correct |
5 |
Correct |
10 ms |
22108 KB |
Output is correct |
6 |
Correct |
8 ms |
21596 KB |
Output is correct |
7 |
Correct |
7 ms |
21540 KB |
Output is correct |
8 |
Correct |
10 ms |
22096 KB |
Output is correct |
9 |
Correct |
8 ms |
21596 KB |
Output is correct |
10 |
Correct |
7 ms |
21340 KB |
Output is correct |
11 |
Correct |
7 ms |
21340 KB |
Output is correct |
12 |
Correct |
9 ms |
21920 KB |
Output is correct |
13 |
Correct |
7 ms |
22104 KB |
Output is correct |
14 |
Correct |
7 ms |
21340 KB |
Output is correct |
15 |
Correct |
7 ms |
21340 KB |
Output is correct |
16 |
Correct |
10 ms |
22412 KB |
Output is correct |
17 |
Correct |
9 ms |
21848 KB |
Output is correct |
18 |
Correct |
6 ms |
21340 KB |
Output is correct |
19 |
Correct |
8 ms |
21852 KB |
Output is correct |
20 |
Correct |
7 ms |
21588 KB |
Output is correct |
21 |
Correct |
7 ms |
21608 KB |
Output is correct |
22 |
Correct |
9 ms |
21860 KB |
Output is correct |
23 |
Correct |
7 ms |
21544 KB |
Output is correct |
24 |
Correct |
8 ms |
21860 KB |
Output is correct |
25 |
Correct |
8 ms |
21796 KB |
Output is correct |
26 |
Correct |
7 ms |
21860 KB |
Output is correct |
27 |
Correct |
10 ms |
21856 KB |
Output is correct |
28 |
Correct |
8 ms |
22024 KB |
Output is correct |
29 |
Correct |
8 ms |
21860 KB |
Output is correct |
30 |
Correct |
8 ms |
21804 KB |
Output is correct |
31 |
Correct |
8 ms |
21860 KB |
Output is correct |
32 |
Correct |
9 ms |
22120 KB |
Output is correct |
33 |
Correct |
363 ms |
90960 KB |
Output is correct |
34 |
Correct |
249 ms |
66316 KB |
Output is correct |
35 |
Correct |
352 ms |
87904 KB |
Output is correct |
36 |
Correct |
238 ms |
66496 KB |
Output is correct |
37 |
Correct |
154 ms |
47896 KB |
Output is correct |
38 |
Correct |
170 ms |
43616 KB |
Output is correct |
39 |
Correct |
156 ms |
64956 KB |
Output is correct |
40 |
Correct |
130 ms |
64960 KB |
Output is correct |
41 |
Correct |
91 ms |
64864 KB |
Output is correct |
42 |
Correct |
156 ms |
49484 KB |
Output is correct |
43 |
Correct |
122 ms |
49448 KB |
Output is correct |
44 |
Correct |
322 ms |
118464 KB |
Output is correct |
45 |
Correct |
231 ms |
82592 KB |
Output is correct |
46 |
Correct |
94 ms |
42944 KB |
Output is correct |
47 |
Correct |
213 ms |
65468 KB |
Output is correct |
48 |
Correct |
131 ms |
58696 KB |
Output is correct |
49 |
Correct |
89 ms |
58756 KB |
Output is correct |
50 |
Correct |
247 ms |
65708 KB |
Output is correct |
51 |
Correct |
111 ms |
53396 KB |
Output is correct |
52 |
Correct |
221 ms |
66752 KB |
Output is correct |
53 |
Correct |
119 ms |
59584 KB |
Output is correct |
54 |
Correct |
107 ms |
65164 KB |
Output is correct |
55 |
Correct |
203 ms |
67880 KB |
Output is correct |
56 |
Correct |
171 ms |
70880 KB |
Output is correct |
57 |
Correct |
166 ms |
72908 KB |
Output is correct |
58 |
Correct |
178 ms |
67456 KB |
Output is correct |
59 |
Correct |
176 ms |
67260 KB |
Output is correct |
60 |
Runtime error |
396 ms |
524288 KB |
Execution killed with signal 9 |
61 |
Halted |
0 ms |
0 KB |
- |