Submission #722692

# Submission time Handle Problem Language Result Execution time Memory
722692 2023-04-12T16:38:03 Z rainboy Winter Driving (CCO19_day1problem3) C
0 / 25
10 ms 340 KB
#include <stdio.h>
#include <stdlib.h>

#define N	200000

long long max(long long a, long long b) { return a > b ? a : b; }

int *ej[N], eo[N];

void append(int i, int j) {
	int o = eo[i]++;

	if (o >= 2 && (o & o - 1) == 0)
		ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
	ej[i][o] = j;
}

int aa[N]; long long dp[N], dq[N];

void dfs(int p, int i) {
	int o;

	dp[i] = aa[i], dq[i] = 0;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p) {
			dfs(i, j);
			dp[i] += dp[j], dq[i] += dq[j];
		}
	}
	dq[i] += aa[i] * dp[i];
}

int main() {
	int n, i, j, o, b;
	long long ans, sum1, sum2;

	scanf("%d", &n);
	for (i = 0; i < n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]);
	for (j = 1; j < n; j++) {
		scanf("%d", &i), i--;
		append(i, j), append(j, i);
	}
	ans = 0;
	for (i = 0; i < n; i++) {
		dfs(-1, i);
		for (b = 0; b < 1 << eo[i]; b++) {
			sum1 = sum2 = 0;
			for (o = 0; o < eo[i]; o++) {
				j = ej[i][o];
				if ((b & 1 << j) == 0)
					sum1 += dp[j];
				else
					sum2 += dp[j];
			}
			ans = max(ans, dq[i] + (long long) sum1 * sum2);
		}
	}
	for (i = 0; i < n; i++)
		ans -= aa[i];
	printf("%lld\n", ans);
	return 0;
}

Compilation message

Main.c: In function 'append':
Main.c:13:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   13 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
Main.c: In function 'main':
Main.c:39:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
Main.c:43:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   43 |   scanf("%d", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~
Main.c:45:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   45 |   scanf("%d", &i), i--;
      |   ^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 288 KB Output is correct
2 Correct 1 ms 296 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 300 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Incorrect 0 ms 292 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 10 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 10 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 288 KB Output is correct
2 Correct 1 ms 296 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 300 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Incorrect 0 ms 292 KB Output isn't correct
8 Halted 0 ms 0 KB -