// vaziat meshki-ghermeze !
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
map<int, ll> mp[N];
int a[N], h[N], c[N];
vector<int> G[N];
void DFS(int u){
for(auto adj : G[u]){
if(adj == u) continue;
DFS(adj);
if(mp[adj].size() > mp[u].size())
mp[u].swap(mp[adj]);
for(auto [la, sm] : mp[adj])
mp[u][la] += sm;
}
ll del = c[u];
while(del){
auto it = mp[u].lower_bound(h[u] + 1);
if(it == mp[u].end())
break;
ll rm = min(del, it -> S);
del -= rm;
if(rm < it -> S){
mp[u][it -> F] -= rm;
} else mp[u].erase(it);
}
mp[u][h[u]] += c[u];
}
int mk[N];
ll ans = 0;
int loop[N], con[N];
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n;
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i] >> h[i] >> c[i];
ans += c[i];
h[i] = -h[i];
}
for(int i = 1; i <= n; i++){
if(mk[i]) continue;
int nw = i;
while(!mk[nw]){
mk[nw] = i;
nw = a[nw];
}
if(mk[nw] < i) continue;
int st = nw;
vector<int> P = {st};
nw = a[nw];
while(st != nw){
P.pb(nw);
nw = a[nw];
}
sort(all(P), [&](int i, int j){
return h[i] < h[j];
});
for(auto x : P) loop[x] = 1;
a[P[0]] = P[0];
for(int i = 1; i < (int) P.size(); i++) a[P[i]] = P[i - 1];
for(auto x : P) con[x] = P.back();
}
for(int i = 1; i <= n; i++){
if(!loop[i] && loop[a[i]])
a[i] = con[a[i]];
G[a[i]].pb(i);
}
for(int i = 1; i <= n; i++)
if(a[i] == i){
DFS(i);
for(auto [x, y] : mp[i])
ans -= y;
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14412 KB |
Output is correct |
2 |
Correct |
10 ms |
14320 KB |
Output is correct |
3 |
Correct |
10 ms |
14412 KB |
Output is correct |
4 |
Correct |
10 ms |
14412 KB |
Output is correct |
5 |
Correct |
16 ms |
15564 KB |
Output is correct |
6 |
Correct |
14 ms |
15024 KB |
Output is correct |
7 |
Correct |
13 ms |
14896 KB |
Output is correct |
8 |
Correct |
17 ms |
15584 KB |
Output is correct |
9 |
Correct |
15 ms |
15052 KB |
Output is correct |
10 |
Correct |
13 ms |
14924 KB |
Output is correct |
11 |
Correct |
13 ms |
14668 KB |
Output is correct |
12 |
Correct |
14 ms |
15384 KB |
Output is correct |
13 |
Correct |
14 ms |
15308 KB |
Output is correct |
14 |
Correct |
14 ms |
14924 KB |
Output is correct |
15 |
Correct |
14 ms |
14924 KB |
Output is correct |
16 |
Correct |
17 ms |
15948 KB |
Output is correct |
17 |
Correct |
15 ms |
15248 KB |
Output is correct |
18 |
Correct |
13 ms |
14668 KB |
Output is correct |
19 |
Correct |
16 ms |
15172 KB |
Output is correct |
20 |
Correct |
18 ms |
15092 KB |
Output is correct |
21 |
Correct |
13 ms |
15052 KB |
Output is correct |
22 |
Correct |
16 ms |
15108 KB |
Output is correct |
23 |
Correct |
15 ms |
14796 KB |
Output is correct |
24 |
Correct |
15 ms |
15244 KB |
Output is correct |
25 |
Correct |
14 ms |
15000 KB |
Output is correct |
26 |
Correct |
14 ms |
15256 KB |
Output is correct |
27 |
Correct |
15 ms |
15180 KB |
Output is correct |
28 |
Correct |
15 ms |
15284 KB |
Output is correct |
29 |
Correct |
14 ms |
15436 KB |
Output is correct |
30 |
Correct |
16 ms |
15416 KB |
Output is correct |
31 |
Correct |
16 ms |
15376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14412 KB |
Output is correct |
2 |
Correct |
10 ms |
14320 KB |
Output is correct |
3 |
Correct |
10 ms |
14412 KB |
Output is correct |
4 |
Correct |
10 ms |
14412 KB |
Output is correct |
5 |
Correct |
16 ms |
15564 KB |
Output is correct |
6 |
Correct |
14 ms |
15024 KB |
Output is correct |
7 |
Correct |
13 ms |
14896 KB |
Output is correct |
8 |
Correct |
17 ms |
15584 KB |
Output is correct |
9 |
Correct |
15 ms |
15052 KB |
Output is correct |
10 |
Correct |
13 ms |
14924 KB |
Output is correct |
11 |
Correct |
13 ms |
14668 KB |
Output is correct |
12 |
Correct |
14 ms |
15384 KB |
Output is correct |
13 |
Correct |
14 ms |
15308 KB |
Output is correct |
14 |
Correct |
14 ms |
14924 KB |
Output is correct |
15 |
Correct |
14 ms |
14924 KB |
Output is correct |
16 |
Correct |
17 ms |
15948 KB |
Output is correct |
17 |
Correct |
15 ms |
15248 KB |
Output is correct |
18 |
Correct |
13 ms |
14668 KB |
Output is correct |
19 |
Correct |
16 ms |
15172 KB |
Output is correct |
20 |
Correct |
18 ms |
15092 KB |
Output is correct |
21 |
Correct |
13 ms |
15052 KB |
Output is correct |
22 |
Correct |
16 ms |
15108 KB |
Output is correct |
23 |
Correct |
15 ms |
14796 KB |
Output is correct |
24 |
Correct |
15 ms |
15244 KB |
Output is correct |
25 |
Correct |
14 ms |
15000 KB |
Output is correct |
26 |
Correct |
14 ms |
15256 KB |
Output is correct |
27 |
Correct |
15 ms |
15180 KB |
Output is correct |
28 |
Correct |
15 ms |
15284 KB |
Output is correct |
29 |
Correct |
14 ms |
15436 KB |
Output is correct |
30 |
Correct |
16 ms |
15416 KB |
Output is correct |
31 |
Correct |
16 ms |
15376 KB |
Output is correct |
32 |
Correct |
16 ms |
15500 KB |
Output is correct |
33 |
Correct |
402 ms |
75100 KB |
Output is correct |
34 |
Correct |
295 ms |
50352 KB |
Output is correct |
35 |
Correct |
376 ms |
72080 KB |
Output is correct |
36 |
Correct |
297 ms |
50372 KB |
Output is correct |
37 |
Correct |
227 ms |
31336 KB |
Output is correct |
38 |
Correct |
177 ms |
27000 KB |
Output is correct |
39 |
Correct |
188 ms |
51924 KB |
Output is correct |
40 |
Correct |
173 ms |
51908 KB |
Output is correct |
41 |
Correct |
141 ms |
51916 KB |
Output is correct |
42 |
Correct |
176 ms |
37932 KB |
Output is correct |
43 |
Correct |
169 ms |
37988 KB |
Output is correct |
44 |
Correct |
375 ms |
100164 KB |
Output is correct |
45 |
Correct |
262 ms |
64472 KB |
Output is correct |
46 |
Correct |
129 ms |
26824 KB |
Output is correct |
47 |
Correct |
362 ms |
47876 KB |
Output is correct |
48 |
Correct |
147 ms |
41028 KB |
Output is correct |
49 |
Correct |
121 ms |
41024 KB |
Output is correct |
50 |
Correct |
322 ms |
43376 KB |
Output is correct |
51 |
Correct |
131 ms |
30780 KB |
Output is correct |
52 |
Correct |
356 ms |
48660 KB |
Output is correct |
53 |
Correct |
144 ms |
41404 KB |
Output is correct |
54 |
Correct |
134 ms |
51960 KB |
Output is correct |
55 |
Correct |
242 ms |
50436 KB |
Output is correct |
56 |
Correct |
214 ms |
56240 KB |
Output is correct |
57 |
Correct |
198 ms |
59324 KB |
Output is correct |
58 |
Correct |
237 ms |
56556 KB |
Output is correct |
59 |
Correct |
225 ms |
56624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14412 KB |
Output is correct |
2 |
Correct |
10 ms |
14320 KB |
Output is correct |
3 |
Correct |
10 ms |
14412 KB |
Output is correct |
4 |
Correct |
10 ms |
14412 KB |
Output is correct |
5 |
Correct |
16 ms |
15564 KB |
Output is correct |
6 |
Correct |
14 ms |
15024 KB |
Output is correct |
7 |
Correct |
13 ms |
14896 KB |
Output is correct |
8 |
Correct |
17 ms |
15584 KB |
Output is correct |
9 |
Correct |
15 ms |
15052 KB |
Output is correct |
10 |
Correct |
13 ms |
14924 KB |
Output is correct |
11 |
Correct |
13 ms |
14668 KB |
Output is correct |
12 |
Correct |
14 ms |
15384 KB |
Output is correct |
13 |
Correct |
14 ms |
15308 KB |
Output is correct |
14 |
Correct |
14 ms |
14924 KB |
Output is correct |
15 |
Correct |
14 ms |
14924 KB |
Output is correct |
16 |
Correct |
17 ms |
15948 KB |
Output is correct |
17 |
Correct |
15 ms |
15248 KB |
Output is correct |
18 |
Correct |
13 ms |
14668 KB |
Output is correct |
19 |
Correct |
16 ms |
15172 KB |
Output is correct |
20 |
Correct |
18 ms |
15092 KB |
Output is correct |
21 |
Correct |
13 ms |
15052 KB |
Output is correct |
22 |
Correct |
16 ms |
15108 KB |
Output is correct |
23 |
Correct |
15 ms |
14796 KB |
Output is correct |
24 |
Correct |
15 ms |
15244 KB |
Output is correct |
25 |
Correct |
14 ms |
15000 KB |
Output is correct |
26 |
Correct |
14 ms |
15256 KB |
Output is correct |
27 |
Correct |
15 ms |
15180 KB |
Output is correct |
28 |
Correct |
15 ms |
15284 KB |
Output is correct |
29 |
Correct |
14 ms |
15436 KB |
Output is correct |
30 |
Correct |
16 ms |
15416 KB |
Output is correct |
31 |
Correct |
16 ms |
15376 KB |
Output is correct |
32 |
Correct |
16 ms |
15500 KB |
Output is correct |
33 |
Correct |
402 ms |
75100 KB |
Output is correct |
34 |
Correct |
295 ms |
50352 KB |
Output is correct |
35 |
Correct |
376 ms |
72080 KB |
Output is correct |
36 |
Correct |
297 ms |
50372 KB |
Output is correct |
37 |
Correct |
227 ms |
31336 KB |
Output is correct |
38 |
Correct |
177 ms |
27000 KB |
Output is correct |
39 |
Correct |
188 ms |
51924 KB |
Output is correct |
40 |
Correct |
173 ms |
51908 KB |
Output is correct |
41 |
Correct |
141 ms |
51916 KB |
Output is correct |
42 |
Correct |
176 ms |
37932 KB |
Output is correct |
43 |
Correct |
169 ms |
37988 KB |
Output is correct |
44 |
Correct |
375 ms |
100164 KB |
Output is correct |
45 |
Correct |
262 ms |
64472 KB |
Output is correct |
46 |
Correct |
129 ms |
26824 KB |
Output is correct |
47 |
Correct |
362 ms |
47876 KB |
Output is correct |
48 |
Correct |
147 ms |
41028 KB |
Output is correct |
49 |
Correct |
121 ms |
41024 KB |
Output is correct |
50 |
Correct |
322 ms |
43376 KB |
Output is correct |
51 |
Correct |
131 ms |
30780 KB |
Output is correct |
52 |
Correct |
356 ms |
48660 KB |
Output is correct |
53 |
Correct |
144 ms |
41404 KB |
Output is correct |
54 |
Correct |
134 ms |
51960 KB |
Output is correct |
55 |
Correct |
242 ms |
50436 KB |
Output is correct |
56 |
Correct |
214 ms |
56240 KB |
Output is correct |
57 |
Correct |
198 ms |
59324 KB |
Output is correct |
58 |
Correct |
237 ms |
56556 KB |
Output is correct |
59 |
Correct |
225 ms |
56624 KB |
Output is correct |
60 |
Correct |
10 ms |
14412 KB |
Output is correct |
61 |
Correct |
11 ms |
14436 KB |
Output is correct |
62 |
Correct |
10 ms |
14436 KB |
Output is correct |
63 |
Correct |
10 ms |
14412 KB |
Output is correct |
64 |
Correct |
390 ms |
61180 KB |
Output is correct |
65 |
Correct |
288 ms |
44540 KB |
Output is correct |
66 |
Correct |
354 ms |
62220 KB |
Output is correct |
67 |
Correct |
295 ms |
43992 KB |
Output is correct |
68 |
Correct |
218 ms |
30984 KB |
Output is correct |
69 |
Correct |
196 ms |
27700 KB |
Output is correct |
70 |
Correct |
243 ms |
44720 KB |
Output is correct |
71 |
Correct |
228 ms |
39808 KB |
Output is correct |
72 |
Correct |
262 ms |
53660 KB |
Output is correct |
73 |
Correct |
222 ms |
53440 KB |
Output is correct |
74 |
Correct |
298 ms |
50704 KB |
Output is correct |
75 |
Correct |
231 ms |
42928 KB |
Output is correct |
76 |
Correct |
170 ms |
42844 KB |
Output is correct |
77 |
Correct |
312 ms |
50612 KB |
Output is correct |
78 |
Correct |
182 ms |
42920 KB |
Output is correct |
79 |
Correct |
367 ms |
66076 KB |
Output is correct |
80 |
Correct |
280 ms |
53916 KB |
Output is correct |
81 |
Correct |
240 ms |
43312 KB |
Output is correct |
82 |
Correct |
283 ms |
53444 KB |
Output is correct |
83 |
Correct |
126 ms |
37832 KB |
Output is correct |
84 |
Correct |
316 ms |
54304 KB |
Output is correct |
85 |
Correct |
310 ms |
54308 KB |
Output is correct |
86 |
Correct |
317 ms |
54308 KB |
Output is correct |
87 |
Correct |
310 ms |
54288 KB |
Output is correct |
88 |
Correct |
319 ms |
54312 KB |
Output is correct |