#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MXN = 2e5 + 1;
const int MX = 2e5 + 1;
struct node {
ll v;
node *left, *right;
node() { v = 0, left = right = 0; }
void print(int lb, int rb) {
if(lb == rb) cout << lb << ":" << v << " ";
else {
int m = (lb + rb) / 2;
if(left) left->print(lb, m);
if(right) right->print(m + 1, rb);
}
}
ll del(ll rem, int r, int lb, int rb) {
r = min(r, rb);
if(r < lb) return 0;
if(rb == r && v <= rem) {
ll d = v;
v -= d;
left = right = 0;
return d;
}
if(rb == r && lb == rb) {
v -= rem;
return rem;
}
int m = (lb + rb) / 2;
ll d = right ? right->del(rem, r, m + 1, rb) : 0;
if(rem - d > 0) {
d += left ? left->del(rem - d, r, lb, m) : 0;
}
v -= d;
return d;
}
};
void upd(node* cur, int k, ll v) {
int lb = 1, rb = MX;
while(lb != rb) {
cur->v += v;
int m = (lb + rb) / 2;
if(k <= m) {
if(!cur->left) cur->left = new node();
cur = cur->left;
rb = m;
} else {
if(!cur->right) cur->right = new node();
cur = cur->right;
lb = m + 1;
}
}
cur->v += v;
}
ll query(node* cur, int k) {
ll ans = 0;
int lb = 1, rb = MX;
while(lb != rb) {
int m = (lb + rb) / 2;
if(k <= m) {
if(cur->right) ans += cur->right->v;
if(!cur->left) return ans;
cur = cur->left;
rb = m;
} else {
if(!cur->right) return ans;
cur = cur->right;
lb = m + 1;
}
}
return ans + cur->v;
}
void merge(node* cur, node* other) {
other->v += cur->v;
if(cur->left) {
if(other->left) {
merge(cur->left, other->left);
} else {
other->left = cur->left;
}
}
if(cur->right) {
if(other->right) {
merge(cur->right, other->right);
} else {
other->right = cur->right;
}
}
delete(cur);
}
int A[MXN], H[MXN], C[MXN], in[MXN];
node *segts[MXN];
int main() {
int N; cin >> N;
ll sm = 0;
map<int, vector<int>> mp;
for(int i = 1; i <= N; i++) {
cin >> A[i] >> H[i] >> C[i];
mp[H[i]].push_back(i);
in[A[i]]++;
segts[i] = new node();
sm += C[i];
}
int rnk = 1;
for(auto [u, v] : mp) {
for(int u2 : v) H[u2] = rnk;
rnk++;
}
queue<int> q;
for(int i = 1; i <= N; i++) if(in[i] == 0) q.push(i);
while(!q.empty()) {
int s = q.front();
q.pop();
// cout << s << endl;
// segts[s]->print(1, MX);
// cout << endl;
segts[s]->del(C[s], H[s] - 1, 1, MX);
upd(segts[s], H[s], C[s]);
merge(segts[s], segts[A[s]]);
if(--in[A[s]] == 0) q.push(A[s]);
}
for(int i = 1; i <= N; i++) {
if(in[i] != 0) {
// cout << i << endl;
vector<int> cyc; cyc.push_back(i);
while(in[A[cyc.back()]]) {
in[A[cyc.back()]] = 0;
cyc.push_back(A[cyc.back()]);
}
cyc.pop_back();
map<int, ll> cnt;
for(int u : cyc) {
cnt[H[u]] += C[u];
if(u != i) {
merge(segts[u], segts[i]);
}
}
ll best = segts[i]->v;
for(auto [u, v] : cnt) {
best = max(best, v + query(segts[i], u));
}
sm -= best;
}
}
cout << sm << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
10 ms |
6492 KB |
Output is correct |
6 |
Correct |
8 ms |
5724 KB |
Output is correct |
7 |
Correct |
7 ms |
5720 KB |
Output is correct |
8 |
Correct |
10 ms |
6492 KB |
Output is correct |
9 |
Correct |
8 ms |
5720 KB |
Output is correct |
10 |
Correct |
7 ms |
5724 KB |
Output is correct |
11 |
Correct |
7 ms |
5720 KB |
Output is correct |
12 |
Correct |
8 ms |
5724 KB |
Output is correct |
13 |
Correct |
5 ms |
4676 KB |
Output is correct |
14 |
Correct |
8 ms |
5896 KB |
Output is correct |
15 |
Correct |
6 ms |
4700 KB |
Output is correct |
16 |
Correct |
11 ms |
6484 KB |
Output is correct |
17 |
Correct |
7 ms |
5468 KB |
Output is correct |
18 |
Correct |
6 ms |
5212 KB |
Output is correct |
19 |
Correct |
9 ms |
6492 KB |
Output is correct |
20 |
Correct |
10 ms |
5980 KB |
Output is correct |
21 |
Correct |
7 ms |
5980 KB |
Output is correct |
22 |
Correct |
8 ms |
5384 KB |
Output is correct |
23 |
Correct |
8 ms |
4700 KB |
Output is correct |
24 |
Correct |
10 ms |
5540 KB |
Output is correct |
25 |
Correct |
6 ms |
4700 KB |
Output is correct |
26 |
Correct |
10 ms |
5220 KB |
Output is correct |
27 |
Correct |
9 ms |
6196 KB |
Output is correct |
28 |
Correct |
8 ms |
5980 KB |
Output is correct |
29 |
Correct |
7 ms |
5724 KB |
Output is correct |
30 |
Correct |
6 ms |
5212 KB |
Output is correct |
31 |
Correct |
7 ms |
5460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
10 ms |
6492 KB |
Output is correct |
6 |
Correct |
8 ms |
5724 KB |
Output is correct |
7 |
Correct |
7 ms |
5720 KB |
Output is correct |
8 |
Correct |
10 ms |
6492 KB |
Output is correct |
9 |
Correct |
8 ms |
5720 KB |
Output is correct |
10 |
Correct |
7 ms |
5724 KB |
Output is correct |
11 |
Correct |
7 ms |
5720 KB |
Output is correct |
12 |
Correct |
8 ms |
5724 KB |
Output is correct |
13 |
Correct |
5 ms |
4676 KB |
Output is correct |
14 |
Correct |
8 ms |
5896 KB |
Output is correct |
15 |
Correct |
6 ms |
4700 KB |
Output is correct |
16 |
Correct |
11 ms |
6484 KB |
Output is correct |
17 |
Correct |
7 ms |
5468 KB |
Output is correct |
18 |
Correct |
6 ms |
5212 KB |
Output is correct |
19 |
Correct |
9 ms |
6492 KB |
Output is correct |
20 |
Correct |
10 ms |
5980 KB |
Output is correct |
21 |
Correct |
7 ms |
5980 KB |
Output is correct |
22 |
Correct |
8 ms |
5384 KB |
Output is correct |
23 |
Correct |
8 ms |
4700 KB |
Output is correct |
24 |
Correct |
10 ms |
5540 KB |
Output is correct |
25 |
Correct |
6 ms |
4700 KB |
Output is correct |
26 |
Correct |
10 ms |
5220 KB |
Output is correct |
27 |
Correct |
9 ms |
6196 KB |
Output is correct |
28 |
Correct |
8 ms |
5980 KB |
Output is correct |
29 |
Correct |
7 ms |
5724 KB |
Output is correct |
30 |
Correct |
6 ms |
5212 KB |
Output is correct |
31 |
Correct |
7 ms |
5460 KB |
Output is correct |
32 |
Correct |
10 ms |
6492 KB |
Output is correct |
33 |
Correct |
894 ms |
113748 KB |
Output is correct |
34 |
Correct |
630 ms |
60448 KB |
Output is correct |
35 |
Correct |
886 ms |
114000 KB |
Output is correct |
36 |
Correct |
592 ms |
60768 KB |
Output is correct |
37 |
Correct |
443 ms |
52100 KB |
Output is correct |
38 |
Correct |
381 ms |
51012 KB |
Output is correct |
39 |
Correct |
408 ms |
75660 KB |
Output is correct |
40 |
Correct |
231 ms |
13648 KB |
Output is correct |
41 |
Correct |
181 ms |
12344 KB |
Output is correct |
42 |
Correct |
442 ms |
78956 KB |
Output is correct |
43 |
Correct |
229 ms |
14160 KB |
Output is correct |
44 |
Correct |
738 ms |
111456 KB |
Output is correct |
45 |
Correct |
435 ms |
54368 KB |
Output is correct |
46 |
Correct |
269 ms |
37244 KB |
Output is correct |
47 |
Correct |
523 ms |
85468 KB |
Output is correct |
48 |
Correct |
355 ms |
65028 KB |
Output is correct |
49 |
Correct |
276 ms |
65408 KB |
Output is correct |
50 |
Correct |
454 ms |
39772 KB |
Output is correct |
51 |
Correct |
234 ms |
13232 KB |
Output is correct |
52 |
Correct |
484 ms |
39980 KB |
Output is correct |
53 |
Correct |
235 ms |
12880 KB |
Output is correct |
54 |
Correct |
258 ms |
39312 KB |
Output is correct |
55 |
Correct |
385 ms |
81744 KB |
Output is correct |
56 |
Correct |
301 ms |
59340 KB |
Output is correct |
57 |
Correct |
270 ms |
50460 KB |
Output is correct |
58 |
Correct |
301 ms |
39588 KB |
Output is correct |
59 |
Correct |
295 ms |
39472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
10 ms |
6492 KB |
Output is correct |
6 |
Correct |
8 ms |
5724 KB |
Output is correct |
7 |
Correct |
7 ms |
5720 KB |
Output is correct |
8 |
Correct |
10 ms |
6492 KB |
Output is correct |
9 |
Correct |
8 ms |
5720 KB |
Output is correct |
10 |
Correct |
7 ms |
5724 KB |
Output is correct |
11 |
Correct |
7 ms |
5720 KB |
Output is correct |
12 |
Correct |
8 ms |
5724 KB |
Output is correct |
13 |
Correct |
5 ms |
4676 KB |
Output is correct |
14 |
Correct |
8 ms |
5896 KB |
Output is correct |
15 |
Correct |
6 ms |
4700 KB |
Output is correct |
16 |
Correct |
11 ms |
6484 KB |
Output is correct |
17 |
Correct |
7 ms |
5468 KB |
Output is correct |
18 |
Correct |
6 ms |
5212 KB |
Output is correct |
19 |
Correct |
9 ms |
6492 KB |
Output is correct |
20 |
Correct |
10 ms |
5980 KB |
Output is correct |
21 |
Correct |
7 ms |
5980 KB |
Output is correct |
22 |
Correct |
8 ms |
5384 KB |
Output is correct |
23 |
Correct |
8 ms |
4700 KB |
Output is correct |
24 |
Correct |
10 ms |
5540 KB |
Output is correct |
25 |
Correct |
6 ms |
4700 KB |
Output is correct |
26 |
Correct |
10 ms |
5220 KB |
Output is correct |
27 |
Correct |
9 ms |
6196 KB |
Output is correct |
28 |
Correct |
8 ms |
5980 KB |
Output is correct |
29 |
Correct |
7 ms |
5724 KB |
Output is correct |
30 |
Correct |
6 ms |
5212 KB |
Output is correct |
31 |
Correct |
7 ms |
5460 KB |
Output is correct |
32 |
Correct |
10 ms |
6492 KB |
Output is correct |
33 |
Correct |
894 ms |
113748 KB |
Output is correct |
34 |
Correct |
630 ms |
60448 KB |
Output is correct |
35 |
Correct |
886 ms |
114000 KB |
Output is correct |
36 |
Correct |
592 ms |
60768 KB |
Output is correct |
37 |
Correct |
443 ms |
52100 KB |
Output is correct |
38 |
Correct |
381 ms |
51012 KB |
Output is correct |
39 |
Correct |
408 ms |
75660 KB |
Output is correct |
40 |
Correct |
231 ms |
13648 KB |
Output is correct |
41 |
Correct |
181 ms |
12344 KB |
Output is correct |
42 |
Correct |
442 ms |
78956 KB |
Output is correct |
43 |
Correct |
229 ms |
14160 KB |
Output is correct |
44 |
Correct |
738 ms |
111456 KB |
Output is correct |
45 |
Correct |
435 ms |
54368 KB |
Output is correct |
46 |
Correct |
269 ms |
37244 KB |
Output is correct |
47 |
Correct |
523 ms |
85468 KB |
Output is correct |
48 |
Correct |
355 ms |
65028 KB |
Output is correct |
49 |
Correct |
276 ms |
65408 KB |
Output is correct |
50 |
Correct |
454 ms |
39772 KB |
Output is correct |
51 |
Correct |
234 ms |
13232 KB |
Output is correct |
52 |
Correct |
484 ms |
39980 KB |
Output is correct |
53 |
Correct |
235 ms |
12880 KB |
Output is correct |
54 |
Correct |
258 ms |
39312 KB |
Output is correct |
55 |
Correct |
385 ms |
81744 KB |
Output is correct |
56 |
Correct |
301 ms |
59340 KB |
Output is correct |
57 |
Correct |
270 ms |
50460 KB |
Output is correct |
58 |
Correct |
301 ms |
39588 KB |
Output is correct |
59 |
Correct |
295 ms |
39472 KB |
Output is correct |
60 |
Correct |
1 ms |
4444 KB |
Output is correct |
61 |
Correct |
1 ms |
4444 KB |
Output is correct |
62 |
Correct |
1 ms |
4444 KB |
Output is correct |
63 |
Correct |
1 ms |
4444 KB |
Output is correct |
64 |
Correct |
854 ms |
107840 KB |
Output is correct |
65 |
Correct |
570 ms |
54472 KB |
Output is correct |
66 |
Correct |
847 ms |
108084 KB |
Output is correct |
67 |
Correct |
568 ms |
54500 KB |
Output is correct |
68 |
Correct |
399 ms |
45216 KB |
Output is correct |
69 |
Correct |
352 ms |
44724 KB |
Output is correct |
70 |
Correct |
467 ms |
42260 KB |
Output is correct |
71 |
Correct |
226 ms |
13424 KB |
Output is correct |
72 |
Correct |
445 ms |
46488 KB |
Output is correct |
73 |
Correct |
246 ms |
13564 KB |
Output is correct |
74 |
Correct |
644 ms |
86148 KB |
Output is correct |
75 |
Correct |
381 ms |
65492 KB |
Output is correct |
76 |
Correct |
403 ms |
66024 KB |
Output is correct |
77 |
Correct |
451 ms |
45260 KB |
Output is correct |
78 |
Correct |
228 ms |
13396 KB |
Output is correct |
79 |
Correct |
695 ms |
74328 KB |
Output is correct |
80 |
Correct |
385 ms |
37144 KB |
Output is correct |
81 |
Correct |
291 ms |
32644 KB |
Output is correct |
82 |
Correct |
421 ms |
46492 KB |
Output is correct |
83 |
Correct |
312 ms |
33108 KB |
Output is correct |
84 |
Correct |
571 ms |
60144 KB |
Output is correct |
85 |
Correct |
565 ms |
60280 KB |
Output is correct |
86 |
Correct |
523 ms |
54820 KB |
Output is correct |
87 |
Correct |
582 ms |
60224 KB |
Output is correct |
88 |
Correct |
566 ms |
60088 KB |
Output is correct |