// vaziat meshki-ghermeze !
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
const ll Mod = 1000000007LL;
const int N = 2e5 + 10;
const ll Inf = 2242545357980376863LL;
const ll Log = 30;
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];
// cerr << "!! " << u << " : \n";
// for(auto [x, y] : mp[u])
// cerr << x << ' ' << y << '\n';
// cerr << "------\n";
}
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];
// G[a[i]].pb(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();
}
// cerr << "!! ";
for(int i = 1; i <= n; i++){
if(!loop[i] && loop[a[i]])
a[i] = con[a[i]];
// cerr << a[i] << ' ';
G[a[i]].pb(i);
}
// cerr << '\n';
for(int i = 1; i <= n; i++)
if(a[i] == i){
// debug(i);
DFS(i);
for(auto [x, y] : mp[i])
ans -= y;
}
// ll ans = 0;
// for(int i = 1; i <= n; i++) ans += c[i];
// for(auto [la, sm] : mp[1]) ans -= sm;
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14436 KB |
Output is correct |
2 |
Correct |
9 ms |
14412 KB |
Output is correct |
3 |
Correct |
10 ms |
14412 KB |
Output is correct |
4 |
Correct |
10 ms |
14328 KB |
Output is correct |
5 |
Correct |
15 ms |
15520 KB |
Output is correct |
6 |
Correct |
14 ms |
15052 KB |
Output is correct |
7 |
Correct |
14 ms |
14836 KB |
Output is correct |
8 |
Correct |
15 ms |
15588 KB |
Output is correct |
9 |
Correct |
16 ms |
15052 KB |
Output is correct |
10 |
Correct |
13 ms |
14796 KB |
Output is correct |
11 |
Correct |
13 ms |
14676 KB |
Output is correct |
12 |
Correct |
14 ms |
15348 KB |
Output is correct |
13 |
Correct |
15 ms |
15344 KB |
Output is correct |
14 |
Correct |
14 ms |
15032 KB |
Output is correct |
15 |
Correct |
13 ms |
15028 KB |
Output is correct |
16 |
Correct |
17 ms |
15948 KB |
Output is correct |
17 |
Correct |
14 ms |
15308 KB |
Output is correct |
18 |
Correct |
12 ms |
14648 KB |
Output is correct |
19 |
Correct |
14 ms |
15176 KB |
Output is correct |
20 |
Correct |
13 ms |
15004 KB |
Output is correct |
21 |
Correct |
12 ms |
15052 KB |
Output is correct |
22 |
Correct |
14 ms |
15052 KB |
Output is correct |
23 |
Correct |
12 ms |
14796 KB |
Output is correct |
24 |
Correct |
14 ms |
15180 KB |
Output is correct |
25 |
Correct |
12 ms |
15116 KB |
Output is correct |
26 |
Correct |
12 ms |
15300 KB |
Output is correct |
27 |
Correct |
13 ms |
15180 KB |
Output is correct |
28 |
Correct |
14 ms |
15356 KB |
Output is correct |
29 |
Correct |
15 ms |
15488 KB |
Output is correct |
30 |
Correct |
16 ms |
15472 KB |
Output is correct |
31 |
Correct |
14 ms |
15436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14436 KB |
Output is correct |
2 |
Correct |
9 ms |
14412 KB |
Output is correct |
3 |
Correct |
10 ms |
14412 KB |
Output is correct |
4 |
Correct |
10 ms |
14328 KB |
Output is correct |
5 |
Correct |
15 ms |
15520 KB |
Output is correct |
6 |
Correct |
14 ms |
15052 KB |
Output is correct |
7 |
Correct |
14 ms |
14836 KB |
Output is correct |
8 |
Correct |
15 ms |
15588 KB |
Output is correct |
9 |
Correct |
16 ms |
15052 KB |
Output is correct |
10 |
Correct |
13 ms |
14796 KB |
Output is correct |
11 |
Correct |
13 ms |
14676 KB |
Output is correct |
12 |
Correct |
14 ms |
15348 KB |
Output is correct |
13 |
Correct |
15 ms |
15344 KB |
Output is correct |
14 |
Correct |
14 ms |
15032 KB |
Output is correct |
15 |
Correct |
13 ms |
15028 KB |
Output is correct |
16 |
Correct |
17 ms |
15948 KB |
Output is correct |
17 |
Correct |
14 ms |
15308 KB |
Output is correct |
18 |
Correct |
12 ms |
14648 KB |
Output is correct |
19 |
Correct |
14 ms |
15176 KB |
Output is correct |
20 |
Correct |
13 ms |
15004 KB |
Output is correct |
21 |
Correct |
12 ms |
15052 KB |
Output is correct |
22 |
Correct |
14 ms |
15052 KB |
Output is correct |
23 |
Correct |
12 ms |
14796 KB |
Output is correct |
24 |
Correct |
14 ms |
15180 KB |
Output is correct |
25 |
Correct |
12 ms |
15116 KB |
Output is correct |
26 |
Correct |
12 ms |
15300 KB |
Output is correct |
27 |
Correct |
13 ms |
15180 KB |
Output is correct |
28 |
Correct |
14 ms |
15356 KB |
Output is correct |
29 |
Correct |
15 ms |
15488 KB |
Output is correct |
30 |
Correct |
16 ms |
15472 KB |
Output is correct |
31 |
Correct |
14 ms |
15436 KB |
Output is correct |
32 |
Correct |
15 ms |
15564 KB |
Output is correct |
33 |
Correct |
379 ms |
75120 KB |
Output is correct |
34 |
Correct |
292 ms |
50336 KB |
Output is correct |
35 |
Correct |
369 ms |
72128 KB |
Output is correct |
36 |
Correct |
287 ms |
50356 KB |
Output is correct |
37 |
Correct |
198 ms |
31340 KB |
Output is correct |
38 |
Correct |
187 ms |
27024 KB |
Output is correct |
39 |
Correct |
199 ms |
52040 KB |
Output is correct |
40 |
Correct |
172 ms |
51976 KB |
Output is correct |
41 |
Correct |
143 ms |
51864 KB |
Output is correct |
42 |
Correct |
194 ms |
37976 KB |
Output is correct |
43 |
Correct |
169 ms |
37868 KB |
Output is correct |
44 |
Correct |
365 ms |
100140 KB |
Output is correct |
45 |
Correct |
277 ms |
64632 KB |
Output is correct |
46 |
Correct |
107 ms |
26820 KB |
Output is correct |
47 |
Correct |
347 ms |
47984 KB |
Output is correct |
48 |
Correct |
171 ms |
40992 KB |
Output is correct |
49 |
Correct |
125 ms |
40992 KB |
Output is correct |
50 |
Correct |
303 ms |
43372 KB |
Output is correct |
51 |
Correct |
118 ms |
30784 KB |
Output is correct |
52 |
Correct |
350 ms |
48656 KB |
Output is correct |
53 |
Correct |
139 ms |
41400 KB |
Output is correct |
54 |
Correct |
132 ms |
51856 KB |
Output is correct |
55 |
Correct |
225 ms |
50420 KB |
Output is correct |
56 |
Correct |
201 ms |
56324 KB |
Output is correct |
57 |
Correct |
195 ms |
59232 KB |
Output is correct |
58 |
Correct |
222 ms |
56516 KB |
Output is correct |
59 |
Correct |
220 ms |
56636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14436 KB |
Output is correct |
2 |
Correct |
9 ms |
14412 KB |
Output is correct |
3 |
Correct |
10 ms |
14412 KB |
Output is correct |
4 |
Correct |
10 ms |
14328 KB |
Output is correct |
5 |
Correct |
15 ms |
15520 KB |
Output is correct |
6 |
Correct |
14 ms |
15052 KB |
Output is correct |
7 |
Correct |
14 ms |
14836 KB |
Output is correct |
8 |
Correct |
15 ms |
15588 KB |
Output is correct |
9 |
Correct |
16 ms |
15052 KB |
Output is correct |
10 |
Correct |
13 ms |
14796 KB |
Output is correct |
11 |
Correct |
13 ms |
14676 KB |
Output is correct |
12 |
Correct |
14 ms |
15348 KB |
Output is correct |
13 |
Correct |
15 ms |
15344 KB |
Output is correct |
14 |
Correct |
14 ms |
15032 KB |
Output is correct |
15 |
Correct |
13 ms |
15028 KB |
Output is correct |
16 |
Correct |
17 ms |
15948 KB |
Output is correct |
17 |
Correct |
14 ms |
15308 KB |
Output is correct |
18 |
Correct |
12 ms |
14648 KB |
Output is correct |
19 |
Correct |
14 ms |
15176 KB |
Output is correct |
20 |
Correct |
13 ms |
15004 KB |
Output is correct |
21 |
Correct |
12 ms |
15052 KB |
Output is correct |
22 |
Correct |
14 ms |
15052 KB |
Output is correct |
23 |
Correct |
12 ms |
14796 KB |
Output is correct |
24 |
Correct |
14 ms |
15180 KB |
Output is correct |
25 |
Correct |
12 ms |
15116 KB |
Output is correct |
26 |
Correct |
12 ms |
15300 KB |
Output is correct |
27 |
Correct |
13 ms |
15180 KB |
Output is correct |
28 |
Correct |
14 ms |
15356 KB |
Output is correct |
29 |
Correct |
15 ms |
15488 KB |
Output is correct |
30 |
Correct |
16 ms |
15472 KB |
Output is correct |
31 |
Correct |
14 ms |
15436 KB |
Output is correct |
32 |
Correct |
15 ms |
15564 KB |
Output is correct |
33 |
Correct |
379 ms |
75120 KB |
Output is correct |
34 |
Correct |
292 ms |
50336 KB |
Output is correct |
35 |
Correct |
369 ms |
72128 KB |
Output is correct |
36 |
Correct |
287 ms |
50356 KB |
Output is correct |
37 |
Correct |
198 ms |
31340 KB |
Output is correct |
38 |
Correct |
187 ms |
27024 KB |
Output is correct |
39 |
Correct |
199 ms |
52040 KB |
Output is correct |
40 |
Correct |
172 ms |
51976 KB |
Output is correct |
41 |
Correct |
143 ms |
51864 KB |
Output is correct |
42 |
Correct |
194 ms |
37976 KB |
Output is correct |
43 |
Correct |
169 ms |
37868 KB |
Output is correct |
44 |
Correct |
365 ms |
100140 KB |
Output is correct |
45 |
Correct |
277 ms |
64632 KB |
Output is correct |
46 |
Correct |
107 ms |
26820 KB |
Output is correct |
47 |
Correct |
347 ms |
47984 KB |
Output is correct |
48 |
Correct |
171 ms |
40992 KB |
Output is correct |
49 |
Correct |
125 ms |
40992 KB |
Output is correct |
50 |
Correct |
303 ms |
43372 KB |
Output is correct |
51 |
Correct |
118 ms |
30784 KB |
Output is correct |
52 |
Correct |
350 ms |
48656 KB |
Output is correct |
53 |
Correct |
139 ms |
41400 KB |
Output is correct |
54 |
Correct |
132 ms |
51856 KB |
Output is correct |
55 |
Correct |
225 ms |
50420 KB |
Output is correct |
56 |
Correct |
201 ms |
56324 KB |
Output is correct |
57 |
Correct |
195 ms |
59232 KB |
Output is correct |
58 |
Correct |
222 ms |
56516 KB |
Output is correct |
59 |
Correct |
220 ms |
56636 KB |
Output is correct |
60 |
Correct |
9 ms |
14444 KB |
Output is correct |
61 |
Correct |
10 ms |
14420 KB |
Output is correct |
62 |
Correct |
10 ms |
14412 KB |
Output is correct |
63 |
Correct |
10 ms |
14412 KB |
Output is correct |
64 |
Correct |
393 ms |
66232 KB |
Output is correct |
65 |
Correct |
283 ms |
49732 KB |
Output is correct |
66 |
Correct |
369 ms |
67284 KB |
Output is correct |
67 |
Correct |
277 ms |
49092 KB |
Output is correct |
68 |
Correct |
214 ms |
36164 KB |
Output is correct |
69 |
Correct |
193 ms |
32836 KB |
Output is correct |
70 |
Correct |
265 ms |
49924 KB |
Output is correct |
71 |
Correct |
231 ms |
44872 KB |
Output is correct |
72 |
Correct |
257 ms |
58640 KB |
Output is correct |
73 |
Correct |
229 ms |
58660 KB |
Output is correct |
74 |
Correct |
310 ms |
55676 KB |
Output is correct |
75 |
Correct |
206 ms |
48048 KB |
Output is correct |
76 |
Correct |
178 ms |
48116 KB |
Output is correct |
77 |
Correct |
339 ms |
55844 KB |
Output is correct |
78 |
Correct |
198 ms |
48040 KB |
Output is correct |
79 |
Correct |
360 ms |
71064 KB |
Output is correct |
80 |
Correct |
300 ms |
59040 KB |
Output is correct |
81 |
Correct |
238 ms |
48428 KB |
Output is correct |
82 |
Correct |
294 ms |
58824 KB |
Output is correct |
83 |
Correct |
154 ms |
42980 KB |
Output is correct |
84 |
Correct |
374 ms |
58924 KB |
Output is correct |
85 |
Correct |
334 ms |
58864 KB |
Output is correct |
86 |
Correct |
348 ms |
58800 KB |
Output is correct |
87 |
Correct |
323 ms |
58932 KB |
Output is correct |
88 |
Correct |
336 ms |
58932 KB |
Output is correct |