답안 #824450

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
824450 2023-08-14T06:29:11 Z 반딧불(#10358) Winter Driving (CCO19_day1problem3) C++17
25 / 25
124 ms 32316 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);
	}

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

    	int S = (int)v.size();
    	if(S < 2){
	    	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];
	    		}
	    		ans = max(ans, a * (sum-arr[i]-a) + b);
	    	}
	    }
	    else{
	    	ll total = sum - arr[i];
	    	int A = S/2, B = S-A;
	    	vector<ll> v1, v2;
	    	for(int d=0; d<(1<<A); d++){
	    		ll a = 0;
	    		for(int j=0; j<A; j++) if((d>>j)&1) a += v[j];
	    		v1.push_back(a);
	    	}
	    	for(int d=0; d<(1<<B); d++){
	    		ll a = 0;
	    		for(int j=0; j<B; j++) if((d>>j)&1) a += v[j+A];
	    		v2.push_back(a);
	    	}
	    	sort(v1.begin(), v1.end());
	    	sort(v2.begin(), v2.end());
	    	for(ll a: v1){
	    		ll y = (total+1)/2 - a;
	    		auto it = lower_bound(v2.begin(), v2.end(), y);
	    		if(it != v2.end()) ans = max(ans, (a+*it) * (total-a-*it) + b);
	    		if(it != v2.begin()){
	    			--it;
	    			ans = max(ans, (a+*it) * (total-a-*it) + 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]);
      |                             ~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4992 KB Output is correct
2 Correct 2 ms 4944 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 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 2 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 4976 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 2 ms 4952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4952 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5036 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 2 ms 5076 KB Output is correct
9 Correct 2 ms 5052 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 2 ms 5048 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
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4952 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 5036 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 2 ms 5076 KB Output is correct
9 Correct 2 ms 5052 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 2 ms 5048 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 80 ms 16084 KB Output is correct
19 Correct 69 ms 15052 KB Output is correct
20 Correct 93 ms 16148 KB Output is correct
21 Correct 76 ms 16104 KB Output is correct
22 Correct 86 ms 16156 KB Output is correct
23 Correct 99 ms 16248 KB Output is correct
24 Correct 69 ms 14360 KB Output is correct
25 Correct 61 ms 14412 KB Output is correct
26 Correct 62 ms 14400 KB Output is correct
27 Correct 81 ms 14464 KB Output is correct
28 Correct 75 ms 15564 KB Output is correct
29 Correct 84 ms 16668 KB Output is correct
30 Correct 48 ms 17904 KB Output is correct
31 Correct 49 ms 17912 KB Output is correct
32 Correct 49 ms 18012 KB Output is correct
33 Correct 48 ms 17976 KB Output is correct
34 Correct 48 ms 18004 KB Output is correct
35 Correct 49 ms 17888 KB Output is correct
36 Correct 52 ms 17996 KB Output is correct
37 Correct 48 ms 17888 KB Output is correct
38 Correct 69 ms 32268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 81 ms 10160 KB Output is correct
2 Correct 77 ms 10052 KB Output is correct
3 Correct 78 ms 10052 KB Output is correct
4 Correct 75 ms 10140 KB Output is correct
5 Correct 76 ms 10124 KB Output is correct
6 Correct 76 ms 10052 KB Output is correct
7 Correct 73 ms 10220 KB Output is correct
8 Correct 76 ms 10136 KB Output is correct
9 Correct 76 ms 10100 KB Output is correct
10 Correct 75 ms 10060 KB Output is correct
11 Correct 76 ms 10160 KB Output is correct
12 Correct 75 ms 10092 KB Output is correct
13 Correct 75 ms 10112 KB Output is correct
14 Correct 76 ms 10052 KB Output is correct
15 Correct 76 ms 10036 KB Output is correct
16 Correct 75 ms 10168 KB Output is correct
17 Correct 75 ms 10060 KB Output is correct
18 Correct 75 ms 10044 KB Output is correct
19 Correct 75 ms 10124 KB Output is correct
20 Correct 80 ms 10184 KB Output is correct
21 Correct 76 ms 10056 KB Output is correct
22 Correct 79 ms 10088 KB Output is correct
23 Correct 75 ms 10048 KB Output is correct
24 Correct 76 ms 10096 KB Output is correct
25 Correct 76 ms 10064 KB Output is correct
26 Correct 75 ms 10080 KB Output is correct
27 Correct 76 ms 10108 KB Output is correct
28 Correct 77 ms 10128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4992 KB Output is correct
2 Correct 2 ms 4944 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 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 2 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 4976 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 2 ms 4952 KB Output is correct
19 Correct 4 ms 4952 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 5036 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 2 ms 4948 KB Output is correct
24 Correct 3 ms 4948 KB Output is correct
25 Correct 3 ms 4948 KB Output is correct
26 Correct 2 ms 5076 KB Output is correct
27 Correct 2 ms 5052 KB Output is correct
28 Correct 2 ms 4948 KB Output is correct
29 Correct 2 ms 5076 KB Output is correct
30 Correct 2 ms 5048 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 80 ms 16084 KB Output is correct
37 Correct 69 ms 15052 KB Output is correct
38 Correct 93 ms 16148 KB Output is correct
39 Correct 76 ms 16104 KB Output is correct
40 Correct 86 ms 16156 KB Output is correct
41 Correct 99 ms 16248 KB Output is correct
42 Correct 69 ms 14360 KB Output is correct
43 Correct 61 ms 14412 KB Output is correct
44 Correct 62 ms 14400 KB Output is correct
45 Correct 81 ms 14464 KB Output is correct
46 Correct 75 ms 15564 KB Output is correct
47 Correct 84 ms 16668 KB Output is correct
48 Correct 48 ms 17904 KB Output is correct
49 Correct 49 ms 17912 KB Output is correct
50 Correct 49 ms 18012 KB Output is correct
51 Correct 48 ms 17976 KB Output is correct
52 Correct 48 ms 18004 KB Output is correct
53 Correct 49 ms 17888 KB Output is correct
54 Correct 52 ms 17996 KB Output is correct
55 Correct 48 ms 17888 KB Output is correct
56 Correct 69 ms 32268 KB Output is correct
57 Correct 81 ms 10160 KB Output is correct
58 Correct 77 ms 10052 KB Output is correct
59 Correct 78 ms 10052 KB Output is correct
60 Correct 75 ms 10140 KB Output is correct
61 Correct 76 ms 10124 KB Output is correct
62 Correct 76 ms 10052 KB Output is correct
63 Correct 73 ms 10220 KB Output is correct
64 Correct 76 ms 10136 KB Output is correct
65 Correct 76 ms 10100 KB Output is correct
66 Correct 75 ms 10060 KB Output is correct
67 Correct 76 ms 10160 KB Output is correct
68 Correct 75 ms 10092 KB Output is correct
69 Correct 75 ms 10112 KB Output is correct
70 Correct 76 ms 10052 KB Output is correct
71 Correct 76 ms 10036 KB Output is correct
72 Correct 75 ms 10168 KB Output is correct
73 Correct 75 ms 10060 KB Output is correct
74 Correct 75 ms 10044 KB Output is correct
75 Correct 75 ms 10124 KB Output is correct
76 Correct 80 ms 10184 KB Output is correct
77 Correct 76 ms 10056 KB Output is correct
78 Correct 79 ms 10088 KB Output is correct
79 Correct 75 ms 10048 KB Output is correct
80 Correct 76 ms 10096 KB Output is correct
81 Correct 76 ms 10064 KB Output is correct
82 Correct 75 ms 10080 KB Output is correct
83 Correct 76 ms 10108 KB Output is correct
84 Correct 77 ms 10128 KB Output is correct
85 Correct 90 ms 16068 KB Output is correct
86 Correct 65 ms 15060 KB Output is correct
87 Correct 86 ms 16184 KB Output is correct
88 Correct 86 ms 16220 KB Output is correct
89 Correct 96 ms 16228 KB Output is correct
90 Correct 79 ms 16140 KB Output is correct
91 Correct 64 ms 14352 KB Output is correct
92 Correct 63 ms 14352 KB Output is correct
93 Correct 60 ms 14424 KB Output is correct
94 Correct 67 ms 14524 KB Output is correct
95 Correct 74 ms 15600 KB Output is correct
96 Correct 98 ms 16724 KB Output is correct
97 Correct 116 ms 23096 KB Output is correct
98 Correct 115 ms 23128 KB Output is correct
99 Correct 116 ms 23140 KB Output is correct
100 Correct 123 ms 23140 KB Output is correct
101 Correct 115 ms 23096 KB Output is correct
102 Correct 114 ms 23080 KB Output is correct
103 Correct 119 ms 23200 KB Output is correct
104 Correct 115 ms 23144 KB Output is correct
105 Correct 115 ms 23196 KB Output is correct
106 Correct 117 ms 23168 KB Output is correct
107 Correct 117 ms 23160 KB Output is correct
108 Correct 124 ms 23132 KB Output is correct
109 Correct 116 ms 23100 KB Output is correct
110 Correct 116 ms 23184 KB Output is correct
111 Correct 117 ms 23204 KB Output is correct
112 Correct 113 ms 23100 KB Output is correct
113 Correct 72 ms 32316 KB Output is correct