This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |