#include<bits/stdc++.h>
#include<fstream>
using namespace std;
#define sz(a) (int)a.size()
#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mpp make_pair
const ld PI = 3.14159265359, prec = 1e-9;;
//using u128 = __uint128_t;
//const int x[4] = {1, 0, -1, 0};
//const int y[4] = {0, -1, 0, 1};
const ll mod = 1e9 + 7, pr = 31;
const int mxn = 2e5 + 5, mxq = 1e5 + 5, sq = 500, mxv = 5e4 + 1;
//const int base = (1 <<18);
const ll inf = 1e9 + 5, neg = -69420, inf2 = 1e14;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// have fun!
int n;
vt<int>adj[mxn + 1];
ll to[mxn + 1], h[mxn + 1], c[mxn + 1];
bool cyc[mxn + 1];
bool vis[mxn + 1];
map<ll, ll>mp[mxn + 1], cost;
void dfs(int s, int pre){
vis[s] = 1;
for(auto i: adj[s]){
if(i != pre && (!s || !cyc[i])){
dfs(i, s);
if(sz(mp[i]) > sz(mp[s])){
swap(mp[s], mp[i]);
}
for(auto j: mp[i])mp[s][j.fi] += j.se;
}
}
if(pre){
mp[s][h[s]] += c[s];
ll lft = c[s];
while(1){
auto it = mp[s].lower_bound(h[s]);
if(it == mp[s].begin())break;
--it;
if((*it).se <= lft){
lft -= (*it).se; mp[s].erase(it);
}else{
(*it).se -= lft; break;
}
}
}
}
void solve(){
cin >> n;
ll totc = 0;
for(int i = 1; i <= n; i++){
cin >> to[i] >> h[i] >> c[i];
totc += c[i];
adj[to[i]].pb(i);
}
ll res = 0;
for(int i = 1; i <= n; i++){
if(!vis[i]){
int a = to[i], b = to[to[i]];
while(a != b){
a = to[a]; b = to[to[b]];
}
a = i;
while(a != b){
a = to[a]; b = to[b];
}
cyc[a] = 1; b = to[a];
adj[0].clear(); mp[0].clear(); cost.clear(); adj[0].pb(a); cost[h[a]] += c[a];
while(a != b){
cyc[b] = 1; adj[0].pb(b); cost[h[b]] += c[b]; b = to[b];
}
dfs(0, -1);
ll tot = 0, ans = 0;
for(auto j: mp[0]){
tot += j.se; mp[0][j.fi] = tot;
}
ans = tot;
for(auto j: cost){
auto it = mp[0].lower_bound(j.fi);
if(it != mp[0].begin()){
--it;
ans = max(ans, tot + (j.se - (*it).se));
}
}
res += ans;
}
}
cout << totc - res;
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("DATA.inp", "r", stdin);
//freopen("DATA.out", "w", stdout);
int tt; tt = 1;
while(tt--){
solve();
}
return(0);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
19544 KB |
Output is correct |
2 |
Correct |
4 ms |
19548 KB |
Output is correct |
3 |
Correct |
5 ms |
19548 KB |
Output is correct |
4 |
Correct |
5 ms |
19548 KB |
Output is correct |
5 |
Correct |
8 ms |
20572 KB |
Output is correct |
6 |
Correct |
7 ms |
20196 KB |
Output is correct |
7 |
Correct |
6 ms |
19804 KB |
Output is correct |
8 |
Correct |
8 ms |
20572 KB |
Output is correct |
9 |
Correct |
8 ms |
20000 KB |
Output is correct |
10 |
Correct |
6 ms |
19804 KB |
Output is correct |
11 |
Correct |
6 ms |
19804 KB |
Output is correct |
12 |
Correct |
7 ms |
20572 KB |
Output is correct |
13 |
Correct |
8 ms |
20572 KB |
Output is correct |
14 |
Correct |
6 ms |
20060 KB |
Output is correct |
15 |
Correct |
6 ms |
20236 KB |
Output is correct |
16 |
Correct |
9 ms |
20828 KB |
Output is correct |
17 |
Correct |
8 ms |
20316 KB |
Output is correct |
18 |
Correct |
5 ms |
19800 KB |
Output is correct |
19 |
Correct |
8 ms |
20456 KB |
Output is correct |
20 |
Correct |
7 ms |
20060 KB |
Output is correct |
21 |
Correct |
6 ms |
20060 KB |
Output is correct |
22 |
Correct |
7 ms |
20256 KB |
Output is correct |
23 |
Correct |
5 ms |
19804 KB |
Output is correct |
24 |
Correct |
7 ms |
20428 KB |
Output is correct |
25 |
Correct |
6 ms |
20316 KB |
Output is correct |
26 |
Correct |
6 ms |
20420 KB |
Output is correct |
27 |
Correct |
8 ms |
20316 KB |
Output is correct |
28 |
Correct |
7 ms |
20568 KB |
Output is correct |
29 |
Correct |
7 ms |
20572 KB |
Output is correct |
30 |
Correct |
8 ms |
20624 KB |
Output is correct |
31 |
Correct |
7 ms |
20572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
19544 KB |
Output is correct |
2 |
Correct |
4 ms |
19548 KB |
Output is correct |
3 |
Correct |
5 ms |
19548 KB |
Output is correct |
4 |
Correct |
5 ms |
19548 KB |
Output is correct |
5 |
Correct |
8 ms |
20572 KB |
Output is correct |
6 |
Correct |
7 ms |
20196 KB |
Output is correct |
7 |
Correct |
6 ms |
19804 KB |
Output is correct |
8 |
Correct |
8 ms |
20572 KB |
Output is correct |
9 |
Correct |
8 ms |
20000 KB |
Output is correct |
10 |
Correct |
6 ms |
19804 KB |
Output is correct |
11 |
Correct |
6 ms |
19804 KB |
Output is correct |
12 |
Correct |
7 ms |
20572 KB |
Output is correct |
13 |
Correct |
8 ms |
20572 KB |
Output is correct |
14 |
Correct |
6 ms |
20060 KB |
Output is correct |
15 |
Correct |
6 ms |
20236 KB |
Output is correct |
16 |
Correct |
9 ms |
20828 KB |
Output is correct |
17 |
Correct |
8 ms |
20316 KB |
Output is correct |
18 |
Correct |
5 ms |
19800 KB |
Output is correct |
19 |
Correct |
8 ms |
20456 KB |
Output is correct |
20 |
Correct |
7 ms |
20060 KB |
Output is correct |
21 |
Correct |
6 ms |
20060 KB |
Output is correct |
22 |
Correct |
7 ms |
20256 KB |
Output is correct |
23 |
Correct |
5 ms |
19804 KB |
Output is correct |
24 |
Correct |
7 ms |
20428 KB |
Output is correct |
25 |
Correct |
6 ms |
20316 KB |
Output is correct |
26 |
Correct |
6 ms |
20420 KB |
Output is correct |
27 |
Correct |
8 ms |
20316 KB |
Output is correct |
28 |
Correct |
7 ms |
20568 KB |
Output is correct |
29 |
Correct |
7 ms |
20572 KB |
Output is correct |
30 |
Correct |
8 ms |
20624 KB |
Output is correct |
31 |
Correct |
7 ms |
20572 KB |
Output is correct |
32 |
Correct |
8 ms |
20572 KB |
Output is correct |
33 |
Correct |
301 ms |
77244 KB |
Output is correct |
34 |
Correct |
213 ms |
52356 KB |
Output is correct |
35 |
Correct |
296 ms |
74268 KB |
Output is correct |
36 |
Correct |
193 ms |
52316 KB |
Output is correct |
37 |
Correct |
129 ms |
33460 KB |
Output is correct |
38 |
Correct |
122 ms |
28876 KB |
Output is correct |
39 |
Correct |
128 ms |
60360 KB |
Output is correct |
40 |
Correct |
115 ms |
60472 KB |
Output is correct |
41 |
Correct |
84 ms |
60324 KB |
Output is correct |
42 |
Correct |
115 ms |
43088 KB |
Output is correct |
43 |
Correct |
106 ms |
43092 KB |
Output is correct |
44 |
Correct |
272 ms |
102272 KB |
Output is correct |
45 |
Correct |
178 ms |
66384 KB |
Output is correct |
46 |
Correct |
71 ms |
28960 KB |
Output is correct |
47 |
Correct |
173 ms |
53212 KB |
Output is correct |
48 |
Correct |
93 ms |
46196 KB |
Output is correct |
49 |
Correct |
83 ms |
46160 KB |
Output is correct |
50 |
Correct |
240 ms |
45264 KB |
Output is correct |
51 |
Correct |
76 ms |
32964 KB |
Output is correct |
52 |
Correct |
199 ms |
53580 KB |
Output is correct |
53 |
Correct |
88 ms |
46540 KB |
Output is correct |
54 |
Correct |
89 ms |
60248 KB |
Output is correct |
55 |
Correct |
156 ms |
55700 KB |
Output is correct |
56 |
Correct |
140 ms |
63060 KB |
Output is correct |
57 |
Correct |
126 ms |
66644 KB |
Output is correct |
58 |
Correct |
144 ms |
61804 KB |
Output is correct |
59 |
Correct |
150 ms |
61896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
19544 KB |
Output is correct |
2 |
Correct |
4 ms |
19548 KB |
Output is correct |
3 |
Correct |
5 ms |
19548 KB |
Output is correct |
4 |
Correct |
5 ms |
19548 KB |
Output is correct |
5 |
Correct |
8 ms |
20572 KB |
Output is correct |
6 |
Correct |
7 ms |
20196 KB |
Output is correct |
7 |
Correct |
6 ms |
19804 KB |
Output is correct |
8 |
Correct |
8 ms |
20572 KB |
Output is correct |
9 |
Correct |
8 ms |
20000 KB |
Output is correct |
10 |
Correct |
6 ms |
19804 KB |
Output is correct |
11 |
Correct |
6 ms |
19804 KB |
Output is correct |
12 |
Correct |
7 ms |
20572 KB |
Output is correct |
13 |
Correct |
8 ms |
20572 KB |
Output is correct |
14 |
Correct |
6 ms |
20060 KB |
Output is correct |
15 |
Correct |
6 ms |
20236 KB |
Output is correct |
16 |
Correct |
9 ms |
20828 KB |
Output is correct |
17 |
Correct |
8 ms |
20316 KB |
Output is correct |
18 |
Correct |
5 ms |
19800 KB |
Output is correct |
19 |
Correct |
8 ms |
20456 KB |
Output is correct |
20 |
Correct |
7 ms |
20060 KB |
Output is correct |
21 |
Correct |
6 ms |
20060 KB |
Output is correct |
22 |
Correct |
7 ms |
20256 KB |
Output is correct |
23 |
Correct |
5 ms |
19804 KB |
Output is correct |
24 |
Correct |
7 ms |
20428 KB |
Output is correct |
25 |
Correct |
6 ms |
20316 KB |
Output is correct |
26 |
Correct |
6 ms |
20420 KB |
Output is correct |
27 |
Correct |
8 ms |
20316 KB |
Output is correct |
28 |
Correct |
7 ms |
20568 KB |
Output is correct |
29 |
Correct |
7 ms |
20572 KB |
Output is correct |
30 |
Correct |
8 ms |
20624 KB |
Output is correct |
31 |
Correct |
7 ms |
20572 KB |
Output is correct |
32 |
Correct |
8 ms |
20572 KB |
Output is correct |
33 |
Correct |
301 ms |
77244 KB |
Output is correct |
34 |
Correct |
213 ms |
52356 KB |
Output is correct |
35 |
Correct |
296 ms |
74268 KB |
Output is correct |
36 |
Correct |
193 ms |
52316 KB |
Output is correct |
37 |
Correct |
129 ms |
33460 KB |
Output is correct |
38 |
Correct |
122 ms |
28876 KB |
Output is correct |
39 |
Correct |
128 ms |
60360 KB |
Output is correct |
40 |
Correct |
115 ms |
60472 KB |
Output is correct |
41 |
Correct |
84 ms |
60324 KB |
Output is correct |
42 |
Correct |
115 ms |
43088 KB |
Output is correct |
43 |
Correct |
106 ms |
43092 KB |
Output is correct |
44 |
Correct |
272 ms |
102272 KB |
Output is correct |
45 |
Correct |
178 ms |
66384 KB |
Output is correct |
46 |
Correct |
71 ms |
28960 KB |
Output is correct |
47 |
Correct |
173 ms |
53212 KB |
Output is correct |
48 |
Correct |
93 ms |
46196 KB |
Output is correct |
49 |
Correct |
83 ms |
46160 KB |
Output is correct |
50 |
Correct |
240 ms |
45264 KB |
Output is correct |
51 |
Correct |
76 ms |
32964 KB |
Output is correct |
52 |
Correct |
199 ms |
53580 KB |
Output is correct |
53 |
Correct |
88 ms |
46540 KB |
Output is correct |
54 |
Correct |
89 ms |
60248 KB |
Output is correct |
55 |
Correct |
156 ms |
55700 KB |
Output is correct |
56 |
Correct |
140 ms |
63060 KB |
Output is correct |
57 |
Correct |
126 ms |
66644 KB |
Output is correct |
58 |
Correct |
144 ms |
61804 KB |
Output is correct |
59 |
Correct |
150 ms |
61896 KB |
Output is correct |
60 |
Correct |
4 ms |
19548 KB |
Output is correct |
61 |
Correct |
4 ms |
19548 KB |
Output is correct |
62 |
Correct |
4 ms |
19548 KB |
Output is correct |
63 |
Correct |
4 ms |
19636 KB |
Output is correct |
64 |
Correct |
236 ms |
66856 KB |
Output is correct |
65 |
Correct |
209 ms |
50272 KB |
Output is correct |
66 |
Correct |
247 ms |
62744 KB |
Output is correct |
67 |
Correct |
199 ms |
50400 KB |
Output is correct |
68 |
Correct |
148 ms |
36676 KB |
Output is correct |
69 |
Correct |
117 ms |
33320 KB |
Output is correct |
70 |
Correct |
174 ms |
40168 KB |
Output is correct |
71 |
Correct |
123 ms |
31696 KB |
Output is correct |
72 |
Correct |
197 ms |
44228 KB |
Output is correct |
73 |
Correct |
98 ms |
32204 KB |
Output is correct |
74 |
Correct |
251 ms |
47004 KB |
Output is correct |
75 |
Correct |
121 ms |
34472 KB |
Output is correct |
76 |
Correct |
149 ms |
34496 KB |
Output is correct |
77 |
Correct |
186 ms |
47512 KB |
Output is correct |
78 |
Correct |
112 ms |
35028 KB |
Output is correct |
79 |
Correct |
240 ms |
62984 KB |
Output is correct |
80 |
Correct |
165 ms |
45960 KB |
Output is correct |
81 |
Correct |
126 ms |
35292 KB |
Output is correct |
82 |
Correct |
132 ms |
44404 KB |
Output is correct |
83 |
Correct |
67 ms |
31232 KB |
Output is correct |
84 |
Correct |
250 ms |
53560 KB |
Output is correct |
85 |
Correct |
208 ms |
53412 KB |
Output is correct |
86 |
Correct |
178 ms |
50372 KB |
Output is correct |
87 |
Correct |
203 ms |
53428 KB |
Output is correct |
88 |
Correct |
206 ms |
53448 KB |
Output is correct |