#include <bits/stdc++.h>
#define int long long
using namespace std;
string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
bool first = true;
string res = "[";
for (const auto &x : v) {
if (!first)
res += ", ";
first = false;
res += to_string(x);
}
res += "]";
return res;
}
void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
cout << ' ' << to_string(H);
dbg_out(T...);
}
#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
const int MAXN = 2e5;
vector<int> childs[MAXN];
int par[MAXN];
int health[MAXN];
int cost[MAXN];
bool inCycle[MAXN];
vector<vector<int>> cycles;
int sz[MAXN];
int sumCost[MAXN];
map<int, int> deltas[MAXN];
void solve(int u) {
sumCost[u] = cost[u];
for (int v : childs[u])
if (!inCycle[v]) {
solve(v);
sz[u] += sz[v];
sumCost[u] += sumCost[v];
if (deltas[u].size() < deltas[v].size())
deltas[u].swap(deltas[v]);
for (auto [x, d] : deltas[v])
deltas[u][x] += d;
}
deltas[u][health[u]] += cost[u];
int toRem = cost[u];
while (toRem > 0) {
auto it = deltas[u].lower_bound(health[u]);
if (it == deltas[u].begin())
break;
--it;
int rem = min(toRem, it->second);
toRem -= rem;
it->second -= rem;
if (it->second == 0)
deltas[u].erase(it);
}
}
signed main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
int nbPersonnes;
cin >> nbPersonnes;
vector<int> values;
for (int i = 0; i < nbPersonnes; ++i) {
cin >> par[i] >> health[i] >> cost[i];
--par[i];
childs[par[i]].push_back(i);
values.push_back(health[i]);
}
sort(values.begin(), values.end());
values.resize(unique(values.begin(), values.end()) - values.begin());
for (int i = 0; i < nbPersonnes; ++i) {
health[i] =
lower_bound(values.begin(), values.end(), health[i]) - values.begin();
}
vector<int> visited(nbPersonnes, 0);
for (int u = 0; u < nbPersonnes; ++u)
if (not visited[u]) {
vector<int> stk;
int v = u;
while (!visited[v]) {
stk.push_back(v);
visited[v] = 1;
v = par[v];
}
if (visited[v] == 1) {
vector<int> c;
while (stk.back() != v) {
c.push_back(stk.back());
stk.pop_back();
}
c.push_back(stk.back());
stk.pop_back();
cycles.push_back(c);
for (int x : c)
inCycle[x] = true, visited[x] = 2;
}
for (int x : stk)
visited[x] = 2;
}
dbg(cycles);
int sol = 0;
for (auto cyc : cycles) {
map<int, int> deltaCycle;
for (int root : cyc)
for (int u : childs[root])
if (!inCycle[u]) {
solve(u);
if (deltaCycle.size() < deltas[u].size())
deltaCycle.swap(deltas[u]);
for (auto [x, d] : deltas[u])
deltaCycle[x] += d;
}
for (auto it = deltaCycle.rbegin(); it != deltaCycle.rend(); ++it)
if (it != deltaCycle.rbegin())
it->second += prev(it)->second;
map<int, int> withH;
int costCycle = 0;
for (int root : cyc)
withH[health[root]] += cost[root], costCycle += cost[root];
for (int root : cyc)
for (int u : childs[root])
if (!inCycle[u])
costCycle += sumCost[u];
int cur = 1e18;
for (auto [x, d] : deltaCycle) {
cur = min(cur, costCycle - withH[x]);
}
for (auto [h, c] : withH) {
auto it = deltaCycle.lower_bound(h);
int curCost = costCycle - c;
if (it != deltaCycle.end())
curCost -= it->second;
cur = min(cur, curCost);
}
sol += cur;
}
cout << sol << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14428 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14448 KB |
Output is correct |
5 |
Correct |
12 ms |
16132 KB |
Output is correct |
6 |
Correct |
11 ms |
15444 KB |
Output is correct |
7 |
Correct |
12 ms |
15172 KB |
Output is correct |
8 |
Correct |
12 ms |
16084 KB |
Output is correct |
9 |
Correct |
10 ms |
15380 KB |
Output is correct |
10 |
Correct |
9 ms |
15208 KB |
Output is correct |
11 |
Correct |
8 ms |
15084 KB |
Output is correct |
12 |
Correct |
10 ms |
15700 KB |
Output is correct |
13 |
Correct |
11 ms |
15704 KB |
Output is correct |
14 |
Correct |
10 ms |
15380 KB |
Output is correct |
15 |
Correct |
9 ms |
15316 KB |
Output is correct |
16 |
Correct |
13 ms |
16468 KB |
Output is correct |
17 |
Correct |
11 ms |
15588 KB |
Output is correct |
18 |
Correct |
10 ms |
14932 KB |
Output is correct |
19 |
Correct |
11 ms |
15700 KB |
Output is correct |
20 |
Correct |
9 ms |
15332 KB |
Output is correct |
21 |
Correct |
8 ms |
15336 KB |
Output is correct |
22 |
Correct |
12 ms |
15724 KB |
Output is correct |
23 |
Correct |
9 ms |
15060 KB |
Output is correct |
24 |
Correct |
11 ms |
15828 KB |
Output is correct |
25 |
Correct |
10 ms |
15468 KB |
Output is correct |
26 |
Correct |
10 ms |
15816 KB |
Output is correct |
27 |
Correct |
11 ms |
15768 KB |
Output is correct |
28 |
Correct |
13 ms |
15956 KB |
Output is correct |
29 |
Correct |
11 ms |
16084 KB |
Output is correct |
30 |
Correct |
11 ms |
16116 KB |
Output is correct |
31 |
Correct |
11 ms |
16104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14428 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14448 KB |
Output is correct |
5 |
Correct |
12 ms |
16132 KB |
Output is correct |
6 |
Correct |
11 ms |
15444 KB |
Output is correct |
7 |
Correct |
12 ms |
15172 KB |
Output is correct |
8 |
Correct |
12 ms |
16084 KB |
Output is correct |
9 |
Correct |
10 ms |
15380 KB |
Output is correct |
10 |
Correct |
9 ms |
15208 KB |
Output is correct |
11 |
Correct |
8 ms |
15084 KB |
Output is correct |
12 |
Correct |
10 ms |
15700 KB |
Output is correct |
13 |
Correct |
11 ms |
15704 KB |
Output is correct |
14 |
Correct |
10 ms |
15380 KB |
Output is correct |
15 |
Correct |
9 ms |
15316 KB |
Output is correct |
16 |
Correct |
13 ms |
16468 KB |
Output is correct |
17 |
Correct |
11 ms |
15588 KB |
Output is correct |
18 |
Correct |
10 ms |
14932 KB |
Output is correct |
19 |
Correct |
11 ms |
15700 KB |
Output is correct |
20 |
Correct |
9 ms |
15332 KB |
Output is correct |
21 |
Correct |
8 ms |
15336 KB |
Output is correct |
22 |
Correct |
12 ms |
15724 KB |
Output is correct |
23 |
Correct |
9 ms |
15060 KB |
Output is correct |
24 |
Correct |
11 ms |
15828 KB |
Output is correct |
25 |
Correct |
10 ms |
15468 KB |
Output is correct |
26 |
Correct |
10 ms |
15816 KB |
Output is correct |
27 |
Correct |
11 ms |
15768 KB |
Output is correct |
28 |
Correct |
13 ms |
15956 KB |
Output is correct |
29 |
Correct |
11 ms |
16084 KB |
Output is correct |
30 |
Correct |
11 ms |
16116 KB |
Output is correct |
31 |
Correct |
11 ms |
16104 KB |
Output is correct |
32 |
Correct |
13 ms |
16080 KB |
Output is correct |
33 |
Correct |
361 ms |
93824 KB |
Output is correct |
34 |
Correct |
240 ms |
59236 KB |
Output is correct |
35 |
Correct |
355 ms |
90744 KB |
Output is correct |
36 |
Correct |
233 ms |
59216 KB |
Output is correct |
37 |
Correct |
160 ms |
40128 KB |
Output is correct |
38 |
Correct |
136 ms |
35768 KB |
Output is correct |
39 |
Correct |
178 ms |
63540 KB |
Output is correct |
40 |
Correct |
133 ms |
63420 KB |
Output is correct |
41 |
Correct |
91 ms |
63436 KB |
Output is correct |
42 |
Correct |
171 ms |
48060 KB |
Output is correct |
43 |
Correct |
132 ms |
47960 KB |
Output is correct |
44 |
Correct |
340 ms |
116856 KB |
Output is correct |
45 |
Correct |
207 ms |
72168 KB |
Output is correct |
46 |
Correct |
76 ms |
34436 KB |
Output is correct |
47 |
Correct |
251 ms |
64048 KB |
Output is correct |
48 |
Correct |
116 ms |
50180 KB |
Output is correct |
49 |
Correct |
85 ms |
50136 KB |
Output is correct |
50 |
Correct |
328 ms |
63452 KB |
Output is correct |
51 |
Correct |
99 ms |
38464 KB |
Output is correct |
52 |
Correct |
289 ms |
65288 KB |
Output is correct |
53 |
Correct |
115 ms |
50976 KB |
Output is correct |
54 |
Correct |
104 ms |
63416 KB |
Output is correct |
55 |
Correct |
225 ms |
69356 KB |
Output is correct |
56 |
Correct |
207 ms |
76600 KB |
Output is correct |
57 |
Correct |
228 ms |
80916 KB |
Output is correct |
58 |
Correct |
237 ms |
79032 KB |
Output is correct |
59 |
Correct |
229 ms |
79052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14428 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14448 KB |
Output is correct |
5 |
Correct |
12 ms |
16132 KB |
Output is correct |
6 |
Correct |
11 ms |
15444 KB |
Output is correct |
7 |
Correct |
12 ms |
15172 KB |
Output is correct |
8 |
Correct |
12 ms |
16084 KB |
Output is correct |
9 |
Correct |
10 ms |
15380 KB |
Output is correct |
10 |
Correct |
9 ms |
15208 KB |
Output is correct |
11 |
Correct |
8 ms |
15084 KB |
Output is correct |
12 |
Correct |
10 ms |
15700 KB |
Output is correct |
13 |
Correct |
11 ms |
15704 KB |
Output is correct |
14 |
Correct |
10 ms |
15380 KB |
Output is correct |
15 |
Correct |
9 ms |
15316 KB |
Output is correct |
16 |
Correct |
13 ms |
16468 KB |
Output is correct |
17 |
Correct |
11 ms |
15588 KB |
Output is correct |
18 |
Correct |
10 ms |
14932 KB |
Output is correct |
19 |
Correct |
11 ms |
15700 KB |
Output is correct |
20 |
Correct |
9 ms |
15332 KB |
Output is correct |
21 |
Correct |
8 ms |
15336 KB |
Output is correct |
22 |
Correct |
12 ms |
15724 KB |
Output is correct |
23 |
Correct |
9 ms |
15060 KB |
Output is correct |
24 |
Correct |
11 ms |
15828 KB |
Output is correct |
25 |
Correct |
10 ms |
15468 KB |
Output is correct |
26 |
Correct |
10 ms |
15816 KB |
Output is correct |
27 |
Correct |
11 ms |
15768 KB |
Output is correct |
28 |
Correct |
13 ms |
15956 KB |
Output is correct |
29 |
Correct |
11 ms |
16084 KB |
Output is correct |
30 |
Correct |
11 ms |
16116 KB |
Output is correct |
31 |
Correct |
11 ms |
16104 KB |
Output is correct |
32 |
Correct |
13 ms |
16080 KB |
Output is correct |
33 |
Correct |
361 ms |
93824 KB |
Output is correct |
34 |
Correct |
240 ms |
59236 KB |
Output is correct |
35 |
Correct |
355 ms |
90744 KB |
Output is correct |
36 |
Correct |
233 ms |
59216 KB |
Output is correct |
37 |
Correct |
160 ms |
40128 KB |
Output is correct |
38 |
Correct |
136 ms |
35768 KB |
Output is correct |
39 |
Correct |
178 ms |
63540 KB |
Output is correct |
40 |
Correct |
133 ms |
63420 KB |
Output is correct |
41 |
Correct |
91 ms |
63436 KB |
Output is correct |
42 |
Correct |
171 ms |
48060 KB |
Output is correct |
43 |
Correct |
132 ms |
47960 KB |
Output is correct |
44 |
Correct |
340 ms |
116856 KB |
Output is correct |
45 |
Correct |
207 ms |
72168 KB |
Output is correct |
46 |
Correct |
76 ms |
34436 KB |
Output is correct |
47 |
Correct |
251 ms |
64048 KB |
Output is correct |
48 |
Correct |
116 ms |
50180 KB |
Output is correct |
49 |
Correct |
85 ms |
50136 KB |
Output is correct |
50 |
Correct |
328 ms |
63452 KB |
Output is correct |
51 |
Correct |
99 ms |
38464 KB |
Output is correct |
52 |
Correct |
289 ms |
65288 KB |
Output is correct |
53 |
Correct |
115 ms |
50976 KB |
Output is correct |
54 |
Correct |
104 ms |
63416 KB |
Output is correct |
55 |
Correct |
225 ms |
69356 KB |
Output is correct |
56 |
Correct |
207 ms |
76600 KB |
Output is correct |
57 |
Correct |
228 ms |
80916 KB |
Output is correct |
58 |
Correct |
237 ms |
79032 KB |
Output is correct |
59 |
Correct |
229 ms |
79052 KB |
Output is correct |
60 |
Correct |
10 ms |
14548 KB |
Output is correct |
61 |
Correct |
9 ms |
14420 KB |
Output is correct |
62 |
Correct |
7 ms |
14420 KB |
Output is correct |
63 |
Correct |
7 ms |
14420 KB |
Output is correct |
64 |
Correct |
390 ms |
81584 KB |
Output is correct |
65 |
Correct |
236 ms |
56580 KB |
Output is correct |
66 |
Correct |
323 ms |
74912 KB |
Output is correct |
67 |
Correct |
243 ms |
56756 KB |
Output is correct |
68 |
Correct |
194 ms |
42980 KB |
Output is correct |
69 |
Correct |
185 ms |
39588 KB |
Output is correct |
70 |
Correct |
228 ms |
45024 KB |
Output is correct |
71 |
Correct |
149 ms |
36868 KB |
Output is correct |
72 |
Correct |
287 ms |
49500 KB |
Output is correct |
73 |
Correct |
143 ms |
38624 KB |
Output is correct |
74 |
Correct |
390 ms |
58872 KB |
Output is correct |
75 |
Correct |
212 ms |
40224 KB |
Output is correct |
76 |
Correct |
192 ms |
40116 KB |
Output is correct |
77 |
Correct |
312 ms |
59620 KB |
Output is correct |
78 |
Correct |
131 ms |
40928 KB |
Output is correct |
79 |
Correct |
346 ms |
75336 KB |
Output is correct |
80 |
Correct |
215 ms |
53356 KB |
Output is correct |
81 |
Correct |
156 ms |
42720 KB |
Output is correct |
82 |
Correct |
200 ms |
49692 KB |
Output is correct |
83 |
Correct |
133 ms |
44748 KB |
Output is correct |
84 |
Correct |
316 ms |
66152 KB |
Output is correct |
85 |
Correct |
313 ms |
66208 KB |
Output is correct |
86 |
Correct |
307 ms |
62964 KB |
Output is correct |
87 |
Correct |
299 ms |
66108 KB |
Output is correct |
88 |
Correct |
308 ms |
66184 KB |
Output is correct |