#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 2e5;
ll v[N],v2[N],cost[N];
vector <ll> e[N];
ll vis[N];
vector <ll> chain;
map <ll,ll> costs[N];
map <ll,ll> res;
map <ll,ll> sv;
void dfs(ll node, ll p){
//cout<<node<<' '<<p<<'\n';
vis[node] = 2;
for(auto i:e[node]){
if(i == p || vis[i] == 2)continue;
dfs(i,node);
if(costs[node].size() < costs[i].size()){
swap(costs[node],costs[i]);
}
for(auto j:costs[i]){
costs[node][j.first]+=j.second;
}
costs[i].clear();
}
if(p != -1){
ll x = cost[node];
while(x > 0){
auto it = costs[node].lower_bound(v2[node]);
if(it == costs[node].begin())break;
it = prev(it);
ll nr = min(x,it->second);
x-=nr;
it->second-=nr;
if(it->second == 0)costs[node].erase(it->first);
}
costs[node][v2[node]]+=cost[node];
}
}
int main(){
ll n;
ll ans2 = 0,sum = 0;
cin>>n;
for(ll i = 0;i < n;i++){
cin>>v[i]>>v2[i]>>cost[i];
sum+=cost[i];
v[i]--;
e[v[i]].push_back(i);
}
for(ll i = 0;i < n;i++){
if(vis[i])continue;
chain.clear();
ll nr = i;
while(vis[nr] == 0){
vis[nr] = 1;
nr = v[nr];
}
while(vis[nr] == 1){
chain.push_back(nr);
vis[nr] = 2;
nr = v[nr];
}
res.clear();
sv.clear();
ll ans = 0;
for(auto j:chain){
dfs(j,-1);
for(auto k:costs[j]){
res[k.first]+=k.second;
}
sv[v2[j]]+=cost[j];
}
if(!res.empty()){
ll sum2 = 0;
for(auto k = prev(res.end());1;k--){
sum2+=k->second;
k->second = sum2;
if(k == res.begin())break;
}
ans = max(ans,sum2);
for(auto k = prev(sv.end());1;k--){
auto it = res.lower_bound(k->first);
if(it == res.end()){
ans = max(ans,k->second);
}else{
ans = max(ans,k->second + it->second);
}
if(k == sv.begin())break;
}
}else{
for(auto k:sv){
ans = max(ans,k.second);
}
}
//cout<<ans<<'\n';
ans2+=ans;
}
cout<<sum - ans2<<'\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
19036 KB |
Output is correct |
2 |
Correct |
3 ms |
19036 KB |
Output is correct |
3 |
Correct |
2 ms |
19072 KB |
Output is correct |
4 |
Correct |
3 ms |
19036 KB |
Output is correct |
5 |
Correct |
8 ms |
19808 KB |
Output is correct |
6 |
Correct |
9 ms |
19348 KB |
Output is correct |
7 |
Correct |
6 ms |
19100 KB |
Output is correct |
8 |
Correct |
8 ms |
19804 KB |
Output is correct |
9 |
Correct |
8 ms |
19284 KB |
Output is correct |
10 |
Correct |
7 ms |
19292 KB |
Output is correct |
11 |
Correct |
6 ms |
19292 KB |
Output is correct |
12 |
Correct |
8 ms |
20072 KB |
Output is correct |
13 |
Correct |
6 ms |
20060 KB |
Output is correct |
14 |
Correct |
7 ms |
19804 KB |
Output is correct |
15 |
Correct |
7 ms |
19804 KB |
Output is correct |
16 |
Correct |
8 ms |
19780 KB |
Output is correct |
17 |
Correct |
7 ms |
19292 KB |
Output is correct |
18 |
Correct |
10 ms |
19292 KB |
Output is correct |
19 |
Correct |
10 ms |
20060 KB |
Output is correct |
20 |
Correct |
7 ms |
19548 KB |
Output is correct |
21 |
Correct |
7 ms |
19508 KB |
Output is correct |
22 |
Correct |
8 ms |
19804 KB |
Output is correct |
23 |
Correct |
6 ms |
19320 KB |
Output is correct |
24 |
Correct |
7 ms |
20060 KB |
Output is correct |
25 |
Correct |
6 ms |
19804 KB |
Output is correct |
26 |
Correct |
6 ms |
20056 KB |
Output is correct |
27 |
Correct |
8 ms |
20060 KB |
Output is correct |
28 |
Correct |
12 ms |
20060 KB |
Output is correct |
29 |
Correct |
8 ms |
20316 KB |
Output is correct |
30 |
Correct |
8 ms |
20312 KB |
Output is correct |
31 |
Correct |
7 ms |
20372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
19036 KB |
Output is correct |
2 |
Correct |
3 ms |
19036 KB |
Output is correct |
3 |
Correct |
2 ms |
19072 KB |
Output is correct |
4 |
Correct |
3 ms |
19036 KB |
Output is correct |
5 |
Correct |
8 ms |
19808 KB |
Output is correct |
6 |
Correct |
9 ms |
19348 KB |
Output is correct |
7 |
Correct |
6 ms |
19100 KB |
Output is correct |
8 |
Correct |
8 ms |
19804 KB |
Output is correct |
9 |
Correct |
8 ms |
19284 KB |
Output is correct |
10 |
Correct |
7 ms |
19292 KB |
Output is correct |
11 |
Correct |
6 ms |
19292 KB |
Output is correct |
12 |
Correct |
8 ms |
20072 KB |
Output is correct |
13 |
Correct |
6 ms |
20060 KB |
Output is correct |
14 |
Correct |
7 ms |
19804 KB |
Output is correct |
15 |
Correct |
7 ms |
19804 KB |
Output is correct |
16 |
Correct |
8 ms |
19780 KB |
Output is correct |
17 |
Correct |
7 ms |
19292 KB |
Output is correct |
18 |
Correct |
10 ms |
19292 KB |
Output is correct |
19 |
Correct |
10 ms |
20060 KB |
Output is correct |
20 |
Correct |
7 ms |
19548 KB |
Output is correct |
21 |
Correct |
7 ms |
19508 KB |
Output is correct |
22 |
Correct |
8 ms |
19804 KB |
Output is correct |
23 |
Correct |
6 ms |
19320 KB |
Output is correct |
24 |
Correct |
7 ms |
20060 KB |
Output is correct |
25 |
Correct |
6 ms |
19804 KB |
Output is correct |
26 |
Correct |
6 ms |
20056 KB |
Output is correct |
27 |
Correct |
8 ms |
20060 KB |
Output is correct |
28 |
Correct |
12 ms |
20060 KB |
Output is correct |
29 |
Correct |
8 ms |
20316 KB |
Output is correct |
30 |
Correct |
8 ms |
20312 KB |
Output is correct |
31 |
Correct |
7 ms |
20372 KB |
Output is correct |
32 |
Correct |
8 ms |
19804 KB |
Output is correct |
33 |
Correct |
327 ms |
48824 KB |
Output is correct |
34 |
Correct |
239 ms |
29924 KB |
Output is correct |
35 |
Correct |
303 ms |
49088 KB |
Output is correct |
36 |
Correct |
225 ms |
29776 KB |
Output is correct |
37 |
Correct |
260 ms |
29464 KB |
Output is correct |
38 |
Correct |
172 ms |
29320 KB |
Output is correct |
39 |
Correct |
212 ms |
66644 KB |
Output is correct |
40 |
Correct |
198 ms |
66528 KB |
Output is correct |
41 |
Correct |
164 ms |
66416 KB |
Output is correct |
42 |
Correct |
211 ms |
49384 KB |
Output is correct |
43 |
Correct |
206 ms |
49488 KB |
Output is correct |
44 |
Correct |
303 ms |
47184 KB |
Output is correct |
45 |
Correct |
211 ms |
29348 KB |
Output is correct |
46 |
Correct |
145 ms |
29012 KB |
Output is correct |
47 |
Correct |
264 ms |
59984 KB |
Output is correct |
48 |
Correct |
164 ms |
46160 KB |
Output is correct |
49 |
Correct |
151 ms |
45908 KB |
Output is correct |
50 |
Correct |
237 ms |
51392 KB |
Output is correct |
51 |
Correct |
131 ms |
26556 KB |
Output is correct |
52 |
Correct |
262 ms |
61432 KB |
Output is correct |
53 |
Correct |
199 ms |
47148 KB |
Output is correct |
54 |
Correct |
162 ms |
66644 KB |
Output is correct |
55 |
Correct |
227 ms |
64548 KB |
Output is correct |
56 |
Correct |
219 ms |
75200 KB |
Output is correct |
57 |
Correct |
206 ms |
80724 KB |
Output is correct |
58 |
Correct |
245 ms |
73692 KB |
Output is correct |
59 |
Correct |
242 ms |
73484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
19036 KB |
Output is correct |
2 |
Correct |
3 ms |
19036 KB |
Output is correct |
3 |
Correct |
2 ms |
19072 KB |
Output is correct |
4 |
Correct |
3 ms |
19036 KB |
Output is correct |
5 |
Correct |
8 ms |
19808 KB |
Output is correct |
6 |
Correct |
9 ms |
19348 KB |
Output is correct |
7 |
Correct |
6 ms |
19100 KB |
Output is correct |
8 |
Correct |
8 ms |
19804 KB |
Output is correct |
9 |
Correct |
8 ms |
19284 KB |
Output is correct |
10 |
Correct |
7 ms |
19292 KB |
Output is correct |
11 |
Correct |
6 ms |
19292 KB |
Output is correct |
12 |
Correct |
8 ms |
20072 KB |
Output is correct |
13 |
Correct |
6 ms |
20060 KB |
Output is correct |
14 |
Correct |
7 ms |
19804 KB |
Output is correct |
15 |
Correct |
7 ms |
19804 KB |
Output is correct |
16 |
Correct |
8 ms |
19780 KB |
Output is correct |
17 |
Correct |
7 ms |
19292 KB |
Output is correct |
18 |
Correct |
10 ms |
19292 KB |
Output is correct |
19 |
Correct |
10 ms |
20060 KB |
Output is correct |
20 |
Correct |
7 ms |
19548 KB |
Output is correct |
21 |
Correct |
7 ms |
19508 KB |
Output is correct |
22 |
Correct |
8 ms |
19804 KB |
Output is correct |
23 |
Correct |
6 ms |
19320 KB |
Output is correct |
24 |
Correct |
7 ms |
20060 KB |
Output is correct |
25 |
Correct |
6 ms |
19804 KB |
Output is correct |
26 |
Correct |
6 ms |
20056 KB |
Output is correct |
27 |
Correct |
8 ms |
20060 KB |
Output is correct |
28 |
Correct |
12 ms |
20060 KB |
Output is correct |
29 |
Correct |
8 ms |
20316 KB |
Output is correct |
30 |
Correct |
8 ms |
20312 KB |
Output is correct |
31 |
Correct |
7 ms |
20372 KB |
Output is correct |
32 |
Correct |
8 ms |
19804 KB |
Output is correct |
33 |
Correct |
327 ms |
48824 KB |
Output is correct |
34 |
Correct |
239 ms |
29924 KB |
Output is correct |
35 |
Correct |
303 ms |
49088 KB |
Output is correct |
36 |
Correct |
225 ms |
29776 KB |
Output is correct |
37 |
Correct |
260 ms |
29464 KB |
Output is correct |
38 |
Correct |
172 ms |
29320 KB |
Output is correct |
39 |
Correct |
212 ms |
66644 KB |
Output is correct |
40 |
Correct |
198 ms |
66528 KB |
Output is correct |
41 |
Correct |
164 ms |
66416 KB |
Output is correct |
42 |
Correct |
211 ms |
49384 KB |
Output is correct |
43 |
Correct |
206 ms |
49488 KB |
Output is correct |
44 |
Correct |
303 ms |
47184 KB |
Output is correct |
45 |
Correct |
211 ms |
29348 KB |
Output is correct |
46 |
Correct |
145 ms |
29012 KB |
Output is correct |
47 |
Correct |
264 ms |
59984 KB |
Output is correct |
48 |
Correct |
164 ms |
46160 KB |
Output is correct |
49 |
Correct |
151 ms |
45908 KB |
Output is correct |
50 |
Correct |
237 ms |
51392 KB |
Output is correct |
51 |
Correct |
131 ms |
26556 KB |
Output is correct |
52 |
Correct |
262 ms |
61432 KB |
Output is correct |
53 |
Correct |
199 ms |
47148 KB |
Output is correct |
54 |
Correct |
162 ms |
66644 KB |
Output is correct |
55 |
Correct |
227 ms |
64548 KB |
Output is correct |
56 |
Correct |
219 ms |
75200 KB |
Output is correct |
57 |
Correct |
206 ms |
80724 KB |
Output is correct |
58 |
Correct |
245 ms |
73692 KB |
Output is correct |
59 |
Correct |
242 ms |
73484 KB |
Output is correct |
60 |
Correct |
2 ms |
19036 KB |
Output is correct |
61 |
Correct |
3 ms |
19032 KB |
Output is correct |
62 |
Correct |
3 ms |
19032 KB |
Output is correct |
63 |
Correct |
3 ms |
19032 KB |
Output is correct |
64 |
Correct |
299 ms |
46124 KB |
Output is correct |
65 |
Correct |
208 ms |
30864 KB |
Output is correct |
66 |
Correct |
283 ms |
43940 KB |
Output is correct |
67 |
Correct |
203 ms |
31576 KB |
Output is correct |
68 |
Correct |
174 ms |
30288 KB |
Output is correct |
69 |
Correct |
175 ms |
30084 KB |
Output is correct |
70 |
Correct |
248 ms |
41604 KB |
Output is correct |
71 |
Correct |
175 ms |
33228 KB |
Output is correct |
72 |
Correct |
245 ms |
46304 KB |
Output is correct |
73 |
Correct |
212 ms |
34248 KB |
Output is correct |
74 |
Correct |
292 ms |
48592 KB |
Output is correct |
75 |
Correct |
197 ms |
36044 KB |
Output is correct |
76 |
Correct |
167 ms |
36036 KB |
Output is correct |
77 |
Correct |
253 ms |
49224 KB |
Output is correct |
78 |
Correct |
163 ms |
31172 KB |
Output is correct |
79 |
Correct |
283 ms |
47564 KB |
Output is correct |
80 |
Correct |
224 ms |
31952 KB |
Output is correct |
81 |
Correct |
200 ms |
31952 KB |
Output is correct |
82 |
Correct |
210 ms |
46272 KB |
Output is correct |
83 |
Correct |
147 ms |
32084 KB |
Output is correct |
84 |
Correct |
245 ms |
58052 KB |
Output is correct |
85 |
Correct |
254 ms |
58056 KB |
Output is correct |
86 |
Correct |
260 ms |
54984 KB |
Output is correct |
87 |
Correct |
264 ms |
58056 KB |
Output is correct |
88 |
Correct |
262 ms |
58052 KB |
Output is correct |