#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 200200;
int n;
int p[N];
int h[N];
int c[N];
map<int, ll> mp[N];
vector<int> g[N];
// u <- v
void join(int u, int v) {
if ((int)mp[u].size() < (int)mp[v].size()) swap(mp[u], mp[v]);
for (auto c : mp[v])
mp[u][c.first] += c.second;
};
bool vis[N];
void dfs(int v) {
vis[v] = true;
for (int to : g[v]) {
dfs(to);
join(v, to);
}
int s = c[v];
auto it = mp[v].upper_bound(h[v]);
while (it != mp[v].begin()) {
it--;
if (s >= it->second) {
s -= it->second;
mp[v].erase(it);
} else {
mp[v][it->first] = it->second - s;
s = 0;
break;
}
it = mp[v].upper_bound(h[v]);
}
mp[v][0] += c[v] - s;
mp[v][h[v] + 1] += c[v];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> p[i] >> h[i] >> c[i];
vector<deque<int>> cycles;
for (int i = 1; i <= n; i++) if (!vis[i]) {
deque<int> cyc;
int x = i;
while (!vis[x]) {
vis[x] = true;
cyc.push_back(x);
x = p[x];
}
while (!cyc.empty() && cyc.front() != x) cyc.pop_front();
// cout << '{';
// for (int c : cyc) cout << c << ' ';
// cout << '}' << '\n';
if (!cyc.empty())
cycles.push_back(cyc);
}
for (int i = 1; i <= n; i++) vis[i] = false;
for (auto c : cycles) for (int x : c)
vis[x] = true;
for (int i = 1; i <= n; i++) if (!vis[i])
g[p[i]].push_back(i);
ll res = 0;
for (auto cyc : cycles) {
for (int v : cyc) {
for (int to : g[v]) {
dfs(to);
join(v, to);
}
mp[v][0] += c[v];
mp[v][h[v]] -= c[v];
mp[v][h[v] + 1] += c[v];
}
for (int v : cyc)
if (v != cyc.back())
join(cyc.back(), v);
ll val = 0;
ll mn = 1e18;
for (auto c : mp[cyc.back()]) {
val += c.second;
if (val < mn) mn = val;
}
res += mn;
}
cout << res;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
16732 KB |
Output is correct |
2 |
Correct |
3 ms |
16732 KB |
Output is correct |
3 |
Correct |
4 ms |
16732 KB |
Output is correct |
4 |
Correct |
4 ms |
16728 KB |
Output is correct |
5 |
Correct |
8 ms |
18020 KB |
Output is correct |
6 |
Correct |
7 ms |
17660 KB |
Output is correct |
7 |
Correct |
7 ms |
17500 KB |
Output is correct |
8 |
Correct |
8 ms |
18168 KB |
Output is correct |
9 |
Correct |
8 ms |
17756 KB |
Output is correct |
10 |
Correct |
6 ms |
17404 KB |
Output is correct |
11 |
Correct |
6 ms |
17244 KB |
Output is correct |
12 |
Correct |
6 ms |
17664 KB |
Output is correct |
13 |
Correct |
5 ms |
17500 KB |
Output is correct |
14 |
Correct |
6 ms |
17244 KB |
Output is correct |
15 |
Correct |
6 ms |
17244 KB |
Output is correct |
16 |
Correct |
9 ms |
18524 KB |
Output is correct |
17 |
Correct |
8 ms |
17756 KB |
Output is correct |
18 |
Correct |
5 ms |
17244 KB |
Output is correct |
19 |
Correct |
7 ms |
17756 KB |
Output is correct |
20 |
Correct |
6 ms |
17496 KB |
Output is correct |
21 |
Correct |
6 ms |
17500 KB |
Output is correct |
22 |
Correct |
7 ms |
17756 KB |
Output is correct |
23 |
Correct |
7 ms |
17496 KB |
Output is correct |
24 |
Correct |
7 ms |
17756 KB |
Output is correct |
25 |
Correct |
6 ms |
17660 KB |
Output is correct |
26 |
Correct |
6 ms |
17656 KB |
Output is correct |
27 |
Correct |
7 ms |
17756 KB |
Output is correct |
28 |
Correct |
6 ms |
17844 KB |
Output is correct |
29 |
Correct |
6 ms |
17752 KB |
Output is correct |
30 |
Correct |
6 ms |
17756 KB |
Output is correct |
31 |
Correct |
7 ms |
17756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
16732 KB |
Output is correct |
2 |
Correct |
3 ms |
16732 KB |
Output is correct |
3 |
Correct |
4 ms |
16732 KB |
Output is correct |
4 |
Correct |
4 ms |
16728 KB |
Output is correct |
5 |
Correct |
8 ms |
18020 KB |
Output is correct |
6 |
Correct |
7 ms |
17660 KB |
Output is correct |
7 |
Correct |
7 ms |
17500 KB |
Output is correct |
8 |
Correct |
8 ms |
18168 KB |
Output is correct |
9 |
Correct |
8 ms |
17756 KB |
Output is correct |
10 |
Correct |
6 ms |
17404 KB |
Output is correct |
11 |
Correct |
6 ms |
17244 KB |
Output is correct |
12 |
Correct |
6 ms |
17664 KB |
Output is correct |
13 |
Correct |
5 ms |
17500 KB |
Output is correct |
14 |
Correct |
6 ms |
17244 KB |
Output is correct |
15 |
Correct |
6 ms |
17244 KB |
Output is correct |
16 |
Correct |
9 ms |
18524 KB |
Output is correct |
17 |
Correct |
8 ms |
17756 KB |
Output is correct |
18 |
Correct |
5 ms |
17244 KB |
Output is correct |
19 |
Correct |
7 ms |
17756 KB |
Output is correct |
20 |
Correct |
6 ms |
17496 KB |
Output is correct |
21 |
Correct |
6 ms |
17500 KB |
Output is correct |
22 |
Correct |
7 ms |
17756 KB |
Output is correct |
23 |
Correct |
7 ms |
17496 KB |
Output is correct |
24 |
Correct |
7 ms |
17756 KB |
Output is correct |
25 |
Correct |
6 ms |
17660 KB |
Output is correct |
26 |
Correct |
6 ms |
17656 KB |
Output is correct |
27 |
Correct |
7 ms |
17756 KB |
Output is correct |
28 |
Correct |
6 ms |
17844 KB |
Output is correct |
29 |
Correct |
6 ms |
17752 KB |
Output is correct |
30 |
Correct |
6 ms |
17756 KB |
Output is correct |
31 |
Correct |
7 ms |
17756 KB |
Output is correct |
32 |
Correct |
10 ms |
18012 KB |
Output is correct |
33 |
Correct |
301 ms |
85804 KB |
Output is correct |
34 |
Correct |
214 ms |
60784 KB |
Output is correct |
35 |
Correct |
279 ms |
83064 KB |
Output is correct |
36 |
Correct |
206 ms |
60948 KB |
Output is correct |
37 |
Correct |
148 ms |
41944 KB |
Output is correct |
38 |
Correct |
128 ms |
37536 KB |
Output is correct |
39 |
Correct |
133 ms |
50468 KB |
Output is correct |
40 |
Correct |
119 ms |
50400 KB |
Output is correct |
41 |
Correct |
81 ms |
50384 KB |
Output is correct |
42 |
Correct |
134 ms |
39372 KB |
Output is correct |
43 |
Correct |
117 ms |
38992 KB |
Output is correct |
44 |
Correct |
282 ms |
110868 KB |
Output is correct |
45 |
Correct |
199 ms |
75348 KB |
Output is correct |
46 |
Correct |
85 ms |
37460 KB |
Output is correct |
47 |
Correct |
199 ms |
55392 KB |
Output is correct |
48 |
Correct |
105 ms |
48368 KB |
Output is correct |
49 |
Correct |
83 ms |
48464 KB |
Output is correct |
50 |
Correct |
221 ms |
59360 KB |
Output is correct |
51 |
Correct |
89 ms |
47052 KB |
Output is correct |
52 |
Correct |
248 ms |
56484 KB |
Output is correct |
53 |
Correct |
119 ms |
49212 KB |
Output is correct |
54 |
Correct |
86 ms |
50388 KB |
Output is correct |
55 |
Correct |
168 ms |
57172 KB |
Output is correct |
56 |
Correct |
142 ms |
58196 KB |
Output is correct |
57 |
Correct |
139 ms |
59360 KB |
Output is correct |
58 |
Correct |
152 ms |
57420 KB |
Output is correct |
59 |
Correct |
167 ms |
57424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
16732 KB |
Output is correct |
2 |
Correct |
3 ms |
16732 KB |
Output is correct |
3 |
Correct |
4 ms |
16732 KB |
Output is correct |
4 |
Correct |
4 ms |
16728 KB |
Output is correct |
5 |
Correct |
8 ms |
18020 KB |
Output is correct |
6 |
Correct |
7 ms |
17660 KB |
Output is correct |
7 |
Correct |
7 ms |
17500 KB |
Output is correct |
8 |
Correct |
8 ms |
18168 KB |
Output is correct |
9 |
Correct |
8 ms |
17756 KB |
Output is correct |
10 |
Correct |
6 ms |
17404 KB |
Output is correct |
11 |
Correct |
6 ms |
17244 KB |
Output is correct |
12 |
Correct |
6 ms |
17664 KB |
Output is correct |
13 |
Correct |
5 ms |
17500 KB |
Output is correct |
14 |
Correct |
6 ms |
17244 KB |
Output is correct |
15 |
Correct |
6 ms |
17244 KB |
Output is correct |
16 |
Correct |
9 ms |
18524 KB |
Output is correct |
17 |
Correct |
8 ms |
17756 KB |
Output is correct |
18 |
Correct |
5 ms |
17244 KB |
Output is correct |
19 |
Correct |
7 ms |
17756 KB |
Output is correct |
20 |
Correct |
6 ms |
17496 KB |
Output is correct |
21 |
Correct |
6 ms |
17500 KB |
Output is correct |
22 |
Correct |
7 ms |
17756 KB |
Output is correct |
23 |
Correct |
7 ms |
17496 KB |
Output is correct |
24 |
Correct |
7 ms |
17756 KB |
Output is correct |
25 |
Correct |
6 ms |
17660 KB |
Output is correct |
26 |
Correct |
6 ms |
17656 KB |
Output is correct |
27 |
Correct |
7 ms |
17756 KB |
Output is correct |
28 |
Correct |
6 ms |
17844 KB |
Output is correct |
29 |
Correct |
6 ms |
17752 KB |
Output is correct |
30 |
Correct |
6 ms |
17756 KB |
Output is correct |
31 |
Correct |
7 ms |
17756 KB |
Output is correct |
32 |
Correct |
10 ms |
18012 KB |
Output is correct |
33 |
Correct |
301 ms |
85804 KB |
Output is correct |
34 |
Correct |
214 ms |
60784 KB |
Output is correct |
35 |
Correct |
279 ms |
83064 KB |
Output is correct |
36 |
Correct |
206 ms |
60948 KB |
Output is correct |
37 |
Correct |
148 ms |
41944 KB |
Output is correct |
38 |
Correct |
128 ms |
37536 KB |
Output is correct |
39 |
Correct |
133 ms |
50468 KB |
Output is correct |
40 |
Correct |
119 ms |
50400 KB |
Output is correct |
41 |
Correct |
81 ms |
50384 KB |
Output is correct |
42 |
Correct |
134 ms |
39372 KB |
Output is correct |
43 |
Correct |
117 ms |
38992 KB |
Output is correct |
44 |
Correct |
282 ms |
110868 KB |
Output is correct |
45 |
Correct |
199 ms |
75348 KB |
Output is correct |
46 |
Correct |
85 ms |
37460 KB |
Output is correct |
47 |
Correct |
199 ms |
55392 KB |
Output is correct |
48 |
Correct |
105 ms |
48368 KB |
Output is correct |
49 |
Correct |
83 ms |
48464 KB |
Output is correct |
50 |
Correct |
221 ms |
59360 KB |
Output is correct |
51 |
Correct |
89 ms |
47052 KB |
Output is correct |
52 |
Correct |
248 ms |
56484 KB |
Output is correct |
53 |
Correct |
119 ms |
49212 KB |
Output is correct |
54 |
Correct |
86 ms |
50388 KB |
Output is correct |
55 |
Correct |
168 ms |
57172 KB |
Output is correct |
56 |
Correct |
142 ms |
58196 KB |
Output is correct |
57 |
Correct |
139 ms |
59360 KB |
Output is correct |
58 |
Correct |
152 ms |
57420 KB |
Output is correct |
59 |
Correct |
167 ms |
57424 KB |
Output is correct |
60 |
Correct |
3 ms |
16728 KB |
Output is correct |
61 |
Correct |
3 ms |
16732 KB |
Output is correct |
62 |
Correct |
3 ms |
16884 KB |
Output is correct |
63 |
Correct |
4 ms |
16984 KB |
Output is correct |
64 |
Correct |
253 ms |
69308 KB |
Output is correct |
65 |
Correct |
207 ms |
52712 KB |
Output is correct |
66 |
Correct |
257 ms |
70024 KB |
Output is correct |
67 |
Correct |
192 ms |
52560 KB |
Output is correct |
68 |
Correct |
145 ms |
38732 KB |
Output is correct |
69 |
Correct |
131 ms |
35424 KB |
Output is correct |
70 |
Correct |
295 ms |
85584 KB |
Output is correct |
71 |
Correct |
194 ms |
61524 KB |
Output is correct |
72 |
Correct |
345 ms |
86244 KB |
Output is correct |
73 |
Correct |
181 ms |
61524 KB |
Output is correct |
74 |
Correct |
301 ms |
70012 KB |
Output is correct |
75 |
Correct |
196 ms |
51344 KB |
Output is correct |
76 |
Correct |
152 ms |
49744 KB |
Output is correct |
77 |
Correct |
321 ms |
77132 KB |
Output is correct |
78 |
Correct |
137 ms |
54740 KB |
Output is correct |
79 |
Correct |
326 ms |
89760 KB |
Output is correct |
80 |
Correct |
199 ms |
62548 KB |
Output is correct |
81 |
Correct |
150 ms |
52048 KB |
Output is correct |
82 |
Correct |
284 ms |
86700 KB |
Output is correct |
83 |
Correct |
196 ms |
195644 KB |
Output is correct |
84 |
Correct |
288 ms |
79444 KB |
Output is correct |
85 |
Correct |
268 ms |
79444 KB |
Output is correct |
86 |
Correct |
237 ms |
67396 KB |
Output is correct |
87 |
Correct |
263 ms |
73276 KB |
Output is correct |
88 |
Correct |
236 ms |
73180 KB |
Output is correct |