#include "bits/stdc++.h"
using namespace std;
#define ar array
typedef long long ll;
const int N = 2e5 + 5;
vector<int> edges[N];
map<int, ll> dp[N];
int a[N], h[N], c[N];
int used[N], is[N];
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n; cin>>n;
vector<int> p(n);
for(int i=0;i<n;i++){
cin>>a[i]>>h[i]>>c[i];
a[i]--; p[i] = i;
} sort(p.begin(), p.end(), [&](int i, int j) { return (h[i] < h[j]); });
for(int i=0, last=0;i<n;){
int j = i;
while(j<n && h[p[i]] == h[p[j]]) j++;
while(i < j) h[p[i]] = last, i++;
last++;
}
vector<vector<int>> cc;
for(int i=0;i<n;i++){
if(used[i]) continue;
vector<int> ss;
int u = i;
while(!used[u]){
used[u] = 1;
ss.push_back(u);
u = a[u];
}
vector<int> tmp = {u};
while(!ss.empty() && ss.back() != u){
tmp.push_back(ss.back());
ss.pop_back();
}
if(ss.empty()) continue;
for(auto x : tmp) is[x] = 1;
cc.push_back(tmp);
} for(int i=0;i<n;i++){
if(is[i] && is[a[i]]) continue;
edges[a[i]].push_back(i);
}
function<void(int)> dfs = [&](int u){
//~ dp[u][h[u]] = c[u];
for(auto x : edges[u]){
dfs(x);
if((int)dp[u].size() < (int)dp[x].size()) swap(dp[u], dp[x]);
for(auto x : dp[x]){
dp[u][x.first] += x.second;
}
}
if(!is[u]){
dp[u][h[u]] += c[u];
auto it = dp[u].lower_bound(h[u]);
int tot = c[u];
vector<int> er;
while(it != dp[u].begin()){
it--;
if((*it).second <= tot){
er.push_back((*it).first);
tot -= (*it).second, (*it).second = 0;
} else {
(*it).second -= tot;
break;
}
} for(auto x : er) dp[u].erase(x);
}
};
for(int i=0;i<n;i++){
if(!is[i]) continue;
dfs(i);
}
ll res = accumulate(c, c+n, 0ll);
for(auto v : cc){
ll tot = 0, mx = 0;
vector<int> cmp;
map<int, ll> tt;
for(auto u : v){
for(auto x : dp[u]) tt[x.first] += x.second;
tt[h[u]] += 0;
}
for(auto x : tt) cmp.push_back(x.first);
reverse(cmp.begin(), cmp.end());
sort(v.begin(), v.end(), [&](int i, int j){
return (h[i] > h[j]);
});
int i = 0;
for(auto x : cmp){
tot += tt[x];
ll cc = 0;
while(i<(int)v.size() && h[v[i]] == x) cc += c[v[i]], i++;
mx = max(mx, tot + cc);
} res -= mx;
}
cout<<res<<"\n";
}
/*
6
1 6 5
1 3 6
1 8 4
3 4 9
2 2 5
2 5 6
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14412 KB |
Output is correct |
2 |
Correct |
7 ms |
14412 KB |
Output is correct |
3 |
Correct |
7 ms |
14352 KB |
Output is correct |
4 |
Correct |
8 ms |
14388 KB |
Output is correct |
5 |
Correct |
13 ms |
15948 KB |
Output is correct |
6 |
Correct |
16 ms |
15052 KB |
Output is correct |
7 |
Correct |
10 ms |
14924 KB |
Output is correct |
8 |
Correct |
13 ms |
15872 KB |
Output is correct |
9 |
Correct |
14 ms |
15052 KB |
Output is correct |
10 |
Correct |
11 ms |
14940 KB |
Output is correct |
11 |
Correct |
9 ms |
14756 KB |
Output is correct |
12 |
Correct |
11 ms |
15428 KB |
Output is correct |
13 |
Correct |
9 ms |
15400 KB |
Output is correct |
14 |
Correct |
11 ms |
15088 KB |
Output is correct |
15 |
Correct |
10 ms |
15016 KB |
Output is correct |
16 |
Correct |
14 ms |
16204 KB |
Output is correct |
17 |
Correct |
11 ms |
15276 KB |
Output is correct |
18 |
Correct |
9 ms |
14668 KB |
Output is correct |
19 |
Correct |
12 ms |
15432 KB |
Output is correct |
20 |
Correct |
10 ms |
15052 KB |
Output is correct |
21 |
Correct |
10 ms |
15148 KB |
Output is correct |
22 |
Correct |
12 ms |
15476 KB |
Output is correct |
23 |
Correct |
9 ms |
14884 KB |
Output is correct |
24 |
Correct |
12 ms |
15436 KB |
Output is correct |
25 |
Correct |
13 ms |
15180 KB |
Output is correct |
26 |
Correct |
10 ms |
15436 KB |
Output is correct |
27 |
Correct |
13 ms |
15564 KB |
Output is correct |
28 |
Correct |
13 ms |
15692 KB |
Output is correct |
29 |
Correct |
13 ms |
15840 KB |
Output is correct |
30 |
Correct |
12 ms |
15820 KB |
Output is correct |
31 |
Correct |
14 ms |
15916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14412 KB |
Output is correct |
2 |
Correct |
7 ms |
14412 KB |
Output is correct |
3 |
Correct |
7 ms |
14352 KB |
Output is correct |
4 |
Correct |
8 ms |
14388 KB |
Output is correct |
5 |
Correct |
13 ms |
15948 KB |
Output is correct |
6 |
Correct |
16 ms |
15052 KB |
Output is correct |
7 |
Correct |
10 ms |
14924 KB |
Output is correct |
8 |
Correct |
13 ms |
15872 KB |
Output is correct |
9 |
Correct |
14 ms |
15052 KB |
Output is correct |
10 |
Correct |
11 ms |
14940 KB |
Output is correct |
11 |
Correct |
9 ms |
14756 KB |
Output is correct |
12 |
Correct |
11 ms |
15428 KB |
Output is correct |
13 |
Correct |
9 ms |
15400 KB |
Output is correct |
14 |
Correct |
11 ms |
15088 KB |
Output is correct |
15 |
Correct |
10 ms |
15016 KB |
Output is correct |
16 |
Correct |
14 ms |
16204 KB |
Output is correct |
17 |
Correct |
11 ms |
15276 KB |
Output is correct |
18 |
Correct |
9 ms |
14668 KB |
Output is correct |
19 |
Correct |
12 ms |
15432 KB |
Output is correct |
20 |
Correct |
10 ms |
15052 KB |
Output is correct |
21 |
Correct |
10 ms |
15148 KB |
Output is correct |
22 |
Correct |
12 ms |
15476 KB |
Output is correct |
23 |
Correct |
9 ms |
14884 KB |
Output is correct |
24 |
Correct |
12 ms |
15436 KB |
Output is correct |
25 |
Correct |
13 ms |
15180 KB |
Output is correct |
26 |
Correct |
10 ms |
15436 KB |
Output is correct |
27 |
Correct |
13 ms |
15564 KB |
Output is correct |
28 |
Correct |
13 ms |
15692 KB |
Output is correct |
29 |
Correct |
13 ms |
15840 KB |
Output is correct |
30 |
Correct |
12 ms |
15820 KB |
Output is correct |
31 |
Correct |
14 ms |
15916 KB |
Output is correct |
32 |
Correct |
13 ms |
15848 KB |
Output is correct |
33 |
Correct |
401 ms |
86744 KB |
Output is correct |
34 |
Correct |
258 ms |
51188 KB |
Output is correct |
35 |
Correct |
377 ms |
83764 KB |
Output is correct |
36 |
Correct |
257 ms |
51128 KB |
Output is correct |
37 |
Correct |
221 ms |
32072 KB |
Output is correct |
38 |
Correct |
150 ms |
27784 KB |
Output is correct |
39 |
Correct |
188 ms |
55936 KB |
Output is correct |
40 |
Correct |
168 ms |
55960 KB |
Output is correct |
41 |
Correct |
120 ms |
55784 KB |
Output is correct |
42 |
Correct |
175 ms |
40424 KB |
Output is correct |
43 |
Correct |
180 ms |
40308 KB |
Output is correct |
44 |
Correct |
379 ms |
111292 KB |
Output is correct |
45 |
Correct |
233 ms |
65348 KB |
Output is correct |
46 |
Correct |
98 ms |
27700 KB |
Output is correct |
47 |
Correct |
299 ms |
57936 KB |
Output is correct |
48 |
Correct |
131 ms |
43404 KB |
Output is correct |
49 |
Correct |
112 ms |
43320 KB |
Output is correct |
50 |
Correct |
363 ms |
58192 KB |
Output is correct |
51 |
Correct |
110 ms |
31700 KB |
Output is correct |
52 |
Correct |
358 ms |
58868 KB |
Output is correct |
53 |
Correct |
134 ms |
43788 KB |
Output is correct |
54 |
Correct |
124 ms |
55824 KB |
Output is correct |
55 |
Correct |
258 ms |
62952 KB |
Output is correct |
56 |
Correct |
243 ms |
70644 KB |
Output is correct |
57 |
Correct |
253 ms |
74536 KB |
Output is correct |
58 |
Correct |
265 ms |
72624 KB |
Output is correct |
59 |
Correct |
269 ms |
72664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14412 KB |
Output is correct |
2 |
Correct |
7 ms |
14412 KB |
Output is correct |
3 |
Correct |
7 ms |
14352 KB |
Output is correct |
4 |
Correct |
8 ms |
14388 KB |
Output is correct |
5 |
Correct |
13 ms |
15948 KB |
Output is correct |
6 |
Correct |
16 ms |
15052 KB |
Output is correct |
7 |
Correct |
10 ms |
14924 KB |
Output is correct |
8 |
Correct |
13 ms |
15872 KB |
Output is correct |
9 |
Correct |
14 ms |
15052 KB |
Output is correct |
10 |
Correct |
11 ms |
14940 KB |
Output is correct |
11 |
Correct |
9 ms |
14756 KB |
Output is correct |
12 |
Correct |
11 ms |
15428 KB |
Output is correct |
13 |
Correct |
9 ms |
15400 KB |
Output is correct |
14 |
Correct |
11 ms |
15088 KB |
Output is correct |
15 |
Correct |
10 ms |
15016 KB |
Output is correct |
16 |
Correct |
14 ms |
16204 KB |
Output is correct |
17 |
Correct |
11 ms |
15276 KB |
Output is correct |
18 |
Correct |
9 ms |
14668 KB |
Output is correct |
19 |
Correct |
12 ms |
15432 KB |
Output is correct |
20 |
Correct |
10 ms |
15052 KB |
Output is correct |
21 |
Correct |
10 ms |
15148 KB |
Output is correct |
22 |
Correct |
12 ms |
15476 KB |
Output is correct |
23 |
Correct |
9 ms |
14884 KB |
Output is correct |
24 |
Correct |
12 ms |
15436 KB |
Output is correct |
25 |
Correct |
13 ms |
15180 KB |
Output is correct |
26 |
Correct |
10 ms |
15436 KB |
Output is correct |
27 |
Correct |
13 ms |
15564 KB |
Output is correct |
28 |
Correct |
13 ms |
15692 KB |
Output is correct |
29 |
Correct |
13 ms |
15840 KB |
Output is correct |
30 |
Correct |
12 ms |
15820 KB |
Output is correct |
31 |
Correct |
14 ms |
15916 KB |
Output is correct |
32 |
Correct |
13 ms |
15848 KB |
Output is correct |
33 |
Correct |
401 ms |
86744 KB |
Output is correct |
34 |
Correct |
258 ms |
51188 KB |
Output is correct |
35 |
Correct |
377 ms |
83764 KB |
Output is correct |
36 |
Correct |
257 ms |
51128 KB |
Output is correct |
37 |
Correct |
221 ms |
32072 KB |
Output is correct |
38 |
Correct |
150 ms |
27784 KB |
Output is correct |
39 |
Correct |
188 ms |
55936 KB |
Output is correct |
40 |
Correct |
168 ms |
55960 KB |
Output is correct |
41 |
Correct |
120 ms |
55784 KB |
Output is correct |
42 |
Correct |
175 ms |
40424 KB |
Output is correct |
43 |
Correct |
180 ms |
40308 KB |
Output is correct |
44 |
Correct |
379 ms |
111292 KB |
Output is correct |
45 |
Correct |
233 ms |
65348 KB |
Output is correct |
46 |
Correct |
98 ms |
27700 KB |
Output is correct |
47 |
Correct |
299 ms |
57936 KB |
Output is correct |
48 |
Correct |
131 ms |
43404 KB |
Output is correct |
49 |
Correct |
112 ms |
43320 KB |
Output is correct |
50 |
Correct |
363 ms |
58192 KB |
Output is correct |
51 |
Correct |
110 ms |
31700 KB |
Output is correct |
52 |
Correct |
358 ms |
58868 KB |
Output is correct |
53 |
Correct |
134 ms |
43788 KB |
Output is correct |
54 |
Correct |
124 ms |
55824 KB |
Output is correct |
55 |
Correct |
258 ms |
62952 KB |
Output is correct |
56 |
Correct |
243 ms |
70644 KB |
Output is correct |
57 |
Correct |
253 ms |
74536 KB |
Output is correct |
58 |
Correct |
265 ms |
72624 KB |
Output is correct |
59 |
Correct |
269 ms |
72664 KB |
Output is correct |
60 |
Correct |
7 ms |
14412 KB |
Output is correct |
61 |
Correct |
7 ms |
14404 KB |
Output is correct |
62 |
Correct |
9 ms |
14340 KB |
Output is correct |
63 |
Correct |
10 ms |
14412 KB |
Output is correct |
64 |
Correct |
368 ms |
74400 KB |
Output is correct |
65 |
Correct |
263 ms |
49848 KB |
Output is correct |
66 |
Correct |
332 ms |
69796 KB |
Output is correct |
67 |
Correct |
285 ms |
49704 KB |
Output is correct |
68 |
Correct |
183 ms |
36164 KB |
Output is correct |
69 |
Correct |
159 ms |
32796 KB |
Output is correct |
70 |
Correct |
242 ms |
35696 KB |
Output is correct |
71 |
Correct |
121 ms |
25676 KB |
Output is correct |
72 |
Correct |
281 ms |
40196 KB |
Output is correct |
73 |
Correct |
119 ms |
26640 KB |
Output is correct |
74 |
Correct |
329 ms |
48376 KB |
Output is correct |
75 |
Correct |
169 ms |
34496 KB |
Output is correct |
76 |
Correct |
145 ms |
34332 KB |
Output is correct |
77 |
Correct |
277 ms |
52000 KB |
Output is correct |
78 |
Correct |
143 ms |
31704 KB |
Output is correct |
79 |
Correct |
336 ms |
66248 KB |
Output is correct |
80 |
Correct |
186 ms |
42712 KB |
Output is correct |
81 |
Correct |
137 ms |
32056 KB |
Output is correct |
82 |
Correct |
197 ms |
40388 KB |
Output is correct |
83 |
Correct |
132 ms |
35140 KB |
Output is correct |
84 |
Correct |
292 ms |
57340 KB |
Output is correct |
85 |
Correct |
275 ms |
57336 KB |
Output is correct |
86 |
Correct |
272 ms |
54256 KB |
Output is correct |
87 |
Correct |
262 ms |
57228 KB |
Output is correct |
88 |
Correct |
266 ms |
57536 KB |
Output is correct |