Submission #824440

# Submission time Handle Problem Language Result Execution time Memory
824440 2023-08-14T06:22:17 Z 반딧불(#10358) Winter Driving (CCO19_day1problem3) C++17
15 / 25
123 ms 32344 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int n;
ll arr[200002];
int par[200002];
vector<int> link[200002];

ll sz[200002], bonus[200002];

void dfs(int x, int p){
	sz[x] = arr[x];
	bonus[x] = 0;
	for(auto y: link[x]){
		if(y==p) continue;
		dfs(y, x);
		sz[x] += sz[y];
		bonus[x] += bonus[y];
	}
	bonus[x] += (sz[x] - arr[x]) * arr[x];
}

ll ans;

int main(){
    scanf("%d", &n);
    for(int i=1; i<=n; i++) scanf("%lld", &arr[i]);
    for(int i=2; i<=n; i++) scanf("%d", &par[i]), link[par[i]].push_back(i), link[i].push_back(par[i]);
    ll sum = accumulate(arr+1, arr+n+1, 0LL);
	ll base = 0;
	for(int i=1; i<=n; i++) base += arr[i] * (arr[i]-1);

	dfs(1, -1);

	vector<int> centroids;
	for(int i=1; i<=n; i++){
		bool good = 1;
		if(sz[i] < (sum+1)/2) continue;
		for(int j: link[i]){
			if(j==par[i]) continue;
			if(sz[j]*2 > sum) good = 0;
		}
		if(good) centroids.push_back(i);
	}
	//printf("Centroids: "); for(int x: centroids) printf("%d ", x);
	//puts("");

    for(int i: centroids){
    	dfs(i, -1);
    	vector<ll> v;
    	ll b = bonus[i];
    	for(int j: link[i]) v.push_back(sz[j]);

    	for(int d=0; d<(1<<(int)link[i].size()); d++){
    		ll a = 0;
    		for(int j=0; j<(int)link[i].size(); j++){
    			if((d>>j)&1) a += v[j];
    		}
    		//printf("%d %d: %lld\n", i, d, a * (sum-arr[i]-a) + b);
    		ans = max(ans, a * (sum-arr[i]-a) + b);
    	}
    }
	printf("%lld", ans + base);
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:29:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   29 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
Main.cpp:30:34: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |     for(int i=1; i<=n; i++) scanf("%lld", &arr[i]);
      |                             ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:31:34: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 |     for(int i=2; i<=n; i++) scanf("%d", &par[i]), link[par[i]].push_back(i), link[i].push_back(par[i]);
      |                             ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4912 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 2 ms 4948 KB Output is correct
15 Correct 2 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 2 ms 4948 KB Output is correct
18 Correct 2 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 5076 KB Output is correct
7 Correct 2 ms 4952 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 2 ms 5076 KB Output is correct
14 Correct 2 ms 5076 KB Output is correct
15 Correct 2 ms 5076 KB Output is correct
16 Correct 2 ms 5076 KB Output is correct
17 Correct 2 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 5076 KB Output is correct
7 Correct 2 ms 4952 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 2 ms 5076 KB Output is correct
14 Correct 2 ms 5076 KB Output is correct
15 Correct 2 ms 5076 KB Output is correct
16 Correct 2 ms 5076 KB Output is correct
17 Correct 2 ms 5076 KB Output is correct
18 Correct 123 ms 16132 KB Output is correct
19 Correct 94 ms 15016 KB Output is correct
20 Correct 122 ms 16112 KB Output is correct
21 Correct 95 ms 16212 KB Output is correct
22 Correct 100 ms 16212 KB Output is correct
23 Correct 80 ms 16248 KB Output is correct
24 Correct 69 ms 14392 KB Output is correct
25 Correct 60 ms 14420 KB Output is correct
26 Correct 71 ms 14432 KB Output is correct
27 Correct 73 ms 14432 KB Output is correct
28 Correct 96 ms 15604 KB Output is correct
29 Correct 100 ms 16724 KB Output is correct
30 Correct 60 ms 17968 KB Output is correct
31 Correct 63 ms 18004 KB Output is correct
32 Correct 61 ms 17972 KB Output is correct
33 Correct 62 ms 17992 KB Output is correct
34 Correct 61 ms 18004 KB Output is correct
35 Correct 61 ms 17924 KB Output is correct
36 Correct 62 ms 18000 KB Output is correct
37 Correct 61 ms 17940 KB Output is correct
38 Correct 72 ms 32344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 4948 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4912 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 2 ms 4948 KB Output is correct
15 Correct 2 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 2 ms 4948 KB Output is correct
18 Correct 2 ms 4948 KB Output is correct
19 Correct 2 ms 4948 KB Output is correct
20 Correct 2 ms 4948 KB Output is correct
21 Correct 2 ms 4948 KB Output is correct
22 Correct 2 ms 5076 KB Output is correct
23 Correct 2 ms 4948 KB Output is correct
24 Correct 2 ms 5076 KB Output is correct
25 Correct 2 ms 4952 KB Output is correct
26 Correct 2 ms 4948 KB Output is correct
27 Correct 2 ms 4948 KB Output is correct
28 Correct 2 ms 4948 KB Output is correct
29 Correct 2 ms 5076 KB Output is correct
30 Correct 3 ms 4948 KB Output is correct
31 Correct 2 ms 5076 KB Output is correct
32 Correct 2 ms 5076 KB Output is correct
33 Correct 2 ms 5076 KB Output is correct
34 Correct 2 ms 5076 KB Output is correct
35 Correct 2 ms 5076 KB Output is correct
36 Correct 123 ms 16132 KB Output is correct
37 Correct 94 ms 15016 KB Output is correct
38 Correct 122 ms 16112 KB Output is correct
39 Correct 95 ms 16212 KB Output is correct
40 Correct 100 ms 16212 KB Output is correct
41 Correct 80 ms 16248 KB Output is correct
42 Correct 69 ms 14392 KB Output is correct
43 Correct 60 ms 14420 KB Output is correct
44 Correct 71 ms 14432 KB Output is correct
45 Correct 73 ms 14432 KB Output is correct
46 Correct 96 ms 15604 KB Output is correct
47 Correct 100 ms 16724 KB Output is correct
48 Correct 60 ms 17968 KB Output is correct
49 Correct 63 ms 18004 KB Output is correct
50 Correct 61 ms 17972 KB Output is correct
51 Correct 62 ms 17992 KB Output is correct
52 Correct 61 ms 18004 KB Output is correct
53 Correct 61 ms 17924 KB Output is correct
54 Correct 62 ms 18000 KB Output is correct
55 Correct 61 ms 17940 KB Output is correct
56 Correct 72 ms 32344 KB Output is correct
57 Incorrect 2 ms 4948 KB Output isn't correct
58 Halted 0 ms 0 KB -