#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 2e5 + 5;
int n;
int a[N], c[N], lab[N], deg[N];
bool vis[N];
vector<int> g[N], gph[N];
map<int, long long> mp[N];
void dfs(int u) {
for (int v : g[u]) {
dfs(v);
if (mp[u].size() < mp[v].size()) {
mp[u].swap(mp[v]);
}
for (auto [x, y] : mp[v]) {
mp[u][x] += y;
}
}
if (a[u] != -1) {
mp[u][a[u]] += c[u];
for (auto it = mp[u].upper_bound(a[u]); it != mp[u].end(); it = mp[u].erase(it)) {
auto &delta = (*it).second;
if (c[u] < delta) {
delta -= c[u];
break;
}
c[u] -= delta;
}
}
}
vector<int> st, ver;
void DFS(int u) {
ver.push_back(u);
vis[u] = 1;
st.push_back(u);
for (int v : gph[u]) {
if (vis[v]) {
while (1) {
int x = st.back(); st.pop_back();
lab[x] = u;
if (u != x) {
c[u] += c[x];
}
if (a[x] != a[u]) {
a[u] = -1;
}
if (x == v) {
break;
}
}
} else {
DFS(v);
}
}
if (lab[u] == -1) {
st.pop_back();
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n;
long long res = 0;
for (int i = 1; i <= n; ++i) {
int p; cin >> p >> a[i] >> c[i];
a[i] = 1e9 - a[i] + 1;
res += c[i];
gph[p].push_back(i);
}
fill(lab + 1, lab + n + 1, -1);
for (int i = 1; i <= n; ++i) {
if (!vis[i]) {
DFS(i);
for (int u : ver) {
for (int v : gph[u]) {
if (lab[v] == -1) {
g[u].push_back(v);
++deg[v];
}
}
}
for (int u : ver) {
if ((lab[u] == -1 || lab[u] == u) && !deg[u]) {
dfs(u);
for (auto [x, y] : mp[u]) {
res -= y;
}
}
}
ver.clear();
}
}
cout << res;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21080 KB |
Output is correct |
2 |
Correct |
4 ms |
21084 KB |
Output is correct |
3 |
Correct |
4 ms |
21152 KB |
Output is correct |
4 |
Correct |
5 ms |
21084 KB |
Output is correct |
5 |
Correct |
9 ms |
22456 KB |
Output is correct |
6 |
Correct |
7 ms |
21924 KB |
Output is correct |
7 |
Correct |
7 ms |
21696 KB |
Output is correct |
8 |
Correct |
8 ms |
22312 KB |
Output is correct |
9 |
Correct |
7 ms |
21848 KB |
Output is correct |
10 |
Correct |
8 ms |
21592 KB |
Output is correct |
11 |
Correct |
6 ms |
21640 KB |
Output is correct |
12 |
Correct |
8 ms |
22316 KB |
Output is correct |
13 |
Correct |
6 ms |
22364 KB |
Output is correct |
14 |
Correct |
6 ms |
21984 KB |
Output is correct |
15 |
Correct |
7 ms |
21848 KB |
Output is correct |
16 |
Correct |
8 ms |
22620 KB |
Output is correct |
17 |
Correct |
7 ms |
22108 KB |
Output is correct |
18 |
Correct |
5 ms |
21540 KB |
Output is correct |
19 |
Correct |
6 ms |
22108 KB |
Output is correct |
20 |
Correct |
6 ms |
21852 KB |
Output is correct |
21 |
Correct |
6 ms |
21852 KB |
Output is correct |
22 |
Correct |
6 ms |
21852 KB |
Output is correct |
23 |
Correct |
6 ms |
21596 KB |
Output is correct |
24 |
Correct |
7 ms |
22108 KB |
Output is correct |
25 |
Correct |
6 ms |
22008 KB |
Output is correct |
26 |
Correct |
7 ms |
22364 KB |
Output is correct |
27 |
Correct |
7 ms |
22108 KB |
Output is correct |
28 |
Correct |
7 ms |
22088 KB |
Output is correct |
29 |
Correct |
6 ms |
22448 KB |
Output is correct |
30 |
Correct |
6 ms |
22324 KB |
Output is correct |
31 |
Correct |
7 ms |
22464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21080 KB |
Output is correct |
2 |
Correct |
4 ms |
21084 KB |
Output is correct |
3 |
Correct |
4 ms |
21152 KB |
Output is correct |
4 |
Correct |
5 ms |
21084 KB |
Output is correct |
5 |
Correct |
9 ms |
22456 KB |
Output is correct |
6 |
Correct |
7 ms |
21924 KB |
Output is correct |
7 |
Correct |
7 ms |
21696 KB |
Output is correct |
8 |
Correct |
8 ms |
22312 KB |
Output is correct |
9 |
Correct |
7 ms |
21848 KB |
Output is correct |
10 |
Correct |
8 ms |
21592 KB |
Output is correct |
11 |
Correct |
6 ms |
21640 KB |
Output is correct |
12 |
Correct |
8 ms |
22316 KB |
Output is correct |
13 |
Correct |
6 ms |
22364 KB |
Output is correct |
14 |
Correct |
6 ms |
21984 KB |
Output is correct |
15 |
Correct |
7 ms |
21848 KB |
Output is correct |
16 |
Correct |
8 ms |
22620 KB |
Output is correct |
17 |
Correct |
7 ms |
22108 KB |
Output is correct |
18 |
Correct |
5 ms |
21540 KB |
Output is correct |
19 |
Correct |
6 ms |
22108 KB |
Output is correct |
20 |
Correct |
6 ms |
21852 KB |
Output is correct |
21 |
Correct |
6 ms |
21852 KB |
Output is correct |
22 |
Correct |
6 ms |
21852 KB |
Output is correct |
23 |
Correct |
6 ms |
21596 KB |
Output is correct |
24 |
Correct |
7 ms |
22108 KB |
Output is correct |
25 |
Correct |
6 ms |
22008 KB |
Output is correct |
26 |
Correct |
7 ms |
22364 KB |
Output is correct |
27 |
Correct |
7 ms |
22108 KB |
Output is correct |
28 |
Correct |
7 ms |
22088 KB |
Output is correct |
29 |
Correct |
6 ms |
22448 KB |
Output is correct |
30 |
Correct |
6 ms |
22324 KB |
Output is correct |
31 |
Correct |
7 ms |
22464 KB |
Output is correct |
32 |
Correct |
12 ms |
22364 KB |
Output is correct |
33 |
Correct |
229 ms |
89036 KB |
Output is correct |
34 |
Correct |
178 ms |
64200 KB |
Output is correct |
35 |
Correct |
233 ms |
86212 KB |
Output is correct |
36 |
Correct |
166 ms |
64180 KB |
Output is correct |
37 |
Correct |
105 ms |
45220 KB |
Output is correct |
38 |
Correct |
95 ms |
40832 KB |
Output is correct |
39 |
Correct |
96 ms |
70000 KB |
Output is correct |
40 |
Correct |
90 ms |
69956 KB |
Output is correct |
41 |
Correct |
82 ms |
69892 KB |
Output is correct |
42 |
Correct |
90 ms |
55408 KB |
Output is correct |
43 |
Correct |
103 ms |
55408 KB |
Output is correct |
44 |
Correct |
226 ms |
113920 KB |
Output is correct |
45 |
Correct |
146 ms |
78280 KB |
Output is correct |
46 |
Correct |
61 ms |
40900 KB |
Output is correct |
47 |
Correct |
165 ms |
62100 KB |
Output is correct |
48 |
Correct |
78 ms |
55408 KB |
Output is correct |
49 |
Correct |
76 ms |
55232 KB |
Output is correct |
50 |
Correct |
184 ms |
54132 KB |
Output is correct |
51 |
Correct |
82 ms |
41648 KB |
Output is correct |
52 |
Correct |
133 ms |
63432 KB |
Output is correct |
53 |
Correct |
78 ms |
56264 KB |
Output is correct |
54 |
Correct |
75 ms |
69988 KB |
Output is correct |
55 |
Correct |
152 ms |
63784 KB |
Output is correct |
56 |
Correct |
124 ms |
71532 KB |
Output is correct |
57 |
Correct |
134 ms |
75192 KB |
Output is correct |
58 |
Correct |
125 ms |
73364 KB |
Output is correct |
59 |
Correct |
136 ms |
73408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
21080 KB |
Output is correct |
2 |
Correct |
4 ms |
21084 KB |
Output is correct |
3 |
Correct |
4 ms |
21152 KB |
Output is correct |
4 |
Correct |
5 ms |
21084 KB |
Output is correct |
5 |
Correct |
9 ms |
22456 KB |
Output is correct |
6 |
Correct |
7 ms |
21924 KB |
Output is correct |
7 |
Correct |
7 ms |
21696 KB |
Output is correct |
8 |
Correct |
8 ms |
22312 KB |
Output is correct |
9 |
Correct |
7 ms |
21848 KB |
Output is correct |
10 |
Correct |
8 ms |
21592 KB |
Output is correct |
11 |
Correct |
6 ms |
21640 KB |
Output is correct |
12 |
Correct |
8 ms |
22316 KB |
Output is correct |
13 |
Correct |
6 ms |
22364 KB |
Output is correct |
14 |
Correct |
6 ms |
21984 KB |
Output is correct |
15 |
Correct |
7 ms |
21848 KB |
Output is correct |
16 |
Correct |
8 ms |
22620 KB |
Output is correct |
17 |
Correct |
7 ms |
22108 KB |
Output is correct |
18 |
Correct |
5 ms |
21540 KB |
Output is correct |
19 |
Correct |
6 ms |
22108 KB |
Output is correct |
20 |
Correct |
6 ms |
21852 KB |
Output is correct |
21 |
Correct |
6 ms |
21852 KB |
Output is correct |
22 |
Correct |
6 ms |
21852 KB |
Output is correct |
23 |
Correct |
6 ms |
21596 KB |
Output is correct |
24 |
Correct |
7 ms |
22108 KB |
Output is correct |
25 |
Correct |
6 ms |
22008 KB |
Output is correct |
26 |
Correct |
7 ms |
22364 KB |
Output is correct |
27 |
Correct |
7 ms |
22108 KB |
Output is correct |
28 |
Correct |
7 ms |
22088 KB |
Output is correct |
29 |
Correct |
6 ms |
22448 KB |
Output is correct |
30 |
Correct |
6 ms |
22324 KB |
Output is correct |
31 |
Correct |
7 ms |
22464 KB |
Output is correct |
32 |
Correct |
12 ms |
22364 KB |
Output is correct |
33 |
Correct |
229 ms |
89036 KB |
Output is correct |
34 |
Correct |
178 ms |
64200 KB |
Output is correct |
35 |
Correct |
233 ms |
86212 KB |
Output is correct |
36 |
Correct |
166 ms |
64180 KB |
Output is correct |
37 |
Correct |
105 ms |
45220 KB |
Output is correct |
38 |
Correct |
95 ms |
40832 KB |
Output is correct |
39 |
Correct |
96 ms |
70000 KB |
Output is correct |
40 |
Correct |
90 ms |
69956 KB |
Output is correct |
41 |
Correct |
82 ms |
69892 KB |
Output is correct |
42 |
Correct |
90 ms |
55408 KB |
Output is correct |
43 |
Correct |
103 ms |
55408 KB |
Output is correct |
44 |
Correct |
226 ms |
113920 KB |
Output is correct |
45 |
Correct |
146 ms |
78280 KB |
Output is correct |
46 |
Correct |
61 ms |
40900 KB |
Output is correct |
47 |
Correct |
165 ms |
62100 KB |
Output is correct |
48 |
Correct |
78 ms |
55408 KB |
Output is correct |
49 |
Correct |
76 ms |
55232 KB |
Output is correct |
50 |
Correct |
184 ms |
54132 KB |
Output is correct |
51 |
Correct |
82 ms |
41648 KB |
Output is correct |
52 |
Correct |
133 ms |
63432 KB |
Output is correct |
53 |
Correct |
78 ms |
56264 KB |
Output is correct |
54 |
Correct |
75 ms |
69988 KB |
Output is correct |
55 |
Correct |
152 ms |
63784 KB |
Output is correct |
56 |
Correct |
124 ms |
71532 KB |
Output is correct |
57 |
Correct |
134 ms |
75192 KB |
Output is correct |
58 |
Correct |
125 ms |
73364 KB |
Output is correct |
59 |
Correct |
136 ms |
73408 KB |
Output is correct |
60 |
Incorrect |
8 ms |
21084 KB |
Output isn't correct |
61 |
Halted |
0 ms |
0 KB |
- |