#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l)==r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
const int MAX_N = 300010, inf = 1e9 + 10;
int n;
vector<int> pa, h, c;
vector<int> edge[MAX_N];
// maintain diff
map<int, ll> dp[MAX_N];
void add_pref(int i, int key, ll val) {
if (val == 0) return;
dp[i][key] += val;
}
// put dp value into a
void merge(int a, int b) {
if (dp[b].size() > dp[a].size()) swap(dp[a], dp[b]);
// do something
for (auto [key, val] : dp[b])
add_pref(a, key, val);
dp[b].clear();
}
ll qry(map<int,ll>&dp, int x) {
auto it = dp.lower_bound(x);
if (it == end(dp)) return 0;
return it->second;
}
void dfs(int x) {
for (int u : edge[x])
dfs(u), merge(x, u);
dp[x][ h[x] ] += c[x];
ll v = c[x];
auto it = dp[x].find(h[x]);
while (it != begin(dp[x])) {
ll &d = prev(it)->second;
if (d <= v) {
v -= d;
dp[x].erase(prev(it));
}
else {
d -= v;
break;
}
}
}
ll cal(vector<int> ring) {
dp[n].clear();
for (int x : ring) for (int u : edge[x]) {
dfs(u);
merge(n, u);
}
ll ret = 0;
vector<pair<int,ll>> val(AI(dp[n]));
val.pb(inf, 0);
for (int i = (int) val.size() - 1;i > 0;--i)
val[i-1].second += val[i].second;
map<int, ll> sum;
for (int x : ring)
sum[ h[x] ] += c[x];
sum[-1] = 0;
for (auto [k, v] : sum)
chmax(ret, lower_bound(AI(val), make_pair(k, -1ll))->second + v);
return ret;
}
ll min_change_val(int n, vector<int> a, vector<int> h, vector<int> c) {
::n = n, pa = a, ::h = h, ::c = c;
ll res = 0;
vector<int> vis(n), rid(n, -1), on_ring(n);
int cnt_ring = 0;
for (int i = 0;i < n;++i) if (!vis[i]) {
vector<int> stk;
for (int x = i;!vis[x];x = pa[x]) {
stk.pb(x);
vis[x] = true;
}
DE(i, "stk");
debug(AI(stk));
if (rid[ pa[stk.back()] ] == -1) {
int s = pa[ stk.back() ];
while (stk.size()) {
int x = stk.back(); stk.pop_back();
on_ring[x] = true;
rid[x] = cnt_ring;
if (x == s) break;
}
++cnt_ring;
}
while (stk.size()) {
int x = stk.back(); stk.pop_back();
rid[x] = rid[ pa[x] ];
}
}
for (int i = 0;i < n;++i) DE(i, rid[i], on_ring[i]);
for (int i = 0;i < n;++i) if (!on_ring[i] || !on_ring[ pa[i] ]) {
DE(i, pa[i]);
edge[ pa[i] ].pb(i);
}
vector<vector<int>> ring(cnt_ring);
for (int i = 0;i < n;++i) if (on_ring[i])
ring[ rid[i] ].pb(i);
for (int i = 0;i < cnt_ring;++i) {
debug(AI(ring[i]));
res += cal(ring[i]);
}
return accumulate(AI(c), 0ll) - res;
}
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
vector<int> a(n), h(n), c(n);
for (int i = 0;i < n;++i)
cin >> a[i] >> h[i] >> c[i], --a[i];
cout << min_change_val(n, a, h, c) << '\n';
}
Compilation message
worst_reporter2.cpp: In function 'll min_change_val(int, std::vector<int>, std::vector<int>, std::vector<int>)':
worst_reporter2.cpp:14:17: warning: statement has no effect [-Wunused-value]
14 | #define DE(...) 0
| ^
worst_reporter2.cpp:107:3: note: in expansion of macro 'DE'
107 | DE(i, "stk");
| ^~
worst_reporter2.cpp:15:20: warning: statement has no effect [-Wunused-value]
15 | #define debug(...) 0
| ^
worst_reporter2.cpp:108:3: note: in expansion of macro 'debug'
108 | debug(AI(stk));
| ^~~~~
worst_reporter2.cpp:14:17: warning: statement has no effect [-Wunused-value]
14 | #define DE(...) 0
| ^
worst_reporter2.cpp:124:28: note: in expansion of macro 'DE'
124 | for (int i = 0;i < n;++i) DE(i, rid[i], on_ring[i]);
| ^~
worst_reporter2.cpp:14:17: warning: statement has no effect [-Wunused-value]
14 | #define DE(...) 0
| ^
worst_reporter2.cpp:127:3: note: in expansion of macro 'DE'
127 | DE(i, pa[i]);
| ^~
worst_reporter2.cpp:15:20: warning: statement has no effect [-Wunused-value]
15 | #define debug(...) 0
| ^
worst_reporter2.cpp:136:3: note: in expansion of macro 'debug'
136 | debug(AI(ring[i]));
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
21324 KB |
Output is correct |
2 |
Correct |
15 ms |
21452 KB |
Output is correct |
3 |
Correct |
15 ms |
21412 KB |
Output is correct |
4 |
Correct |
17 ms |
21452 KB |
Output is correct |
5 |
Correct |
24 ms |
22256 KB |
Output is correct |
6 |
Correct |
22 ms |
21908 KB |
Output is correct |
7 |
Correct |
19 ms |
21892 KB |
Output is correct |
8 |
Correct |
23 ms |
22228 KB |
Output is correct |
9 |
Correct |
19 ms |
21888 KB |
Output is correct |
10 |
Correct |
22 ms |
21836 KB |
Output is correct |
11 |
Correct |
18 ms |
21820 KB |
Output is correct |
12 |
Correct |
24 ms |
22348 KB |
Output is correct |
13 |
Correct |
17 ms |
22356 KB |
Output is correct |
14 |
Correct |
27 ms |
22076 KB |
Output is correct |
15 |
Correct |
24 ms |
22104 KB |
Output is correct |
16 |
Correct |
22 ms |
22228 KB |
Output is correct |
17 |
Correct |
23 ms |
21900 KB |
Output is correct |
18 |
Correct |
20 ms |
21836 KB |
Output is correct |
19 |
Correct |
25 ms |
22256 KB |
Output is correct |
20 |
Correct |
26 ms |
21976 KB |
Output is correct |
21 |
Correct |
20 ms |
21964 KB |
Output is correct |
22 |
Correct |
19 ms |
22224 KB |
Output is correct |
23 |
Correct |
21 ms |
21844 KB |
Output is correct |
24 |
Correct |
20 ms |
22356 KB |
Output is correct |
25 |
Correct |
20 ms |
22008 KB |
Output is correct |
26 |
Correct |
19 ms |
22348 KB |
Output is correct |
27 |
Correct |
21 ms |
22360 KB |
Output is correct |
28 |
Correct |
20 ms |
22404 KB |
Output is correct |
29 |
Correct |
22 ms |
22608 KB |
Output is correct |
30 |
Correct |
20 ms |
22680 KB |
Output is correct |
31 |
Correct |
22 ms |
22604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
21324 KB |
Output is correct |
2 |
Correct |
15 ms |
21452 KB |
Output is correct |
3 |
Correct |
15 ms |
21412 KB |
Output is correct |
4 |
Correct |
17 ms |
21452 KB |
Output is correct |
5 |
Correct |
24 ms |
22256 KB |
Output is correct |
6 |
Correct |
22 ms |
21908 KB |
Output is correct |
7 |
Correct |
19 ms |
21892 KB |
Output is correct |
8 |
Correct |
23 ms |
22228 KB |
Output is correct |
9 |
Correct |
19 ms |
21888 KB |
Output is correct |
10 |
Correct |
22 ms |
21836 KB |
Output is correct |
11 |
Correct |
18 ms |
21820 KB |
Output is correct |
12 |
Correct |
24 ms |
22348 KB |
Output is correct |
13 |
Correct |
17 ms |
22356 KB |
Output is correct |
14 |
Correct |
27 ms |
22076 KB |
Output is correct |
15 |
Correct |
24 ms |
22104 KB |
Output is correct |
16 |
Correct |
22 ms |
22228 KB |
Output is correct |
17 |
Correct |
23 ms |
21900 KB |
Output is correct |
18 |
Correct |
20 ms |
21836 KB |
Output is correct |
19 |
Correct |
25 ms |
22256 KB |
Output is correct |
20 |
Correct |
26 ms |
21976 KB |
Output is correct |
21 |
Correct |
20 ms |
21964 KB |
Output is correct |
22 |
Correct |
19 ms |
22224 KB |
Output is correct |
23 |
Correct |
21 ms |
21844 KB |
Output is correct |
24 |
Correct |
20 ms |
22356 KB |
Output is correct |
25 |
Correct |
20 ms |
22008 KB |
Output is correct |
26 |
Correct |
19 ms |
22348 KB |
Output is correct |
27 |
Correct |
21 ms |
22360 KB |
Output is correct |
28 |
Correct |
20 ms |
22404 KB |
Output is correct |
29 |
Correct |
22 ms |
22608 KB |
Output is correct |
30 |
Correct |
20 ms |
22680 KB |
Output is correct |
31 |
Correct |
22 ms |
22604 KB |
Output is correct |
32 |
Correct |
26 ms |
22232 KB |
Output is correct |
33 |
Correct |
394 ms |
56216 KB |
Output is correct |
34 |
Correct |
350 ms |
39436 KB |
Output is correct |
35 |
Correct |
398 ms |
54232 KB |
Output is correct |
36 |
Correct |
329 ms |
39444 KB |
Output is correct |
37 |
Correct |
224 ms |
39156 KB |
Output is correct |
38 |
Correct |
235 ms |
38852 KB |
Output is correct |
39 |
Correct |
179 ms |
57964 KB |
Output is correct |
40 |
Correct |
189 ms |
57880 KB |
Output is correct |
41 |
Correct |
150 ms |
57808 KB |
Output is correct |
42 |
Correct |
174 ms |
50220 KB |
Output is correct |
43 |
Correct |
181 ms |
50156 KB |
Output is correct |
44 |
Correct |
438 ms |
55848 KB |
Output is correct |
45 |
Correct |
282 ms |
39404 KB |
Output is correct |
46 |
Correct |
172 ms |
38984 KB |
Output is correct |
47 |
Correct |
305 ms |
57220 KB |
Output is correct |
48 |
Correct |
195 ms |
46832 KB |
Output is correct |
49 |
Correct |
156 ms |
46840 KB |
Output is correct |
50 |
Correct |
372 ms |
54712 KB |
Output is correct |
51 |
Correct |
135 ms |
36196 KB |
Output is correct |
52 |
Correct |
289 ms |
57996 KB |
Output is correct |
53 |
Correct |
162 ms |
47464 KB |
Output is correct |
54 |
Correct |
145 ms |
58132 KB |
Output is correct |
55 |
Correct |
237 ms |
60012 KB |
Output is correct |
56 |
Correct |
258 ms |
66184 KB |
Output is correct |
57 |
Correct |
265 ms |
69612 KB |
Output is correct |
58 |
Correct |
266 ms |
71272 KB |
Output is correct |
59 |
Correct |
275 ms |
71264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
21324 KB |
Output is correct |
2 |
Correct |
15 ms |
21452 KB |
Output is correct |
3 |
Correct |
15 ms |
21412 KB |
Output is correct |
4 |
Correct |
17 ms |
21452 KB |
Output is correct |
5 |
Correct |
24 ms |
22256 KB |
Output is correct |
6 |
Correct |
22 ms |
21908 KB |
Output is correct |
7 |
Correct |
19 ms |
21892 KB |
Output is correct |
8 |
Correct |
23 ms |
22228 KB |
Output is correct |
9 |
Correct |
19 ms |
21888 KB |
Output is correct |
10 |
Correct |
22 ms |
21836 KB |
Output is correct |
11 |
Correct |
18 ms |
21820 KB |
Output is correct |
12 |
Correct |
24 ms |
22348 KB |
Output is correct |
13 |
Correct |
17 ms |
22356 KB |
Output is correct |
14 |
Correct |
27 ms |
22076 KB |
Output is correct |
15 |
Correct |
24 ms |
22104 KB |
Output is correct |
16 |
Correct |
22 ms |
22228 KB |
Output is correct |
17 |
Correct |
23 ms |
21900 KB |
Output is correct |
18 |
Correct |
20 ms |
21836 KB |
Output is correct |
19 |
Correct |
25 ms |
22256 KB |
Output is correct |
20 |
Correct |
26 ms |
21976 KB |
Output is correct |
21 |
Correct |
20 ms |
21964 KB |
Output is correct |
22 |
Correct |
19 ms |
22224 KB |
Output is correct |
23 |
Correct |
21 ms |
21844 KB |
Output is correct |
24 |
Correct |
20 ms |
22356 KB |
Output is correct |
25 |
Correct |
20 ms |
22008 KB |
Output is correct |
26 |
Correct |
19 ms |
22348 KB |
Output is correct |
27 |
Correct |
21 ms |
22360 KB |
Output is correct |
28 |
Correct |
20 ms |
22404 KB |
Output is correct |
29 |
Correct |
22 ms |
22608 KB |
Output is correct |
30 |
Correct |
20 ms |
22680 KB |
Output is correct |
31 |
Correct |
22 ms |
22604 KB |
Output is correct |
32 |
Correct |
26 ms |
22232 KB |
Output is correct |
33 |
Correct |
394 ms |
56216 KB |
Output is correct |
34 |
Correct |
350 ms |
39436 KB |
Output is correct |
35 |
Correct |
398 ms |
54232 KB |
Output is correct |
36 |
Correct |
329 ms |
39444 KB |
Output is correct |
37 |
Correct |
224 ms |
39156 KB |
Output is correct |
38 |
Correct |
235 ms |
38852 KB |
Output is correct |
39 |
Correct |
179 ms |
57964 KB |
Output is correct |
40 |
Correct |
189 ms |
57880 KB |
Output is correct |
41 |
Correct |
150 ms |
57808 KB |
Output is correct |
42 |
Correct |
174 ms |
50220 KB |
Output is correct |
43 |
Correct |
181 ms |
50156 KB |
Output is correct |
44 |
Correct |
438 ms |
55848 KB |
Output is correct |
45 |
Correct |
282 ms |
39404 KB |
Output is correct |
46 |
Correct |
172 ms |
38984 KB |
Output is correct |
47 |
Correct |
305 ms |
57220 KB |
Output is correct |
48 |
Correct |
195 ms |
46832 KB |
Output is correct |
49 |
Correct |
156 ms |
46840 KB |
Output is correct |
50 |
Correct |
372 ms |
54712 KB |
Output is correct |
51 |
Correct |
135 ms |
36196 KB |
Output is correct |
52 |
Correct |
289 ms |
57996 KB |
Output is correct |
53 |
Correct |
162 ms |
47464 KB |
Output is correct |
54 |
Correct |
145 ms |
58132 KB |
Output is correct |
55 |
Correct |
237 ms |
60012 KB |
Output is correct |
56 |
Correct |
258 ms |
66184 KB |
Output is correct |
57 |
Correct |
265 ms |
69612 KB |
Output is correct |
58 |
Correct |
266 ms |
71272 KB |
Output is correct |
59 |
Correct |
275 ms |
71264 KB |
Output is correct |
60 |
Correct |
14 ms |
21452 KB |
Output is correct |
61 |
Correct |
22 ms |
21452 KB |
Output is correct |
62 |
Correct |
26 ms |
21324 KB |
Output is correct |
63 |
Correct |
17 ms |
21452 KB |
Output is correct |
64 |
Correct |
459 ms |
53052 KB |
Output is correct |
65 |
Correct |
318 ms |
40784 KB |
Output is correct |
66 |
Correct |
438 ms |
51100 KB |
Output is correct |
67 |
Correct |
338 ms |
41232 KB |
Output is correct |
68 |
Correct |
298 ms |
40044 KB |
Output is correct |
69 |
Correct |
305 ms |
40020 KB |
Output is correct |
70 |
Correct |
263 ms |
46104 KB |
Output is correct |
71 |
Correct |
124 ms |
37656 KB |
Output is correct |
72 |
Correct |
406 ms |
50052 KB |
Output is correct |
73 |
Correct |
127 ms |
37712 KB |
Output is correct |
74 |
Correct |
349 ms |
54004 KB |
Output is correct |
75 |
Correct |
192 ms |
40164 KB |
Output is correct |
76 |
Correct |
178 ms |
39976 KB |
Output is correct |
77 |
Correct |
353 ms |
51292 KB |
Output is correct |
78 |
Correct |
146 ms |
37520 KB |
Output is correct |
79 |
Correct |
355 ms |
50652 KB |
Output is correct |
80 |
Correct |
264 ms |
38844 KB |
Output is correct |
81 |
Correct |
213 ms |
38548 KB |
Output is correct |
82 |
Correct |
394 ms |
50312 KB |
Output is correct |
83 |
Correct |
149 ms |
46944 KB |
Output is correct |
84 |
Correct |
370 ms |
55720 KB |
Output is correct |
85 |
Correct |
326 ms |
55824 KB |
Output is correct |
86 |
Correct |
331 ms |
52524 KB |
Output is correct |
87 |
Correct |
343 ms |
55748 KB |
Output is correct |
88 |
Correct |
365 ms |
55732 KB |
Output is correct |