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>
#include <string.h>
#define N 100000
#define INF 0x3f3f3f3f
int min(int a, int b) { return a < b ? a : b; }
int main() {
static int pp[N], cc1[N], cc2[N], jj[N];
static char visited[N];
int n, i, j, c, x;
long long ans;
scanf("%d", &n);
memset(jj, -1, n * sizeof *jj);
ans = 0;
for (j = 0; j < n; j++) {
scanf("%d%d", &i, &c), i--;
pp[j] = i;
if (cc1[i] < c)
cc2[i] = cc1[i], cc1[i] = c, jj[i] = j;
else if (cc2[i] < c)
cc2[i] = c;
ans += c;
}
for (i = 0; i < n; i++)
ans -= cc1[i];
for (i = 0; i < n; i++)
if (!visited[i]) {
j = i;
while (!visited[j])
visited[j] = 1, j = pp[j];
if (visited[j] == 1) {
x = INF, c = 0;
while (visited[j] == 1) {
if (jj[pp[j]] != j)
x = 0;
else
x = min(x, cc1[pp[j]] - cc2[pp[j]]);
visited[j] = 2, j = pp[j], c++;
}
if (c == n) {
ans = 0;
break;
}
ans += x;
}
j = i;
while (visited[j] != 3)
visited[j] = 3, j = pp[j];
}
printf("%lld\n", ans);
return 0;
}
Compilation message (stderr)
telegraph.c: In function 'main':
telegraph.c:15:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
telegraph.c:19:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | scanf("%d%d", &i, &c), 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... |