// 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14316 KB |
Output is correct |
2 |
Correct |
10 ms |
14344 KB |
Output is correct |
3 |
Correct |
10 ms |
14412 KB |
Output is correct |
4 |
Correct |
10 ms |
14420 KB |
Output is correct |
5 |
Correct |
16 ms |
15580 KB |
Output is correct |
6 |
Correct |
17 ms |
15200 KB |
Output is correct |
7 |
Correct |
14 ms |
15036 KB |
Output is correct |
8 |
Correct |
17 ms |
15708 KB |
Output is correct |
9 |
Correct |
14 ms |
15180 KB |
Output is correct |
10 |
Correct |
13 ms |
14936 KB |
Output is correct |
11 |
Correct |
13 ms |
14796 KB |
Output is correct |
12 |
Correct |
14 ms |
15436 KB |
Output is correct |
13 |
Correct |
12 ms |
15392 KB |
Output is correct |
14 |
Correct |
16 ms |
15096 KB |
Output is correct |
15 |
Correct |
13 ms |
15088 KB |
Output is correct |
16 |
Correct |
16 ms |
15948 KB |
Output is correct |
17 |
Correct |
14 ms |
15436 KB |
Output is correct |
18 |
Correct |
12 ms |
14796 KB |
Output is correct |
19 |
Correct |
16 ms |
15308 KB |
Output is correct |
20 |
Correct |
13 ms |
15112 KB |
Output is correct |
21 |
Correct |
15 ms |
15204 KB |
Output is correct |
22 |
Correct |
13 ms |
15180 KB |
Output is correct |
23 |
Correct |
12 ms |
14848 KB |
Output is correct |
24 |
Correct |
15 ms |
15332 KB |
Output is correct |
25 |
Correct |
13 ms |
15180 KB |
Output is correct |
26 |
Correct |
14 ms |
15480 KB |
Output is correct |
27 |
Correct |
15 ms |
15320 KB |
Output is correct |
28 |
Correct |
15 ms |
15436 KB |
Output is correct |
29 |
Correct |
16 ms |
15524 KB |
Output is correct |
30 |
Correct |
16 ms |
15584 KB |
Output is correct |
31 |
Correct |
16 ms |
15492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14316 KB |
Output is correct |
2 |
Correct |
10 ms |
14344 KB |
Output is correct |
3 |
Correct |
10 ms |
14412 KB |
Output is correct |
4 |
Correct |
10 ms |
14420 KB |
Output is correct |
5 |
Correct |
16 ms |
15580 KB |
Output is correct |
6 |
Correct |
17 ms |
15200 KB |
Output is correct |
7 |
Correct |
14 ms |
15036 KB |
Output is correct |
8 |
Correct |
17 ms |
15708 KB |
Output is correct |
9 |
Correct |
14 ms |
15180 KB |
Output is correct |
10 |
Correct |
13 ms |
14936 KB |
Output is correct |
11 |
Correct |
13 ms |
14796 KB |
Output is correct |
12 |
Correct |
14 ms |
15436 KB |
Output is correct |
13 |
Correct |
12 ms |
15392 KB |
Output is correct |
14 |
Correct |
16 ms |
15096 KB |
Output is correct |
15 |
Correct |
13 ms |
15088 KB |
Output is correct |
16 |
Correct |
16 ms |
15948 KB |
Output is correct |
17 |
Correct |
14 ms |
15436 KB |
Output is correct |
18 |
Correct |
12 ms |
14796 KB |
Output is correct |
19 |
Correct |
16 ms |
15308 KB |
Output is correct |
20 |
Correct |
13 ms |
15112 KB |
Output is correct |
21 |
Correct |
15 ms |
15204 KB |
Output is correct |
22 |
Correct |
13 ms |
15180 KB |
Output is correct |
23 |
Correct |
12 ms |
14848 KB |
Output is correct |
24 |
Correct |
15 ms |
15332 KB |
Output is correct |
25 |
Correct |
13 ms |
15180 KB |
Output is correct |
26 |
Correct |
14 ms |
15480 KB |
Output is correct |
27 |
Correct |
15 ms |
15320 KB |
Output is correct |
28 |
Correct |
15 ms |
15436 KB |
Output is correct |
29 |
Correct |
16 ms |
15524 KB |
Output is correct |
30 |
Correct |
16 ms |
15584 KB |
Output is correct |
31 |
Correct |
16 ms |
15492 KB |
Output is correct |
32 |
Correct |
15 ms |
15700 KB |
Output is correct |
33 |
Correct |
404 ms |
76256 KB |
Output is correct |
34 |
Correct |
292 ms |
51380 KB |
Output is correct |
35 |
Correct |
384 ms |
73252 KB |
Output is correct |
36 |
Correct |
303 ms |
51448 KB |
Output is correct |
37 |
Correct |
201 ms |
32512 KB |
Output is correct |
38 |
Correct |
183 ms |
28028 KB |
Output is correct |
39 |
Correct |
185 ms |
53144 KB |
Output is correct |
40 |
Correct |
169 ms |
53040 KB |
Output is correct |
41 |
Correct |
123 ms |
53064 KB |
Output is correct |
42 |
Correct |
178 ms |
39056 KB |
Output is correct |
43 |
Correct |
179 ms |
39016 KB |
Output is correct |
44 |
Correct |
372 ms |
101480 KB |
Output is correct |
45 |
Correct |
268 ms |
65676 KB |
Output is correct |
46 |
Correct |
110 ms |
27944 KB |
Output is correct |
47 |
Correct |
306 ms |
49040 KB |
Output is correct |
48 |
Correct |
151 ms |
42048 KB |
Output is correct |
49 |
Correct |
131 ms |
42092 KB |
Output is correct |
50 |
Correct |
364 ms |
44476 KB |
Output is correct |
51 |
Correct |
119 ms |
31960 KB |
Output is correct |
52 |
Correct |
353 ms |
49604 KB |
Output is correct |
53 |
Correct |
154 ms |
42508 KB |
Output is correct |
54 |
Correct |
130 ms |
52952 KB |
Output is correct |
55 |
Correct |
281 ms |
51436 KB |
Output is correct |
56 |
Correct |
210 ms |
57412 KB |
Output is correct |
57 |
Correct |
194 ms |
60372 KB |
Output is correct |
58 |
Correct |
223 ms |
57688 KB |
Output is correct |
59 |
Correct |
250 ms |
57704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14316 KB |
Output is correct |
2 |
Correct |
10 ms |
14344 KB |
Output is correct |
3 |
Correct |
10 ms |
14412 KB |
Output is correct |
4 |
Correct |
10 ms |
14420 KB |
Output is correct |
5 |
Correct |
16 ms |
15580 KB |
Output is correct |
6 |
Correct |
17 ms |
15200 KB |
Output is correct |
7 |
Correct |
14 ms |
15036 KB |
Output is correct |
8 |
Correct |
17 ms |
15708 KB |
Output is correct |
9 |
Correct |
14 ms |
15180 KB |
Output is correct |
10 |
Correct |
13 ms |
14936 KB |
Output is correct |
11 |
Correct |
13 ms |
14796 KB |
Output is correct |
12 |
Correct |
14 ms |
15436 KB |
Output is correct |
13 |
Correct |
12 ms |
15392 KB |
Output is correct |
14 |
Correct |
16 ms |
15096 KB |
Output is correct |
15 |
Correct |
13 ms |
15088 KB |
Output is correct |
16 |
Correct |
16 ms |
15948 KB |
Output is correct |
17 |
Correct |
14 ms |
15436 KB |
Output is correct |
18 |
Correct |
12 ms |
14796 KB |
Output is correct |
19 |
Correct |
16 ms |
15308 KB |
Output is correct |
20 |
Correct |
13 ms |
15112 KB |
Output is correct |
21 |
Correct |
15 ms |
15204 KB |
Output is correct |
22 |
Correct |
13 ms |
15180 KB |
Output is correct |
23 |
Correct |
12 ms |
14848 KB |
Output is correct |
24 |
Correct |
15 ms |
15332 KB |
Output is correct |
25 |
Correct |
13 ms |
15180 KB |
Output is correct |
26 |
Correct |
14 ms |
15480 KB |
Output is correct |
27 |
Correct |
15 ms |
15320 KB |
Output is correct |
28 |
Correct |
15 ms |
15436 KB |
Output is correct |
29 |
Correct |
16 ms |
15524 KB |
Output is correct |
30 |
Correct |
16 ms |
15584 KB |
Output is correct |
31 |
Correct |
16 ms |
15492 KB |
Output is correct |
32 |
Correct |
15 ms |
15700 KB |
Output is correct |
33 |
Correct |
404 ms |
76256 KB |
Output is correct |
34 |
Correct |
292 ms |
51380 KB |
Output is correct |
35 |
Correct |
384 ms |
73252 KB |
Output is correct |
36 |
Correct |
303 ms |
51448 KB |
Output is correct |
37 |
Correct |
201 ms |
32512 KB |
Output is correct |
38 |
Correct |
183 ms |
28028 KB |
Output is correct |
39 |
Correct |
185 ms |
53144 KB |
Output is correct |
40 |
Correct |
169 ms |
53040 KB |
Output is correct |
41 |
Correct |
123 ms |
53064 KB |
Output is correct |
42 |
Correct |
178 ms |
39056 KB |
Output is correct |
43 |
Correct |
179 ms |
39016 KB |
Output is correct |
44 |
Correct |
372 ms |
101480 KB |
Output is correct |
45 |
Correct |
268 ms |
65676 KB |
Output is correct |
46 |
Correct |
110 ms |
27944 KB |
Output is correct |
47 |
Correct |
306 ms |
49040 KB |
Output is correct |
48 |
Correct |
151 ms |
42048 KB |
Output is correct |
49 |
Correct |
131 ms |
42092 KB |
Output is correct |
50 |
Correct |
364 ms |
44476 KB |
Output is correct |
51 |
Correct |
119 ms |
31960 KB |
Output is correct |
52 |
Correct |
353 ms |
49604 KB |
Output is correct |
53 |
Correct |
154 ms |
42508 KB |
Output is correct |
54 |
Correct |
130 ms |
52952 KB |
Output is correct |
55 |
Correct |
281 ms |
51436 KB |
Output is correct |
56 |
Correct |
210 ms |
57412 KB |
Output is correct |
57 |
Correct |
194 ms |
60372 KB |
Output is correct |
58 |
Correct |
223 ms |
57688 KB |
Output is correct |
59 |
Correct |
250 ms |
57704 KB |
Output is correct |
60 |
Incorrect |
9 ms |
14348 KB |
Output isn't correct |
61 |
Halted |
0 ms |
0 KB |
- |