#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) {
for (int root : cyc)
solve(root);
int tailleCycle = cyc.size();
for (int root : cyc)
for (auto it = deltas[root].rbegin(); it != deltas[root].rend(); ++it)
if (it != deltas[root].rbegin())
it->second += prev(it)->second;
int cur = 0;
for (int root : cyc) {
cur += sumCost[root];
cur -= deltas[root].begin()->second;
}
sol += cur;
}
cout << sol << endl;
}
Compilation message
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:124:9: warning: unused variable 'tailleCycle' [-Wunused-variable]
124 | int tailleCycle = cyc.size();
| ^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14384 KB |
Output is correct |
2 |
Correct |
6 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14444 KB |
Output is correct |
4 |
Correct |
7 ms |
14428 KB |
Output is correct |
5 |
Correct |
15 ms |
15852 KB |
Output is correct |
6 |
Correct |
11 ms |
15464 KB |
Output is correct |
7 |
Correct |
9 ms |
15256 KB |
Output is correct |
8 |
Correct |
11 ms |
15956 KB |
Output is correct |
9 |
Correct |
10 ms |
15460 KB |
Output is correct |
10 |
Correct |
9 ms |
15188 KB |
Output is correct |
11 |
Correct |
10 ms |
15092 KB |
Output is correct |
12 |
Correct |
10 ms |
15720 KB |
Output is correct |
13 |
Correct |
9 ms |
15764 KB |
Output is correct |
14 |
Correct |
11 ms |
15336 KB |
Output is correct |
15 |
Correct |
11 ms |
15444 KB |
Output is correct |
16 |
Correct |
11 ms |
16208 KB |
Output is correct |
17 |
Correct |
12 ms |
15592 KB |
Output is correct |
18 |
Correct |
8 ms |
14956 KB |
Output is correct |
19 |
Correct |
10 ms |
15572 KB |
Output is correct |
20 |
Correct |
10 ms |
15348 KB |
Output is correct |
21 |
Correct |
8 ms |
15444 KB |
Output is correct |
22 |
Correct |
10 ms |
15444 KB |
Output is correct |
23 |
Correct |
8 ms |
15136 KB |
Output is correct |
24 |
Correct |
10 ms |
15596 KB |
Output is correct |
25 |
Correct |
9 ms |
15460 KB |
Output is correct |
26 |
Correct |
8 ms |
15700 KB |
Output is correct |
27 |
Correct |
10 ms |
15572 KB |
Output is correct |
28 |
Correct |
9 ms |
15664 KB |
Output is correct |
29 |
Correct |
10 ms |
15828 KB |
Output is correct |
30 |
Correct |
10 ms |
15716 KB |
Output is correct |
31 |
Correct |
10 ms |
15848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14384 KB |
Output is correct |
2 |
Correct |
6 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14444 KB |
Output is correct |
4 |
Correct |
7 ms |
14428 KB |
Output is correct |
5 |
Correct |
15 ms |
15852 KB |
Output is correct |
6 |
Correct |
11 ms |
15464 KB |
Output is correct |
7 |
Correct |
9 ms |
15256 KB |
Output is correct |
8 |
Correct |
11 ms |
15956 KB |
Output is correct |
9 |
Correct |
10 ms |
15460 KB |
Output is correct |
10 |
Correct |
9 ms |
15188 KB |
Output is correct |
11 |
Correct |
10 ms |
15092 KB |
Output is correct |
12 |
Correct |
10 ms |
15720 KB |
Output is correct |
13 |
Correct |
9 ms |
15764 KB |
Output is correct |
14 |
Correct |
11 ms |
15336 KB |
Output is correct |
15 |
Correct |
11 ms |
15444 KB |
Output is correct |
16 |
Correct |
11 ms |
16208 KB |
Output is correct |
17 |
Correct |
12 ms |
15592 KB |
Output is correct |
18 |
Correct |
8 ms |
14956 KB |
Output is correct |
19 |
Correct |
10 ms |
15572 KB |
Output is correct |
20 |
Correct |
10 ms |
15348 KB |
Output is correct |
21 |
Correct |
8 ms |
15444 KB |
Output is correct |
22 |
Correct |
10 ms |
15444 KB |
Output is correct |
23 |
Correct |
8 ms |
15136 KB |
Output is correct |
24 |
Correct |
10 ms |
15596 KB |
Output is correct |
25 |
Correct |
9 ms |
15460 KB |
Output is correct |
26 |
Correct |
8 ms |
15700 KB |
Output is correct |
27 |
Correct |
10 ms |
15572 KB |
Output is correct |
28 |
Correct |
9 ms |
15664 KB |
Output is correct |
29 |
Correct |
10 ms |
15828 KB |
Output is correct |
30 |
Correct |
10 ms |
15716 KB |
Output is correct |
31 |
Correct |
10 ms |
15848 KB |
Output is correct |
32 |
Correct |
11 ms |
15824 KB |
Output is correct |
33 |
Correct |
328 ms |
88556 KB |
Output is correct |
34 |
Correct |
229 ms |
63632 KB |
Output is correct |
35 |
Correct |
327 ms |
85440 KB |
Output is correct |
36 |
Correct |
254 ms |
63660 KB |
Output is correct |
37 |
Correct |
150 ms |
44728 KB |
Output is correct |
38 |
Correct |
144 ms |
40156 KB |
Output is correct |
39 |
Correct |
168 ms |
68084 KB |
Output is correct |
40 |
Correct |
135 ms |
68020 KB |
Output is correct |
41 |
Correct |
92 ms |
68024 KB |
Output is correct |
42 |
Correct |
160 ms |
52596 KB |
Output is correct |
43 |
Correct |
131 ms |
52428 KB |
Output is correct |
44 |
Correct |
304 ms |
112256 KB |
Output is correct |
45 |
Correct |
216 ms |
76764 KB |
Output is correct |
46 |
Correct |
80 ms |
38916 KB |
Output is correct |
47 |
Correct |
220 ms |
61628 KB |
Output is correct |
48 |
Correct |
120 ms |
54628 KB |
Output is correct |
49 |
Correct |
98 ms |
54600 KB |
Output is correct |
50 |
Correct |
247 ms |
54704 KB |
Output is correct |
51 |
Correct |
99 ms |
42176 KB |
Output is correct |
52 |
Correct |
226 ms |
62644 KB |
Output is correct |
53 |
Correct |
114 ms |
55528 KB |
Output is correct |
54 |
Correct |
101 ms |
68212 KB |
Output is correct |
55 |
Correct |
177 ms |
63896 KB |
Output is correct |
56 |
Correct |
154 ms |
70448 KB |
Output is correct |
57 |
Correct |
156 ms |
73748 KB |
Output is correct |
58 |
Correct |
209 ms |
70444 KB |
Output is correct |
59 |
Correct |
181 ms |
70456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14384 KB |
Output is correct |
2 |
Correct |
6 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14444 KB |
Output is correct |
4 |
Correct |
7 ms |
14428 KB |
Output is correct |
5 |
Correct |
15 ms |
15852 KB |
Output is correct |
6 |
Correct |
11 ms |
15464 KB |
Output is correct |
7 |
Correct |
9 ms |
15256 KB |
Output is correct |
8 |
Correct |
11 ms |
15956 KB |
Output is correct |
9 |
Correct |
10 ms |
15460 KB |
Output is correct |
10 |
Correct |
9 ms |
15188 KB |
Output is correct |
11 |
Correct |
10 ms |
15092 KB |
Output is correct |
12 |
Correct |
10 ms |
15720 KB |
Output is correct |
13 |
Correct |
9 ms |
15764 KB |
Output is correct |
14 |
Correct |
11 ms |
15336 KB |
Output is correct |
15 |
Correct |
11 ms |
15444 KB |
Output is correct |
16 |
Correct |
11 ms |
16208 KB |
Output is correct |
17 |
Correct |
12 ms |
15592 KB |
Output is correct |
18 |
Correct |
8 ms |
14956 KB |
Output is correct |
19 |
Correct |
10 ms |
15572 KB |
Output is correct |
20 |
Correct |
10 ms |
15348 KB |
Output is correct |
21 |
Correct |
8 ms |
15444 KB |
Output is correct |
22 |
Correct |
10 ms |
15444 KB |
Output is correct |
23 |
Correct |
8 ms |
15136 KB |
Output is correct |
24 |
Correct |
10 ms |
15596 KB |
Output is correct |
25 |
Correct |
9 ms |
15460 KB |
Output is correct |
26 |
Correct |
8 ms |
15700 KB |
Output is correct |
27 |
Correct |
10 ms |
15572 KB |
Output is correct |
28 |
Correct |
9 ms |
15664 KB |
Output is correct |
29 |
Correct |
10 ms |
15828 KB |
Output is correct |
30 |
Correct |
10 ms |
15716 KB |
Output is correct |
31 |
Correct |
10 ms |
15848 KB |
Output is correct |
32 |
Correct |
11 ms |
15824 KB |
Output is correct |
33 |
Correct |
328 ms |
88556 KB |
Output is correct |
34 |
Correct |
229 ms |
63632 KB |
Output is correct |
35 |
Correct |
327 ms |
85440 KB |
Output is correct |
36 |
Correct |
254 ms |
63660 KB |
Output is correct |
37 |
Correct |
150 ms |
44728 KB |
Output is correct |
38 |
Correct |
144 ms |
40156 KB |
Output is correct |
39 |
Correct |
168 ms |
68084 KB |
Output is correct |
40 |
Correct |
135 ms |
68020 KB |
Output is correct |
41 |
Correct |
92 ms |
68024 KB |
Output is correct |
42 |
Correct |
160 ms |
52596 KB |
Output is correct |
43 |
Correct |
131 ms |
52428 KB |
Output is correct |
44 |
Correct |
304 ms |
112256 KB |
Output is correct |
45 |
Correct |
216 ms |
76764 KB |
Output is correct |
46 |
Correct |
80 ms |
38916 KB |
Output is correct |
47 |
Correct |
220 ms |
61628 KB |
Output is correct |
48 |
Correct |
120 ms |
54628 KB |
Output is correct |
49 |
Correct |
98 ms |
54600 KB |
Output is correct |
50 |
Correct |
247 ms |
54704 KB |
Output is correct |
51 |
Correct |
99 ms |
42176 KB |
Output is correct |
52 |
Correct |
226 ms |
62644 KB |
Output is correct |
53 |
Correct |
114 ms |
55528 KB |
Output is correct |
54 |
Correct |
101 ms |
68212 KB |
Output is correct |
55 |
Correct |
177 ms |
63896 KB |
Output is correct |
56 |
Correct |
154 ms |
70448 KB |
Output is correct |
57 |
Correct |
156 ms |
73748 KB |
Output is correct |
58 |
Correct |
209 ms |
70444 KB |
Output is correct |
59 |
Correct |
181 ms |
70456 KB |
Output is correct |
60 |
Incorrect |
7 ms |
14420 KB |
Output isn't correct |
61 |
Halted |
0 ms |
0 KB |
- |