#include <stdio.h>
#define N 1000000
#define INF 0x3f3f3f3f3f3f3f3f
long long max(long long a, long long b) { return a > b ? a : b; }
int main() {
static int pp[N], ww[N], in[N];
static long long dd1[N], dd2[N];
int n, i, j;
long long ans;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d%d", &pp[i], &ww[i]), pp[i]--;
in[pp[i]]++;
}
for (i = 0; i < n; i++)
for (j = i; in[j] == 0; j = pp[j]) {
int p = pp[j];
dd2[p] = max(max(dd2[p], dd2[j]), dd1[p] + dd1[j] + ww[j]);
dd1[p] = max(dd1[p], dd1[j] + ww[j]);
in[p]--;
}
ans = 0;
for (i = 0; i < n; i++)
if (in[i] != 0) {
long long d, e, f, w, x, y;
d = e = f = -INF, w = 0, x = y = -INF, j = i;
for (j = i; in[j] != 0; j = pp[j]) {
in[j] = 0;
d = max(d, dd2[j]);
if (x != -INF)
e = max(e, x + dd1[j]);
if (y != -INF)
f = max(f, y + dd1[j]);
w += ww[j], x = max(x, dd1[j]) + ww[j], y = max(y, dd1[j]) - ww[j];
}
ans += max(d, max(e, f + w));
}
printf("%lld\n", ans);
return 0;
}
Compilation message
islands.c: In function 'main':
islands.c:14:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
14 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
islands.c:16:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | scanf("%d%d", &pp[i], &ww[i]), pp[i]--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
284 KB |
Output isn't correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
844 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
16 ms |
2296 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
53 ms |
8128 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
100 ms |
15288 KB |
Output is correct |
2 |
Incorrect |
121 ms |
19660 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
253 ms |
23604 KB |
Output is correct |
2 |
Correct |
287 ms |
33156 KB |
Output is correct |
3 |
Correct |
211 ms |
31756 KB |
Output is correct |
4 |
Correct |
253 ms |
40260 KB |
Output is correct |
5 |
Incorrect |
248 ms |
39788 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
276 ms |
43032 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |