#include <iostream>
#include <vector>
#include <set>
#include <queue>
#include <map>
using namespace std;
const int maxN = 200'000;
int N;
int A[1+maxN];
int H[1+maxN];
long long C[1+maxN];
vector<int> indegree(1+maxN, 0);
vector<int> subtree(1+maxN, 1);
vector<int> children[1+maxN];
queue<int> tbv;
map<int, long long> X[1+maxN];
void map_insert(int u, int l, long long v)
{
if(X[u].find(l) == X[u].end()) X[u][l] = 0;
X[u][l] += v;
}
int prv;
void merge(int u, int v) // if not cycle: u is parent, v is child
{
if(subtree[u] < subtree[v]) swap(u, v);
for(pair<int, long long> c: X[v])
map_insert(u, c.first, c.second);
X[v].clear();
prv = u;
}
int main()
{
cin >> N;
for(int i = 1; i <= N; i++)
{
cin >> A[i] >> H[i] >> C[i];
children[ A[i] ].push_back(i);
indegree[ A[i] ]++;
}
for(int i = 1; i <= N; i++)
{
if(indegree[i] == 0) tbv.push(i);
}
while(!tbv.empty())
{
int u = tbv.front();
tbv.pop();
// cerr << "u = " << u << '\n';
int max_child = -1;
for(int v: children[u])
{
if(v == A[u] || v == u) continue;
if(indegree[v] == 0)
{
if(max_child == -1 || subtree[v] > subtree[max_child])
max_child = v;
}
}
indegree[ A[u] ]--;
subtree[ A[u] ] += subtree[u];
if(indegree[ A[u] ] == 0)
tbv.push( A[u] );
// cerr << "max child = " << max_child << '\n';
if(max_child == -1)
{
map_insert(u, H[u]+1, +C[u]);
}
else
{
swap(X[u], X[max_child]);
for(int v: children[u])
{
if(indegree[v] == 0 && v != max_child)
{
merge(u, v);
}
}
map_insert(u, 1, +C[u]);
map_insert(u, H[u]+1, +C[u]);
// cerr << "intermediate: ";
// for(pair<int, long long> q: *X[u])
// {
// cerr << q.first << ' ' << q.second << " | ";
// }
// cerr << '\n';
long long Y = +C[u];
while(Y > 0)
{
map<int, long long>::iterator it = X[u].upper_bound(H[u]);
it--;
// cerr << "it = " << it->first << ' ' << it->second << '\n';
auto q = *it;
X[u].erase(it);
long long delta_stair = min(q.second, Y);
q.second -= delta_stair;
Y -= delta_stair;
if(q.second > 0)
map_insert(u, q.first, q.second);
}
}
// for(pair<int, long long> q: *X[u])
// {
// cerr << q.first << ' ' << q.second << " | ";
// }
// cerr << '\n';
}
long long ans = 0;
vector<bool> visit(1+N, 0);
for(int i = 1; i <= N; i++)
{
if(indegree[i] == 0) continue;
if(visit[i]) continue;
vector<int> F{i};
for(int u = A[i]; u != i; u = A[u])
F.push_back(u);
prv = i;
for(int u:F)
{
visit[u] = 1;
for(int q: children[u])
{
if(indegree[q] == 0)
{
merge(prv, q);
}
}
}
for(int f:F)
{
map_insert(prv, 1, +C[f]);
map_insert(prv, H[f], -C[f]);
map_insert(prv, H[f]+1, +C[f]);
}
long long cycle_ans = 1'000'000'000'000'000'000;
long long curr = 0;
for(pair<int, long long> z: X[prv])
{
curr += z.second;
cycle_ans = min(cycle_ans, curr);
}
ans += cycle_ans;
}
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
15948 KB |
Output is correct |
2 |
Correct |
9 ms |
15972 KB |
Output is correct |
3 |
Correct |
9 ms |
15968 KB |
Output is correct |
4 |
Correct |
9 ms |
15980 KB |
Output is correct |
5 |
Correct |
20 ms |
16668 KB |
Output is correct |
6 |
Correct |
18 ms |
16460 KB |
Output is correct |
7 |
Correct |
18 ms |
16496 KB |
Output is correct |
8 |
Correct |
21 ms |
16588 KB |
Output is correct |
9 |
Correct |
18 ms |
16460 KB |
Output is correct |
10 |
Correct |
17 ms |
16472 KB |
Output is correct |
11 |
Correct |
17 ms |
16480 KB |
Output is correct |
12 |
Correct |
19 ms |
16260 KB |
Output is correct |
13 |
Correct |
17 ms |
16332 KB |
Output is correct |
14 |
Correct |
18 ms |
16352 KB |
Output is correct |
15 |
Correct |
17 ms |
16244 KB |
Output is correct |
16 |
Correct |
23 ms |
16512 KB |
Output is correct |
17 |
Correct |
19 ms |
16460 KB |
Output is correct |
18 |
Correct |
16 ms |
16332 KB |
Output is correct |
19 |
Correct |
24 ms |
16612 KB |
Output is correct |
20 |
Correct |
17 ms |
16384 KB |
Output is correct |
21 |
Correct |
19 ms |
16368 KB |
Output is correct |
22 |
Correct |
17 ms |
16500 KB |
Output is correct |
23 |
Correct |
15 ms |
16464 KB |
Output is correct |
24 |
Correct |
19 ms |
16588 KB |
Output is correct |
25 |
Correct |
16 ms |
16416 KB |
Output is correct |
26 |
Correct |
16 ms |
16228 KB |
Output is correct |
27 |
Correct |
18 ms |
16716 KB |
Output is correct |
28 |
Correct |
19 ms |
16740 KB |
Output is correct |
29 |
Correct |
19 ms |
16728 KB |
Output is correct |
30 |
Correct |
19 ms |
16752 KB |
Output is correct |
31 |
Correct |
19 ms |
16752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
15948 KB |
Output is correct |
2 |
Correct |
9 ms |
15972 KB |
Output is correct |
3 |
Correct |
9 ms |
15968 KB |
Output is correct |
4 |
Correct |
9 ms |
15980 KB |
Output is correct |
5 |
Correct |
20 ms |
16668 KB |
Output is correct |
6 |
Correct |
18 ms |
16460 KB |
Output is correct |
7 |
Correct |
18 ms |
16496 KB |
Output is correct |
8 |
Correct |
21 ms |
16588 KB |
Output is correct |
9 |
Correct |
18 ms |
16460 KB |
Output is correct |
10 |
Correct |
17 ms |
16472 KB |
Output is correct |
11 |
Correct |
17 ms |
16480 KB |
Output is correct |
12 |
Correct |
19 ms |
16260 KB |
Output is correct |
13 |
Correct |
17 ms |
16332 KB |
Output is correct |
14 |
Correct |
18 ms |
16352 KB |
Output is correct |
15 |
Correct |
17 ms |
16244 KB |
Output is correct |
16 |
Correct |
23 ms |
16512 KB |
Output is correct |
17 |
Correct |
19 ms |
16460 KB |
Output is correct |
18 |
Correct |
16 ms |
16332 KB |
Output is correct |
19 |
Correct |
24 ms |
16612 KB |
Output is correct |
20 |
Correct |
17 ms |
16384 KB |
Output is correct |
21 |
Correct |
19 ms |
16368 KB |
Output is correct |
22 |
Correct |
17 ms |
16500 KB |
Output is correct |
23 |
Correct |
15 ms |
16464 KB |
Output is correct |
24 |
Correct |
19 ms |
16588 KB |
Output is correct |
25 |
Correct |
16 ms |
16416 KB |
Output is correct |
26 |
Correct |
16 ms |
16228 KB |
Output is correct |
27 |
Correct |
18 ms |
16716 KB |
Output is correct |
28 |
Correct |
19 ms |
16740 KB |
Output is correct |
29 |
Correct |
19 ms |
16728 KB |
Output is correct |
30 |
Correct |
19 ms |
16752 KB |
Output is correct |
31 |
Correct |
19 ms |
16752 KB |
Output is correct |
32 |
Correct |
21 ms |
16716 KB |
Output is correct |
33 |
Correct |
883 ms |
41368 KB |
Output is correct |
34 |
Correct |
538 ms |
38384 KB |
Output is correct |
35 |
Correct |
793 ms |
42740 KB |
Output is correct |
36 |
Correct |
573 ms |
38388 KB |
Output is correct |
37 |
Correct |
434 ms |
36420 KB |
Output is correct |
38 |
Correct |
435 ms |
33620 KB |
Output is correct |
39 |
Correct |
417 ms |
30636 KB |
Output is correct |
40 |
Correct |
399 ms |
30444 KB |
Output is correct |
41 |
Correct |
308 ms |
30392 KB |
Output is correct |
42 |
Correct |
428 ms |
30604 KB |
Output is correct |
43 |
Correct |
430 ms |
30652 KB |
Output is correct |
44 |
Correct |
775 ms |
42420 KB |
Output is correct |
45 |
Correct |
475 ms |
37572 KB |
Output is correct |
46 |
Correct |
298 ms |
33860 KB |
Output is correct |
47 |
Correct |
550 ms |
41304 KB |
Output is correct |
48 |
Correct |
356 ms |
33484 KB |
Output is correct |
49 |
Correct |
298 ms |
33432 KB |
Output is correct |
50 |
Correct |
429 ms |
36916 KB |
Output is correct |
51 |
Correct |
286 ms |
36664 KB |
Output is correct |
52 |
Correct |
562 ms |
42156 KB |
Output is correct |
53 |
Correct |
377 ms |
34076 KB |
Output is correct |
54 |
Correct |
299 ms |
30720 KB |
Output is correct |
55 |
Correct |
548 ms |
44912 KB |
Output is correct |
56 |
Correct |
513 ms |
47552 KB |
Output is correct |
57 |
Correct |
563 ms |
49864 KB |
Output is correct |
58 |
Correct |
705 ms |
48604 KB |
Output is correct |
59 |
Correct |
618 ms |
48536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
15948 KB |
Output is correct |
2 |
Correct |
9 ms |
15972 KB |
Output is correct |
3 |
Correct |
9 ms |
15968 KB |
Output is correct |
4 |
Correct |
9 ms |
15980 KB |
Output is correct |
5 |
Correct |
20 ms |
16668 KB |
Output is correct |
6 |
Correct |
18 ms |
16460 KB |
Output is correct |
7 |
Correct |
18 ms |
16496 KB |
Output is correct |
8 |
Correct |
21 ms |
16588 KB |
Output is correct |
9 |
Correct |
18 ms |
16460 KB |
Output is correct |
10 |
Correct |
17 ms |
16472 KB |
Output is correct |
11 |
Correct |
17 ms |
16480 KB |
Output is correct |
12 |
Correct |
19 ms |
16260 KB |
Output is correct |
13 |
Correct |
17 ms |
16332 KB |
Output is correct |
14 |
Correct |
18 ms |
16352 KB |
Output is correct |
15 |
Correct |
17 ms |
16244 KB |
Output is correct |
16 |
Correct |
23 ms |
16512 KB |
Output is correct |
17 |
Correct |
19 ms |
16460 KB |
Output is correct |
18 |
Correct |
16 ms |
16332 KB |
Output is correct |
19 |
Correct |
24 ms |
16612 KB |
Output is correct |
20 |
Correct |
17 ms |
16384 KB |
Output is correct |
21 |
Correct |
19 ms |
16368 KB |
Output is correct |
22 |
Correct |
17 ms |
16500 KB |
Output is correct |
23 |
Correct |
15 ms |
16464 KB |
Output is correct |
24 |
Correct |
19 ms |
16588 KB |
Output is correct |
25 |
Correct |
16 ms |
16416 KB |
Output is correct |
26 |
Correct |
16 ms |
16228 KB |
Output is correct |
27 |
Correct |
18 ms |
16716 KB |
Output is correct |
28 |
Correct |
19 ms |
16740 KB |
Output is correct |
29 |
Correct |
19 ms |
16728 KB |
Output is correct |
30 |
Correct |
19 ms |
16752 KB |
Output is correct |
31 |
Correct |
19 ms |
16752 KB |
Output is correct |
32 |
Correct |
21 ms |
16716 KB |
Output is correct |
33 |
Correct |
883 ms |
41368 KB |
Output is correct |
34 |
Correct |
538 ms |
38384 KB |
Output is correct |
35 |
Correct |
793 ms |
42740 KB |
Output is correct |
36 |
Correct |
573 ms |
38388 KB |
Output is correct |
37 |
Correct |
434 ms |
36420 KB |
Output is correct |
38 |
Correct |
435 ms |
33620 KB |
Output is correct |
39 |
Correct |
417 ms |
30636 KB |
Output is correct |
40 |
Correct |
399 ms |
30444 KB |
Output is correct |
41 |
Correct |
308 ms |
30392 KB |
Output is correct |
42 |
Correct |
428 ms |
30604 KB |
Output is correct |
43 |
Correct |
430 ms |
30652 KB |
Output is correct |
44 |
Correct |
775 ms |
42420 KB |
Output is correct |
45 |
Correct |
475 ms |
37572 KB |
Output is correct |
46 |
Correct |
298 ms |
33860 KB |
Output is correct |
47 |
Correct |
550 ms |
41304 KB |
Output is correct |
48 |
Correct |
356 ms |
33484 KB |
Output is correct |
49 |
Correct |
298 ms |
33432 KB |
Output is correct |
50 |
Correct |
429 ms |
36916 KB |
Output is correct |
51 |
Correct |
286 ms |
36664 KB |
Output is correct |
52 |
Correct |
562 ms |
42156 KB |
Output is correct |
53 |
Correct |
377 ms |
34076 KB |
Output is correct |
54 |
Correct |
299 ms |
30720 KB |
Output is correct |
55 |
Correct |
548 ms |
44912 KB |
Output is correct |
56 |
Correct |
513 ms |
47552 KB |
Output is correct |
57 |
Correct |
563 ms |
49864 KB |
Output is correct |
58 |
Correct |
705 ms |
48604 KB |
Output is correct |
59 |
Correct |
618 ms |
48536 KB |
Output is correct |
60 |
Correct |
9 ms |
15948 KB |
Output is correct |
61 |
Correct |
9 ms |
15972 KB |
Output is correct |
62 |
Correct |
9 ms |
15904 KB |
Output is correct |
63 |
Correct |
9 ms |
15972 KB |
Output is correct |
64 |
Correct |
714 ms |
37532 KB |
Output is correct |
65 |
Correct |
626 ms |
37124 KB |
Output is correct |
66 |
Correct |
753 ms |
37896 KB |
Output is correct |
67 |
Correct |
588 ms |
36952 KB |
Output is correct |
68 |
Correct |
462 ms |
35356 KB |
Output is correct |
69 |
Correct |
417 ms |
32952 KB |
Output is correct |
70 |
Correct |
659 ms |
55500 KB |
Output is correct |
71 |
Correct |
384 ms |
31528 KB |
Output is correct |
72 |
Correct |
664 ms |
56244 KB |
Output is correct |
73 |
Correct |
446 ms |
31616 KB |
Output is correct |
74 |
Correct |
652 ms |
46436 KB |
Output is correct |
75 |
Correct |
440 ms |
34300 KB |
Output is correct |
76 |
Correct |
386 ms |
34248 KB |
Output is correct |
77 |
Correct |
594 ms |
46968 KB |
Output is correct |
78 |
Correct |
377 ms |
34760 KB |
Output is correct |
79 |
Correct |
734 ms |
46700 KB |
Output is correct |
80 |
Correct |
486 ms |
35024 KB |
Output is correct |
81 |
Correct |
389 ms |
33860 KB |
Output is correct |
82 |
Correct |
722 ms |
56524 KB |
Output is correct |
83 |
Correct |
343 ms |
67972 KB |
Output is correct |
84 |
Correct |
548 ms |
47588 KB |
Output is correct |
85 |
Correct |
638 ms |
47548 KB |
Output is correct |
86 |
Correct |
507 ms |
41320 KB |
Output is correct |
87 |
Correct |
573 ms |
47624 KB |
Output is correct |
88 |
Correct |
554 ms |
47548 KB |
Output is correct |