#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
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14412 KB |
Output is correct |
2 |
Correct |
7 ms |
14412 KB |
Output is correct |
3 |
Correct |
8 ms |
14432 KB |
Output is correct |
4 |
Correct |
7 ms |
14404 KB |
Output is correct |
5 |
Correct |
14 ms |
16076 KB |
Output is correct |
6 |
Correct |
12 ms |
15180 KB |
Output is correct |
7 |
Correct |
14 ms |
14872 KB |
Output is correct |
8 |
Correct |
15 ms |
16260 KB |
Output is correct |
9 |
Correct |
11 ms |
15180 KB |
Output is correct |
10 |
Correct |
11 ms |
14924 KB |
Output is correct |
11 |
Correct |
10 ms |
14676 KB |
Output is correct |
12 |
Correct |
17 ms |
16092 KB |
Output is correct |
13 |
Correct |
9 ms |
15468 KB |
Output is correct |
14 |
Correct |
14 ms |
15904 KB |
Output is correct |
15 |
Correct |
10 ms |
14996 KB |
Output is correct |
16 |
Correct |
15 ms |
16716 KB |
Output is correct |
17 |
Correct |
17 ms |
15552 KB |
Output is correct |
18 |
Correct |
14 ms |
14668 KB |
Output is correct |
19 |
Correct |
14 ms |
15776 KB |
Output is correct |
20 |
Correct |
10 ms |
15084 KB |
Output is correct |
21 |
Correct |
10 ms |
15052 KB |
Output is correct |
22 |
Correct |
12 ms |
15488 KB |
Output is correct |
23 |
Correct |
10 ms |
14796 KB |
Output is correct |
24 |
Correct |
12 ms |
15820 KB |
Output is correct |
25 |
Correct |
11 ms |
15180 KB |
Output is correct |
26 |
Correct |
92 ms |
16128 KB |
Output is correct |
27 |
Correct |
17 ms |
15680 KB |
Output is correct |
28 |
Correct |
28 ms |
15820 KB |
Output is correct |
29 |
Correct |
20 ms |
16012 KB |
Output is correct |
30 |
Correct |
13 ms |
15820 KB |
Output is correct |
31 |
Correct |
17 ms |
15820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14412 KB |
Output is correct |
2 |
Correct |
7 ms |
14412 KB |
Output is correct |
3 |
Correct |
8 ms |
14432 KB |
Output is correct |
4 |
Correct |
7 ms |
14404 KB |
Output is correct |
5 |
Correct |
14 ms |
16076 KB |
Output is correct |
6 |
Correct |
12 ms |
15180 KB |
Output is correct |
7 |
Correct |
14 ms |
14872 KB |
Output is correct |
8 |
Correct |
15 ms |
16260 KB |
Output is correct |
9 |
Correct |
11 ms |
15180 KB |
Output is correct |
10 |
Correct |
11 ms |
14924 KB |
Output is correct |
11 |
Correct |
10 ms |
14676 KB |
Output is correct |
12 |
Correct |
17 ms |
16092 KB |
Output is correct |
13 |
Correct |
9 ms |
15468 KB |
Output is correct |
14 |
Correct |
14 ms |
15904 KB |
Output is correct |
15 |
Correct |
10 ms |
14996 KB |
Output is correct |
16 |
Correct |
15 ms |
16716 KB |
Output is correct |
17 |
Correct |
17 ms |
15552 KB |
Output is correct |
18 |
Correct |
14 ms |
14668 KB |
Output is correct |
19 |
Correct |
14 ms |
15776 KB |
Output is correct |
20 |
Correct |
10 ms |
15084 KB |
Output is correct |
21 |
Correct |
10 ms |
15052 KB |
Output is correct |
22 |
Correct |
12 ms |
15488 KB |
Output is correct |
23 |
Correct |
10 ms |
14796 KB |
Output is correct |
24 |
Correct |
12 ms |
15820 KB |
Output is correct |
25 |
Correct |
11 ms |
15180 KB |
Output is correct |
26 |
Correct |
92 ms |
16128 KB |
Output is correct |
27 |
Correct |
17 ms |
15680 KB |
Output is correct |
28 |
Correct |
28 ms |
15820 KB |
Output is correct |
29 |
Correct |
20 ms |
16012 KB |
Output is correct |
30 |
Correct |
13 ms |
15820 KB |
Output is correct |
31 |
Correct |
17 ms |
15820 KB |
Output is correct |
32 |
Correct |
14 ms |
16116 KB |
Output is correct |
33 |
Correct |
426 ms |
103988 KB |
Output is correct |
34 |
Correct |
276 ms |
57192 KB |
Output is correct |
35 |
Correct |
422 ms |
100108 KB |
Output is correct |
36 |
Correct |
300 ms |
57192 KB |
Output is correct |
37 |
Correct |
164 ms |
33060 KB |
Output is correct |
38 |
Correct |
145 ms |
27704 KB |
Output is correct |
39 |
Execution timed out |
2061 ms |
67228 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14412 KB |
Output is correct |
2 |
Correct |
7 ms |
14412 KB |
Output is correct |
3 |
Correct |
8 ms |
14432 KB |
Output is correct |
4 |
Correct |
7 ms |
14404 KB |
Output is correct |
5 |
Correct |
14 ms |
16076 KB |
Output is correct |
6 |
Correct |
12 ms |
15180 KB |
Output is correct |
7 |
Correct |
14 ms |
14872 KB |
Output is correct |
8 |
Correct |
15 ms |
16260 KB |
Output is correct |
9 |
Correct |
11 ms |
15180 KB |
Output is correct |
10 |
Correct |
11 ms |
14924 KB |
Output is correct |
11 |
Correct |
10 ms |
14676 KB |
Output is correct |
12 |
Correct |
17 ms |
16092 KB |
Output is correct |
13 |
Correct |
9 ms |
15468 KB |
Output is correct |
14 |
Correct |
14 ms |
15904 KB |
Output is correct |
15 |
Correct |
10 ms |
14996 KB |
Output is correct |
16 |
Correct |
15 ms |
16716 KB |
Output is correct |
17 |
Correct |
17 ms |
15552 KB |
Output is correct |
18 |
Correct |
14 ms |
14668 KB |
Output is correct |
19 |
Correct |
14 ms |
15776 KB |
Output is correct |
20 |
Correct |
10 ms |
15084 KB |
Output is correct |
21 |
Correct |
10 ms |
15052 KB |
Output is correct |
22 |
Correct |
12 ms |
15488 KB |
Output is correct |
23 |
Correct |
10 ms |
14796 KB |
Output is correct |
24 |
Correct |
12 ms |
15820 KB |
Output is correct |
25 |
Correct |
11 ms |
15180 KB |
Output is correct |
26 |
Correct |
92 ms |
16128 KB |
Output is correct |
27 |
Correct |
17 ms |
15680 KB |
Output is correct |
28 |
Correct |
28 ms |
15820 KB |
Output is correct |
29 |
Correct |
20 ms |
16012 KB |
Output is correct |
30 |
Correct |
13 ms |
15820 KB |
Output is correct |
31 |
Correct |
17 ms |
15820 KB |
Output is correct |
32 |
Correct |
14 ms |
16116 KB |
Output is correct |
33 |
Correct |
426 ms |
103988 KB |
Output is correct |
34 |
Correct |
276 ms |
57192 KB |
Output is correct |
35 |
Correct |
422 ms |
100108 KB |
Output is correct |
36 |
Correct |
300 ms |
57192 KB |
Output is correct |
37 |
Correct |
164 ms |
33060 KB |
Output is correct |
38 |
Correct |
145 ms |
27704 KB |
Output is correct |
39 |
Execution timed out |
2061 ms |
67228 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |