#include <bits/stdc++.h>
#define mkt make_tuple
#define all(x) x.begin(),x.end()
#define sz(x) (int)(x.size())
#define ll long long
#define lp(i,a,b) for(int i = a ; i < b ; i++ )
#define pii pair<int,int>
#define mk make_pair
#define ff first
#define ss second
#define tiii tuple<int,int,int>
#define pb push_back
const int MAX = 2e5+10 ;
const ll inf = 1e18+10LL ;
using namespace std ;
int N ;
int adj[MAX] , val[MAX] , inDeg[MAX] ;
ll cost[MAX] ;
vector<int> rev[MAX] ;
map<int, ll> *mp[MAX] ;
void print(int x){
cout << x <<": " << endl ;
for(auto e : (*mp[x])) cout << e.ff <<" " << e.ss << endl ;
cout << endl ;
}
int main(){
ios_base::sync_with_stdio(false) ;
cin.tie(0) ;
cin >> N ;
vector<int> p ;
for(int i = 1 ; i <= N ; i++ ) {
cin >> adj[i] >> val[i] >> cost[i] ;
p.pb(val[i] ) ;
}
sort(all(p) ) ;
p.erase(unique(all(p) ) , p.end() ) ;
for(int i = 1 ; i <= N ; i++ ) val[i] = lower_bound(all(p), val[i] )-p.begin()+1 ;
for(int i = 1 ; i <= N ; i++ ) rev[ adj[i] ].pb(i) ;
lp(i,1,N+1) inDeg[i] = sz(rev[i]) ;
lp(i,1,N+1) mp[i] = NULL ;
queue<int> fila ;
for(int i= 1 ; i <= N ; i++ )
if( sz(rev[i] ) == 0 ) fila.push(i) , mp[i] = new map<int,ll>() ;
while(!fila.empty() ){
int x = fila.front() ;
fila.pop() ;
for(auto e : rev[x] ){
if( mp[e] == mp[x] ) continue ;
for(auto &ee : (*mp[e]) ) (*mp[x])[ee.first] += ee.second ;
(*mp[e]).clear() ;
}
(*mp[x])[1] += cost[x] ;
(*mp[x])[val[x]] -= cost[x] ;
(*mp[x])[val[x]+1] += cost[x] ;
auto it = (*mp[x]).find(val[x] ) ;
while(it != (*mp[x]).begin() && it->second < 0 ){
ll s = it->second ;
it = (*mp[x]).erase(it) ;
it-- ;
it->second += s ;
}
if( mp[adj[x]] == NULL ) mp[adj[x]]=mp[x] ;
if( sz( (*mp[x]) ) > sz( (*mp[adj[x]])) ) mp[adj[x] ] = mp[x] ;
if( (--inDeg[adj[x]] ) == 0 ) fila.push(adj[x]) ;
}
ll ans = 0 ;
for(int i= 1 ; i <= N ; i++ ){
if(inDeg[i] == 0 ) continue ;
vector<int> cycle = {i, adj[i]} ;
while(cycle.back() != cycle[0] ){
int aux = adj[ cycle.back() ] ;
cycle.pb(aux) ;
}
cycle.pop_back() ;
for(auto e : cycle ){
if( mp[e] == NULL ) mp[e] = new map<int,ll>() ;
(*mp[e] )[1] += cost[e] ;
(*mp[e])[val[e]] -= cost[e] ;
(*mp[e])[val[e]+1] += cost[e] ;
for(auto ee : rev[e]){
if(mp[e] == mp[ee] or inDeg[ee] != 0 ) continue ;
for(auto &y : (*mp[ee])) (*mp[e])[y.ff] += y.ss ;
}
}
for(auto e : cycle ) inDeg[e] = 0 ;
for(int i= 1 ; i< sz(cycle) ; i++ )
for(auto e : (*mp[cycle[i]] ) ) (*mp[cycle[0]])[e.ff] += e.ss ;
ll curMin = inf ;
ll cur = 0 ;
for(auto e : (*mp[cycle[0]]) ){
cur += e.ss ;
curMin = min(curMin, cur) ;
}
ans += curMin ;
}
cout << ans <<"\n" ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
2 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
8 ms |
5708 KB |
Output is correct |
6 |
Correct |
7 ms |
5788 KB |
Output is correct |
7 |
Correct |
6 ms |
5836 KB |
Output is correct |
8 |
Correct |
9 ms |
5780 KB |
Output is correct |
9 |
Correct |
7 ms |
5804 KB |
Output is correct |
10 |
Correct |
8 ms |
5800 KB |
Output is correct |
11 |
Correct |
6 ms |
5860 KB |
Output is correct |
12 |
Correct |
7 ms |
5412 KB |
Output is correct |
13 |
Correct |
5 ms |
5452 KB |
Output is correct |
14 |
Correct |
7 ms |
5452 KB |
Output is correct |
15 |
Correct |
6 ms |
5424 KB |
Output is correct |
16 |
Correct |
12 ms |
5828 KB |
Output is correct |
17 |
Correct |
7 ms |
5784 KB |
Output is correct |
18 |
Correct |
6 ms |
5792 KB |
Output is correct |
19 |
Correct |
7 ms |
5800 KB |
Output is correct |
20 |
Correct |
6 ms |
5788 KB |
Output is correct |
21 |
Correct |
8 ms |
5804 KB |
Output is correct |
22 |
Correct |
8 ms |
6568 KB |
Output is correct |
23 |
Correct |
8 ms |
6220 KB |
Output is correct |
24 |
Correct |
10 ms |
5836 KB |
Output is correct |
25 |
Correct |
6 ms |
5836 KB |
Output is correct |
26 |
Correct |
7 ms |
5796 KB |
Output is correct |
27 |
Correct |
7 ms |
5836 KB |
Output is correct |
28 |
Correct |
7 ms |
5708 KB |
Output is correct |
29 |
Correct |
7 ms |
5676 KB |
Output is correct |
30 |
Correct |
7 ms |
5900 KB |
Output is correct |
31 |
Correct |
6 ms |
5836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
2 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
8 ms |
5708 KB |
Output is correct |
6 |
Correct |
7 ms |
5788 KB |
Output is correct |
7 |
Correct |
6 ms |
5836 KB |
Output is correct |
8 |
Correct |
9 ms |
5780 KB |
Output is correct |
9 |
Correct |
7 ms |
5804 KB |
Output is correct |
10 |
Correct |
8 ms |
5800 KB |
Output is correct |
11 |
Correct |
6 ms |
5860 KB |
Output is correct |
12 |
Correct |
7 ms |
5412 KB |
Output is correct |
13 |
Correct |
5 ms |
5452 KB |
Output is correct |
14 |
Correct |
7 ms |
5452 KB |
Output is correct |
15 |
Correct |
6 ms |
5424 KB |
Output is correct |
16 |
Correct |
12 ms |
5828 KB |
Output is correct |
17 |
Correct |
7 ms |
5784 KB |
Output is correct |
18 |
Correct |
6 ms |
5792 KB |
Output is correct |
19 |
Correct |
7 ms |
5800 KB |
Output is correct |
20 |
Correct |
6 ms |
5788 KB |
Output is correct |
21 |
Correct |
8 ms |
5804 KB |
Output is correct |
22 |
Correct |
8 ms |
6568 KB |
Output is correct |
23 |
Correct |
8 ms |
6220 KB |
Output is correct |
24 |
Correct |
10 ms |
5836 KB |
Output is correct |
25 |
Correct |
6 ms |
5836 KB |
Output is correct |
26 |
Correct |
7 ms |
5796 KB |
Output is correct |
27 |
Correct |
7 ms |
5836 KB |
Output is correct |
28 |
Correct |
7 ms |
5708 KB |
Output is correct |
29 |
Correct |
7 ms |
5676 KB |
Output is correct |
30 |
Correct |
7 ms |
5900 KB |
Output is correct |
31 |
Correct |
6 ms |
5836 KB |
Output is correct |
32 |
Correct |
9 ms |
5836 KB |
Output is correct |
33 |
Correct |
473 ms |
40904 KB |
Output is correct |
34 |
Correct |
311 ms |
39504 KB |
Output is correct |
35 |
Correct |
435 ms |
39600 KB |
Output is correct |
36 |
Correct |
318 ms |
39524 KB |
Output is correct |
37 |
Correct |
190 ms |
38844 KB |
Output is correct |
38 |
Correct |
158 ms |
38416 KB |
Output is correct |
39 |
Correct |
191 ms |
22700 KB |
Output is correct |
40 |
Correct |
156 ms |
22688 KB |
Output is correct |
41 |
Correct |
97 ms |
22592 KB |
Output is correct |
42 |
Correct |
199 ms |
22756 KB |
Output is correct |
43 |
Correct |
164 ms |
22752 KB |
Output is correct |
44 |
Correct |
371 ms |
38400 KB |
Output is correct |
45 |
Correct |
230 ms |
38452 KB |
Output is correct |
46 |
Correct |
103 ms |
38376 KB |
Output is correct |
47 |
Correct |
257 ms |
38240 KB |
Output is correct |
48 |
Correct |
150 ms |
38140 KB |
Output is correct |
49 |
Correct |
131 ms |
38184 KB |
Output is correct |
50 |
Correct |
278 ms |
66448 KB |
Output is correct |
51 |
Correct |
166 ms |
53948 KB |
Output is correct |
52 |
Correct |
290 ms |
38676 KB |
Output is correct |
53 |
Correct |
150 ms |
38672 KB |
Output is correct |
54 |
Correct |
231 ms |
35496 KB |
Output is correct |
55 |
Correct |
210 ms |
37700 KB |
Output is correct |
56 |
Correct |
186 ms |
33084 KB |
Output is correct |
57 |
Correct |
177 ms |
33248 KB |
Output is correct |
58 |
Correct |
266 ms |
40848 KB |
Output is correct |
59 |
Correct |
222 ms |
40728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
2 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
8 ms |
5708 KB |
Output is correct |
6 |
Correct |
7 ms |
5788 KB |
Output is correct |
7 |
Correct |
6 ms |
5836 KB |
Output is correct |
8 |
Correct |
9 ms |
5780 KB |
Output is correct |
9 |
Correct |
7 ms |
5804 KB |
Output is correct |
10 |
Correct |
8 ms |
5800 KB |
Output is correct |
11 |
Correct |
6 ms |
5860 KB |
Output is correct |
12 |
Correct |
7 ms |
5412 KB |
Output is correct |
13 |
Correct |
5 ms |
5452 KB |
Output is correct |
14 |
Correct |
7 ms |
5452 KB |
Output is correct |
15 |
Correct |
6 ms |
5424 KB |
Output is correct |
16 |
Correct |
12 ms |
5828 KB |
Output is correct |
17 |
Correct |
7 ms |
5784 KB |
Output is correct |
18 |
Correct |
6 ms |
5792 KB |
Output is correct |
19 |
Correct |
7 ms |
5800 KB |
Output is correct |
20 |
Correct |
6 ms |
5788 KB |
Output is correct |
21 |
Correct |
8 ms |
5804 KB |
Output is correct |
22 |
Correct |
8 ms |
6568 KB |
Output is correct |
23 |
Correct |
8 ms |
6220 KB |
Output is correct |
24 |
Correct |
10 ms |
5836 KB |
Output is correct |
25 |
Correct |
6 ms |
5836 KB |
Output is correct |
26 |
Correct |
7 ms |
5796 KB |
Output is correct |
27 |
Correct |
7 ms |
5836 KB |
Output is correct |
28 |
Correct |
7 ms |
5708 KB |
Output is correct |
29 |
Correct |
7 ms |
5676 KB |
Output is correct |
30 |
Correct |
7 ms |
5900 KB |
Output is correct |
31 |
Correct |
6 ms |
5836 KB |
Output is correct |
32 |
Correct |
9 ms |
5836 KB |
Output is correct |
33 |
Correct |
473 ms |
40904 KB |
Output is correct |
34 |
Correct |
311 ms |
39504 KB |
Output is correct |
35 |
Correct |
435 ms |
39600 KB |
Output is correct |
36 |
Correct |
318 ms |
39524 KB |
Output is correct |
37 |
Correct |
190 ms |
38844 KB |
Output is correct |
38 |
Correct |
158 ms |
38416 KB |
Output is correct |
39 |
Correct |
191 ms |
22700 KB |
Output is correct |
40 |
Correct |
156 ms |
22688 KB |
Output is correct |
41 |
Correct |
97 ms |
22592 KB |
Output is correct |
42 |
Correct |
199 ms |
22756 KB |
Output is correct |
43 |
Correct |
164 ms |
22752 KB |
Output is correct |
44 |
Correct |
371 ms |
38400 KB |
Output is correct |
45 |
Correct |
230 ms |
38452 KB |
Output is correct |
46 |
Correct |
103 ms |
38376 KB |
Output is correct |
47 |
Correct |
257 ms |
38240 KB |
Output is correct |
48 |
Correct |
150 ms |
38140 KB |
Output is correct |
49 |
Correct |
131 ms |
38184 KB |
Output is correct |
50 |
Correct |
278 ms |
66448 KB |
Output is correct |
51 |
Correct |
166 ms |
53948 KB |
Output is correct |
52 |
Correct |
290 ms |
38676 KB |
Output is correct |
53 |
Correct |
150 ms |
38672 KB |
Output is correct |
54 |
Correct |
231 ms |
35496 KB |
Output is correct |
55 |
Correct |
210 ms |
37700 KB |
Output is correct |
56 |
Correct |
186 ms |
33084 KB |
Output is correct |
57 |
Correct |
177 ms |
33248 KB |
Output is correct |
58 |
Correct |
266 ms |
40848 KB |
Output is correct |
59 |
Correct |
222 ms |
40728 KB |
Output is correct |
60 |
Correct |
3 ms |
5016 KB |
Output is correct |
61 |
Correct |
3 ms |
4944 KB |
Output is correct |
62 |
Correct |
3 ms |
4940 KB |
Output is correct |
63 |
Correct |
3 ms |
4940 KB |
Output is correct |
64 |
Correct |
444 ms |
41136 KB |
Output is correct |
65 |
Correct |
314 ms |
35392 KB |
Output is correct |
66 |
Correct |
412 ms |
41412 KB |
Output is correct |
67 |
Correct |
299 ms |
35468 KB |
Output is correct |
68 |
Correct |
195 ms |
34924 KB |
Output is correct |
69 |
Correct |
182 ms |
34340 KB |
Output is correct |
70 |
Correct |
386 ms |
91180 KB |
Output is correct |
71 |
Correct |
248 ms |
72916 KB |
Output is correct |
72 |
Correct |
385 ms |
86044 KB |
Output is correct |
73 |
Correct |
259 ms |
73536 KB |
Output is correct |
74 |
Correct |
409 ms |
63860 KB |
Output is correct |
75 |
Correct |
228 ms |
51276 KB |
Output is correct |
76 |
Correct |
183 ms |
47628 KB |
Output is correct |
77 |
Correct |
337 ms |
82992 KB |
Output is correct |
78 |
Correct |
205 ms |
64140 KB |
Output is correct |
79 |
Correct |
423 ms |
68668 KB |
Output is correct |
80 |
Correct |
268 ms |
49948 KB |
Output is correct |
81 |
Correct |
216 ms |
48540 KB |
Output is correct |
82 |
Correct |
326 ms |
86264 KB |
Output is correct |
83 |
Correct |
166 ms |
72700 KB |
Output is correct |
84 |
Correct |
350 ms |
67880 KB |
Output is correct |
85 |
Correct |
357 ms |
67932 KB |
Output is correct |
86 |
Correct |
363 ms |
64764 KB |
Output is correct |
87 |
Correct |
347 ms |
67872 KB |
Output is correct |
88 |
Correct |
363 ms |
67844 KB |
Output is correct |