#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[j] = -1, 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]--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
7 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
8 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
9 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
10 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
716 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
1612 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
52 ms |
5700 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
104 ms |
9956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
267 ms |
12088 KB |
Output is correct |
2 |
Correct |
303 ms |
19780 KB |
Output is correct |
3 |
Incorrect |
238 ms |
20984 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
268 ms |
27640 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |