#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2e5;
int n;
bool vis[MAX_N + 1];
int parent[MAX_N + 1], height[MAX_N + 1], cost[MAX_N + 1], sc[MAX_N + 1];
vector <int> edges[MAX_N + 1], children[MAX_N + 1];
map <int, long long> inc[MAX_N + 1];
void computeDP( int u ) {
sc[u] = cost[u];
for ( int v: children[u] ) {
computeDP( v );
sc[u] += sc[v];
}
int maxSize = 0, h = -1;
for ( int v: children[u] ) {
if ( inc[v].size() > maxSize ) {
maxSize = inc[v].size();
h = v;
}
}
inc[u][-1] = 0;
if ( h == -1 ) {
inc[u][0] -= cost[u];
inc[u][height[u] + 1] += cost[u];
} else {
swap( inc[u], inc[h] );
for ( int v: children[u] ) {
if ( v == h )
continue;
for ( auto x: inc[v] )
inc[u][x.first] += x.second;
inc[v].clear();
}
auto p = inc[u].lower_bound( height[u] );
if ( p != inc[u].begin() ) {
p--;
long long c = inc[u][height[u]];
vector<pair<int, long long>> pos;
pos.push_back( { height[u], 0 } );
while ( p->first != -1 && c <= cost[u] ) {
pos.push_back( { p->first, c } );
c += p->second;
p--;
}
inc[u][pos.back().first] += pos.back().second - cost[u];
for ( int i = pos.size() - 2; i >= 0; i-- )
inc[u].erase( pos[i].first );
inc[u][height[u] + 1] += cost[u];
}
}
inc[u][-1] = 0;
}
vector<int> st;
vector<vector<int>> cycles;
bool onCycle[MAX_N + 1];
int main() {
long long c = 0;
cin >> n;
for ( int v = 1; v <= n; v++ ) {
cin >> parent[v] >> height[v] >> cost[v];
edges[parent[v]].push_back( v );
c += cost[v];
}
for ( int v = 1; v <= n; v++ ) {
if ( vis[v] || edges[v].size() > 0 )
continue;
vector<int> vert;
int u = v;
while ( !vis[u] ) {
vert.push_back( u );
vis[u] = true;
u = parent[u];
}
int i = vert.size() - 1;
vector<int> cycle;
while ( i >= 0 && vert[i] != u ) {
cycle.push_back( vert[i] );
i--;
}
cycle.push_back( u );
if ( i >= 0 )
cycles.push_back( cycle );
}
for ( int v = 1; v <= n; v++ ) {
if ( vis[v] )
continue;
vector<int> vert;
int u = v;
while ( !vis[u] ) {
vert.push_back( u );
vis[u] = true;
u = parent[u];
}
int i = vert.size() - 1;
vector<int> cycle;
while ( i >= 0 && vert[i] != u ) {
cycle.push_back( vert[i] );
i--;
}
cycle.push_back( u );
if ( i >= 0 )
cycles.push_back( cycle );
}
for ( int i = 0; i < cycles.size(); i++ ) {
for ( int v: cycles[i] )
onCycle[v] = true;
}
for ( int v = 1; v <= n; v++ ) {
if ( !onCycle[v] )
children[parent[v]].push_back( v );
}
for ( int v = 1; v <= n; v++ ) {
if ( onCycle[v] ) {
for ( int u: children[v] )
computeDP( u );
}
}
long long ans = c;
for ( vector<int> cycle: cycles ) {
map<int, long long> upd;
for ( int v: cycle ) {
upd[height[v]] -= cost[v];
upd[height[v] + 1] += cost[v];
for ( int u: children[v] ) {
for ( auto p: inc[u] )
upd[p.first] += p.second;
}
}
long long minS = LONG_LONG_MAX, s = 0;
for ( auto p: upd ) {
s += p.second;
if ( s < minS )
minS = s;
}
ans += minS;
}
cout << ans;
return 0;
}
Compilation message
worst_reporter2.cpp: In function 'void computeDP(int)':
worst_reporter2.cpp:21:28: warning: comparison of integer expressions of different signedness: 'std::map<int, long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
21 | if ( inc[v].size() > maxSize ) {
| ~~~~~~~~~~~~~~^~~~~~~~~
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:116:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
116 | for ( int i = 0; i < cycles.size(); i++ ) {
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
20828 KB |
Output is correct |
2 |
Correct |
5 ms |
20936 KB |
Output is correct |
3 |
Correct |
4 ms |
20828 KB |
Output is correct |
4 |
Correct |
5 ms |
20828 KB |
Output is correct |
5 |
Correct |
12 ms |
21748 KB |
Output is correct |
6 |
Correct |
11 ms |
21340 KB |
Output is correct |
7 |
Correct |
10 ms |
21340 KB |
Output is correct |
8 |
Correct |
12 ms |
21852 KB |
Output is correct |
9 |
Correct |
11 ms |
21336 KB |
Output is correct |
10 |
Correct |
11 ms |
21340 KB |
Output is correct |
11 |
Correct |
10 ms |
21440 KB |
Output is correct |
12 |
Correct |
12 ms |
22624 KB |
Output is correct |
13 |
Correct |
12 ms |
22620 KB |
Output is correct |
14 |
Correct |
11 ms |
22108 KB |
Output is correct |
15 |
Correct |
11 ms |
22164 KB |
Output is correct |
16 |
Correct |
12 ms |
21852 KB |
Output is correct |
17 |
Correct |
11 ms |
21340 KB |
Output is correct |
18 |
Correct |
9 ms |
21340 KB |
Output is correct |
19 |
Correct |
11 ms |
22108 KB |
Output is correct |
20 |
Correct |
10 ms |
21852 KB |
Output is correct |
21 |
Correct |
9 ms |
21852 KB |
Output is correct |
22 |
Correct |
12 ms |
22364 KB |
Output is correct |
23 |
Correct |
10 ms |
21912 KB |
Output is correct |
24 |
Correct |
13 ms |
22108 KB |
Output is correct |
25 |
Correct |
10 ms |
22000 KB |
Output is correct |
26 |
Correct |
10 ms |
22624 KB |
Output is correct |
27 |
Correct |
11 ms |
22228 KB |
Output is correct |
28 |
Correct |
11 ms |
22364 KB |
Output is correct |
29 |
Correct |
13 ms |
22616 KB |
Output is correct |
30 |
Correct |
11 ms |
22620 KB |
Output is correct |
31 |
Correct |
12 ms |
22620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
20828 KB |
Output is correct |
2 |
Correct |
5 ms |
20936 KB |
Output is correct |
3 |
Correct |
4 ms |
20828 KB |
Output is correct |
4 |
Correct |
5 ms |
20828 KB |
Output is correct |
5 |
Correct |
12 ms |
21748 KB |
Output is correct |
6 |
Correct |
11 ms |
21340 KB |
Output is correct |
7 |
Correct |
10 ms |
21340 KB |
Output is correct |
8 |
Correct |
12 ms |
21852 KB |
Output is correct |
9 |
Correct |
11 ms |
21336 KB |
Output is correct |
10 |
Correct |
11 ms |
21340 KB |
Output is correct |
11 |
Correct |
10 ms |
21440 KB |
Output is correct |
12 |
Correct |
12 ms |
22624 KB |
Output is correct |
13 |
Correct |
12 ms |
22620 KB |
Output is correct |
14 |
Correct |
11 ms |
22108 KB |
Output is correct |
15 |
Correct |
11 ms |
22164 KB |
Output is correct |
16 |
Correct |
12 ms |
21852 KB |
Output is correct |
17 |
Correct |
11 ms |
21340 KB |
Output is correct |
18 |
Correct |
9 ms |
21340 KB |
Output is correct |
19 |
Correct |
11 ms |
22108 KB |
Output is correct |
20 |
Correct |
10 ms |
21852 KB |
Output is correct |
21 |
Correct |
9 ms |
21852 KB |
Output is correct |
22 |
Correct |
12 ms |
22364 KB |
Output is correct |
23 |
Correct |
10 ms |
21912 KB |
Output is correct |
24 |
Correct |
13 ms |
22108 KB |
Output is correct |
25 |
Correct |
10 ms |
22000 KB |
Output is correct |
26 |
Correct |
10 ms |
22624 KB |
Output is correct |
27 |
Correct |
11 ms |
22228 KB |
Output is correct |
28 |
Correct |
11 ms |
22364 KB |
Output is correct |
29 |
Correct |
13 ms |
22616 KB |
Output is correct |
30 |
Correct |
11 ms |
22620 KB |
Output is correct |
31 |
Correct |
12 ms |
22620 KB |
Output is correct |
32 |
Correct |
12 ms |
21848 KB |
Output is correct |
33 |
Correct |
415 ms |
57836 KB |
Output is correct |
34 |
Correct |
326 ms |
38576 KB |
Output is correct |
35 |
Correct |
417 ms |
57628 KB |
Output is correct |
36 |
Correct |
318 ms |
38388 KB |
Output is correct |
37 |
Correct |
286 ms |
38272 KB |
Output is correct |
38 |
Correct |
250 ms |
38120 KB |
Output is correct |
39 |
Correct |
327 ms |
88232 KB |
Output is correct |
40 |
Correct |
302 ms |
88088 KB |
Output is correct |
41 |
Correct |
241 ms |
88232 KB |
Output is correct |
42 |
Correct |
314 ms |
69636 KB |
Output is correct |
43 |
Correct |
293 ms |
69148 KB |
Output is correct |
44 |
Correct |
395 ms |
56620 KB |
Output is correct |
45 |
Correct |
289 ms |
38272 KB |
Output is correct |
46 |
Correct |
215 ms |
37716 KB |
Output is correct |
47 |
Correct |
411 ms |
70828 KB |
Output is correct |
48 |
Correct |
260 ms |
56736 KB |
Output is correct |
49 |
Correct |
222 ms |
53792 KB |
Output is correct |
50 |
Correct |
294 ms |
74156 KB |
Output is correct |
51 |
Correct |
203 ms |
62036 KB |
Output is correct |
52 |
Correct |
447 ms |
68816 KB |
Output is correct |
53 |
Correct |
262 ms |
67192 KB |
Output is correct |
54 |
Correct |
247 ms |
85032 KB |
Output is correct |
55 |
Correct |
318 ms |
72356 KB |
Output is correct |
56 |
Correct |
311 ms |
89188 KB |
Output is correct |
57 |
Correct |
325 ms |
96932 KB |
Output is correct |
58 |
Correct |
380 ms |
91556 KB |
Output is correct |
59 |
Correct |
372 ms |
91236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
20828 KB |
Output is correct |
2 |
Correct |
5 ms |
20936 KB |
Output is correct |
3 |
Correct |
4 ms |
20828 KB |
Output is correct |
4 |
Correct |
5 ms |
20828 KB |
Output is correct |
5 |
Correct |
12 ms |
21748 KB |
Output is correct |
6 |
Correct |
11 ms |
21340 KB |
Output is correct |
7 |
Correct |
10 ms |
21340 KB |
Output is correct |
8 |
Correct |
12 ms |
21852 KB |
Output is correct |
9 |
Correct |
11 ms |
21336 KB |
Output is correct |
10 |
Correct |
11 ms |
21340 KB |
Output is correct |
11 |
Correct |
10 ms |
21440 KB |
Output is correct |
12 |
Correct |
12 ms |
22624 KB |
Output is correct |
13 |
Correct |
12 ms |
22620 KB |
Output is correct |
14 |
Correct |
11 ms |
22108 KB |
Output is correct |
15 |
Correct |
11 ms |
22164 KB |
Output is correct |
16 |
Correct |
12 ms |
21852 KB |
Output is correct |
17 |
Correct |
11 ms |
21340 KB |
Output is correct |
18 |
Correct |
9 ms |
21340 KB |
Output is correct |
19 |
Correct |
11 ms |
22108 KB |
Output is correct |
20 |
Correct |
10 ms |
21852 KB |
Output is correct |
21 |
Correct |
9 ms |
21852 KB |
Output is correct |
22 |
Correct |
12 ms |
22364 KB |
Output is correct |
23 |
Correct |
10 ms |
21912 KB |
Output is correct |
24 |
Correct |
13 ms |
22108 KB |
Output is correct |
25 |
Correct |
10 ms |
22000 KB |
Output is correct |
26 |
Correct |
10 ms |
22624 KB |
Output is correct |
27 |
Correct |
11 ms |
22228 KB |
Output is correct |
28 |
Correct |
11 ms |
22364 KB |
Output is correct |
29 |
Correct |
13 ms |
22616 KB |
Output is correct |
30 |
Correct |
11 ms |
22620 KB |
Output is correct |
31 |
Correct |
12 ms |
22620 KB |
Output is correct |
32 |
Correct |
12 ms |
21848 KB |
Output is correct |
33 |
Correct |
415 ms |
57836 KB |
Output is correct |
34 |
Correct |
326 ms |
38576 KB |
Output is correct |
35 |
Correct |
417 ms |
57628 KB |
Output is correct |
36 |
Correct |
318 ms |
38388 KB |
Output is correct |
37 |
Correct |
286 ms |
38272 KB |
Output is correct |
38 |
Correct |
250 ms |
38120 KB |
Output is correct |
39 |
Correct |
327 ms |
88232 KB |
Output is correct |
40 |
Correct |
302 ms |
88088 KB |
Output is correct |
41 |
Correct |
241 ms |
88232 KB |
Output is correct |
42 |
Correct |
314 ms |
69636 KB |
Output is correct |
43 |
Correct |
293 ms |
69148 KB |
Output is correct |
44 |
Correct |
395 ms |
56620 KB |
Output is correct |
45 |
Correct |
289 ms |
38272 KB |
Output is correct |
46 |
Correct |
215 ms |
37716 KB |
Output is correct |
47 |
Correct |
411 ms |
70828 KB |
Output is correct |
48 |
Correct |
260 ms |
56736 KB |
Output is correct |
49 |
Correct |
222 ms |
53792 KB |
Output is correct |
50 |
Correct |
294 ms |
74156 KB |
Output is correct |
51 |
Correct |
203 ms |
62036 KB |
Output is correct |
52 |
Correct |
447 ms |
68816 KB |
Output is correct |
53 |
Correct |
262 ms |
67192 KB |
Output is correct |
54 |
Correct |
247 ms |
85032 KB |
Output is correct |
55 |
Correct |
318 ms |
72356 KB |
Output is correct |
56 |
Correct |
311 ms |
89188 KB |
Output is correct |
57 |
Correct |
325 ms |
96932 KB |
Output is correct |
58 |
Correct |
380 ms |
91556 KB |
Output is correct |
59 |
Correct |
372 ms |
91236 KB |
Output is correct |
60 |
Correct |
4 ms |
20828 KB |
Output is correct |
61 |
Correct |
4 ms |
20828 KB |
Output is correct |
62 |
Correct |
5 ms |
20824 KB |
Output is correct |
63 |
Correct |
6 ms |
20944 KB |
Output is correct |
64 |
Correct |
417 ms |
60036 KB |
Output is correct |
65 |
Correct |
343 ms |
44684 KB |
Output is correct |
66 |
Correct |
396 ms |
58852 KB |
Output is correct |
67 |
Correct |
351 ms |
44148 KB |
Output is correct |
68 |
Correct |
335 ms |
44144 KB |
Output is correct |
69 |
Correct |
278 ms |
43876 KB |
Output is correct |
70 |
Correct |
338 ms |
52688 KB |
Output is correct |
71 |
Correct |
206 ms |
35428 KB |
Output is correct |
72 |
Correct |
389 ms |
60812 KB |
Output is correct |
73 |
Correct |
217 ms |
36632 KB |
Output is correct |
74 |
Correct |
439 ms |
72460 KB |
Output is correct |
75 |
Correct |
302 ms |
53840 KB |
Output is correct |
76 |
Correct |
331 ms |
53716 KB |
Output is correct |
77 |
Correct |
358 ms |
70284 KB |
Output is correct |
78 |
Correct |
223 ms |
51396 KB |
Output is correct |
79 |
Correct |
381 ms |
60300 KB |
Output is correct |
80 |
Correct |
260 ms |
38716 KB |
Output is correct |
81 |
Correct |
232 ms |
37940 KB |
Output is correct |
82 |
Correct |
258 ms |
60704 KB |
Output is correct |
83 |
Correct |
206 ms |
46460 KB |
Output is correct |
84 |
Correct |
361 ms |
71896 KB |
Output is correct |
85 |
Correct |
371 ms |
71724 KB |
Output is correct |
86 |
Correct |
328 ms |
65560 KB |
Output is correct |
87 |
Correct |
403 ms |
71892 KB |
Output is correct |
88 |
Correct |
358 ms |
71848 KB |
Output is correct |