#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(X[u] == X[v]) return;
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();
X[v] = X[u];
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] ]++;
X[i] = new map<int, long long>;
}
for(int i = 1; i <= N; i++)
{
if(indegree[i] == 0) tbv.push(i);
}
//First two subtasks!!!!!
// indegree[1]--;
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
{
X[u] = X[max_child];
for(int v: children[u])
{
if(indegree[v] == 0)
{
merge(v, u);
}
}
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';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6476 KB |
Output is correct |
2 |
Correct |
4 ms |
6580 KB |
Output is correct |
3 |
Correct |
4 ms |
6476 KB |
Output is correct |
4 |
Correct |
4 ms |
6476 KB |
Output is correct |
5 |
Correct |
15 ms |
7624 KB |
Output is correct |
6 |
Correct |
12 ms |
7372 KB |
Output is correct |
7 |
Correct |
12 ms |
7360 KB |
Output is correct |
8 |
Correct |
16 ms |
7564 KB |
Output is correct |
9 |
Correct |
13 ms |
7500 KB |
Output is correct |
10 |
Correct |
14 ms |
7372 KB |
Output is correct |
11 |
Correct |
13 ms |
7496 KB |
Output is correct |
12 |
Correct |
13 ms |
7312 KB |
Output is correct |
13 |
Correct |
11 ms |
7240 KB |
Output is correct |
14 |
Correct |
13 ms |
7240 KB |
Output is correct |
15 |
Correct |
12 ms |
7308 KB |
Output is correct |
16 |
Correct |
16 ms |
7592 KB |
Output is correct |
17 |
Correct |
13 ms |
7432 KB |
Output is correct |
18 |
Correct |
11 ms |
7348 KB |
Output is correct |
19 |
Correct |
14 ms |
7568 KB |
Output is correct |
20 |
Correct |
12 ms |
7376 KB |
Output is correct |
21 |
Correct |
11 ms |
7352 KB |
Output is correct |
22 |
Correct |
11 ms |
7476 KB |
Output is correct |
23 |
Correct |
10 ms |
7372 KB |
Output is correct |
24 |
Correct |
14 ms |
7500 KB |
Output is correct |
25 |
Correct |
11 ms |
7408 KB |
Output is correct |
26 |
Correct |
11 ms |
7360 KB |
Output is correct |
27 |
Correct |
13 ms |
7568 KB |
Output is correct |
28 |
Correct |
17 ms |
7652 KB |
Output is correct |
29 |
Correct |
16 ms |
7700 KB |
Output is correct |
30 |
Correct |
17 ms |
7628 KB |
Output is correct |
31 |
Correct |
16 ms |
7720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6476 KB |
Output is correct |
2 |
Correct |
4 ms |
6580 KB |
Output is correct |
3 |
Correct |
4 ms |
6476 KB |
Output is correct |
4 |
Correct |
4 ms |
6476 KB |
Output is correct |
5 |
Correct |
15 ms |
7624 KB |
Output is correct |
6 |
Correct |
12 ms |
7372 KB |
Output is correct |
7 |
Correct |
12 ms |
7360 KB |
Output is correct |
8 |
Correct |
16 ms |
7564 KB |
Output is correct |
9 |
Correct |
13 ms |
7500 KB |
Output is correct |
10 |
Correct |
14 ms |
7372 KB |
Output is correct |
11 |
Correct |
13 ms |
7496 KB |
Output is correct |
12 |
Correct |
13 ms |
7312 KB |
Output is correct |
13 |
Correct |
11 ms |
7240 KB |
Output is correct |
14 |
Correct |
13 ms |
7240 KB |
Output is correct |
15 |
Correct |
12 ms |
7308 KB |
Output is correct |
16 |
Correct |
16 ms |
7592 KB |
Output is correct |
17 |
Correct |
13 ms |
7432 KB |
Output is correct |
18 |
Correct |
11 ms |
7348 KB |
Output is correct |
19 |
Correct |
14 ms |
7568 KB |
Output is correct |
20 |
Correct |
12 ms |
7376 KB |
Output is correct |
21 |
Correct |
11 ms |
7352 KB |
Output is correct |
22 |
Correct |
11 ms |
7476 KB |
Output is correct |
23 |
Correct |
10 ms |
7372 KB |
Output is correct |
24 |
Correct |
14 ms |
7500 KB |
Output is correct |
25 |
Correct |
11 ms |
7408 KB |
Output is correct |
26 |
Correct |
11 ms |
7360 KB |
Output is correct |
27 |
Correct |
13 ms |
7568 KB |
Output is correct |
28 |
Correct |
17 ms |
7652 KB |
Output is correct |
29 |
Correct |
16 ms |
7700 KB |
Output is correct |
30 |
Correct |
17 ms |
7628 KB |
Output is correct |
31 |
Correct |
16 ms |
7720 KB |
Output is correct |
32 |
Correct |
15 ms |
7616 KB |
Output is correct |
33 |
Correct |
832 ms |
41280 KB |
Output is correct |
34 |
Correct |
549 ms |
38456 KB |
Output is correct |
35 |
Correct |
844 ms |
42436 KB |
Output is correct |
36 |
Correct |
641 ms |
38340 KB |
Output is correct |
37 |
Correct |
431 ms |
36356 KB |
Output is correct |
38 |
Correct |
387 ms |
33644 KB |
Output is correct |
39 |
Correct |
417 ms |
30376 KB |
Output is correct |
40 |
Correct |
391 ms |
30388 KB |
Output is correct |
41 |
Correct |
296 ms |
30248 KB |
Output is correct |
42 |
Correct |
420 ms |
30464 KB |
Output is correct |
43 |
Correct |
394 ms |
30404 KB |
Output is correct |
44 |
Correct |
702 ms |
42344 KB |
Output is correct |
45 |
Correct |
489 ms |
37712 KB |
Output is correct |
46 |
Correct |
339 ms |
33580 KB |
Output is correct |
47 |
Correct |
560 ms |
41192 KB |
Output is correct |
48 |
Correct |
354 ms |
33348 KB |
Output is correct |
49 |
Correct |
297 ms |
33448 KB |
Output is correct |
50 |
Correct |
386 ms |
37652 KB |
Output is correct |
51 |
Correct |
296 ms |
37380 KB |
Output is correct |
52 |
Correct |
585 ms |
41924 KB |
Output is correct |
53 |
Correct |
346 ms |
33828 KB |
Output is correct |
54 |
Correct |
284 ms |
30200 KB |
Output is correct |
55 |
Correct |
537 ms |
45688 KB |
Output is correct |
56 |
Correct |
493 ms |
48404 KB |
Output is correct |
57 |
Correct |
506 ms |
50688 KB |
Output is correct |
58 |
Correct |
590 ms |
49044 KB |
Output is correct |
59 |
Correct |
645 ms |
49100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6476 KB |
Output is correct |
2 |
Correct |
4 ms |
6580 KB |
Output is correct |
3 |
Correct |
4 ms |
6476 KB |
Output is correct |
4 |
Correct |
4 ms |
6476 KB |
Output is correct |
5 |
Correct |
15 ms |
7624 KB |
Output is correct |
6 |
Correct |
12 ms |
7372 KB |
Output is correct |
7 |
Correct |
12 ms |
7360 KB |
Output is correct |
8 |
Correct |
16 ms |
7564 KB |
Output is correct |
9 |
Correct |
13 ms |
7500 KB |
Output is correct |
10 |
Correct |
14 ms |
7372 KB |
Output is correct |
11 |
Correct |
13 ms |
7496 KB |
Output is correct |
12 |
Correct |
13 ms |
7312 KB |
Output is correct |
13 |
Correct |
11 ms |
7240 KB |
Output is correct |
14 |
Correct |
13 ms |
7240 KB |
Output is correct |
15 |
Correct |
12 ms |
7308 KB |
Output is correct |
16 |
Correct |
16 ms |
7592 KB |
Output is correct |
17 |
Correct |
13 ms |
7432 KB |
Output is correct |
18 |
Correct |
11 ms |
7348 KB |
Output is correct |
19 |
Correct |
14 ms |
7568 KB |
Output is correct |
20 |
Correct |
12 ms |
7376 KB |
Output is correct |
21 |
Correct |
11 ms |
7352 KB |
Output is correct |
22 |
Correct |
11 ms |
7476 KB |
Output is correct |
23 |
Correct |
10 ms |
7372 KB |
Output is correct |
24 |
Correct |
14 ms |
7500 KB |
Output is correct |
25 |
Correct |
11 ms |
7408 KB |
Output is correct |
26 |
Correct |
11 ms |
7360 KB |
Output is correct |
27 |
Correct |
13 ms |
7568 KB |
Output is correct |
28 |
Correct |
17 ms |
7652 KB |
Output is correct |
29 |
Correct |
16 ms |
7700 KB |
Output is correct |
30 |
Correct |
17 ms |
7628 KB |
Output is correct |
31 |
Correct |
16 ms |
7720 KB |
Output is correct |
32 |
Correct |
15 ms |
7616 KB |
Output is correct |
33 |
Correct |
832 ms |
41280 KB |
Output is correct |
34 |
Correct |
549 ms |
38456 KB |
Output is correct |
35 |
Correct |
844 ms |
42436 KB |
Output is correct |
36 |
Correct |
641 ms |
38340 KB |
Output is correct |
37 |
Correct |
431 ms |
36356 KB |
Output is correct |
38 |
Correct |
387 ms |
33644 KB |
Output is correct |
39 |
Correct |
417 ms |
30376 KB |
Output is correct |
40 |
Correct |
391 ms |
30388 KB |
Output is correct |
41 |
Correct |
296 ms |
30248 KB |
Output is correct |
42 |
Correct |
420 ms |
30464 KB |
Output is correct |
43 |
Correct |
394 ms |
30404 KB |
Output is correct |
44 |
Correct |
702 ms |
42344 KB |
Output is correct |
45 |
Correct |
489 ms |
37712 KB |
Output is correct |
46 |
Correct |
339 ms |
33580 KB |
Output is correct |
47 |
Correct |
560 ms |
41192 KB |
Output is correct |
48 |
Correct |
354 ms |
33348 KB |
Output is correct |
49 |
Correct |
297 ms |
33448 KB |
Output is correct |
50 |
Correct |
386 ms |
37652 KB |
Output is correct |
51 |
Correct |
296 ms |
37380 KB |
Output is correct |
52 |
Correct |
585 ms |
41924 KB |
Output is correct |
53 |
Correct |
346 ms |
33828 KB |
Output is correct |
54 |
Correct |
284 ms |
30200 KB |
Output is correct |
55 |
Correct |
537 ms |
45688 KB |
Output is correct |
56 |
Correct |
493 ms |
48404 KB |
Output is correct |
57 |
Correct |
506 ms |
50688 KB |
Output is correct |
58 |
Correct |
590 ms |
49044 KB |
Output is correct |
59 |
Correct |
645 ms |
49100 KB |
Output is correct |
60 |
Correct |
4 ms |
6588 KB |
Output is correct |
61 |
Correct |
4 ms |
6584 KB |
Output is correct |
62 |
Correct |
4 ms |
6476 KB |
Output is correct |
63 |
Correct |
4 ms |
6604 KB |
Output is correct |
64 |
Correct |
690 ms |
37192 KB |
Output is correct |
65 |
Correct |
645 ms |
41720 KB |
Output is correct |
66 |
Correct |
669 ms |
42572 KB |
Output is correct |
67 |
Correct |
576 ms |
41816 KB |
Output is correct |
68 |
Correct |
479 ms |
39992 KB |
Output is correct |
69 |
Correct |
414 ms |
37668 KB |
Output is correct |
70 |
Correct |
594 ms |
60212 KB |
Output is correct |
71 |
Correct |
401 ms |
36064 KB |
Output is correct |
72 |
Correct |
663 ms |
60900 KB |
Output is correct |
73 |
Correct |
415 ms |
36344 KB |
Output is correct |
74 |
Correct |
700 ms |
51136 KB |
Output is correct |
75 |
Correct |
439 ms |
38980 KB |
Output is correct |
76 |
Correct |
385 ms |
38904 KB |
Output is correct |
77 |
Correct |
552 ms |
51660 KB |
Output is correct |
78 |
Correct |
358 ms |
39360 KB |
Output is correct |
79 |
Correct |
658 ms |
51440 KB |
Output is correct |
80 |
Correct |
471 ms |
39912 KB |
Output is correct |
81 |
Correct |
399 ms |
38596 KB |
Output is correct |
82 |
Correct |
711 ms |
61224 KB |
Output is correct |
83 |
Correct |
334 ms |
72792 KB |
Output is correct |
84 |
Correct |
550 ms |
52296 KB |
Output is correct |
85 |
Correct |
545 ms |
52296 KB |
Output is correct |
86 |
Correct |
511 ms |
45908 KB |
Output is correct |
87 |
Correct |
541 ms |
52276 KB |
Output is correct |
88 |
Correct |
541 ms |
52200 KB |
Output is correct |