Submission #824381

# Submission time Handle Problem Language Result Execution time Memory
824381 2023-08-14T05:03:23 Z 이동현(#10361) Winter Driving (CCO19_day1problem3) C++17
10 / 25
1000 ms 14592 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define int long long
using namespace std;

signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	int n;
	cin >> n;
	vector<int> a(n);
	for(int i = 0; i < n; ++i){
		cin >> a[i];
	}
	vector<vector<int>> way(n);
	for(int i = 1; i < n; ++i){
		int x;
		cin >> x;
		--x;
		way[x].push_back(i);
		way[i].push_back(x);
	}

	int ans = 0;
	auto sol = [&](int rt){
		for(int i = 0; i < (1 << (int)way[rt].size()); ++i){
			// cout << rt << ' ' << i << endl;
			vector<int> sum(2);

			int allsum = 0, cnt = 0;

			auto dfs = [&](auto&&self, int x, int pr, int psum)->void{
				// cout << rt << ' ' << x << ' ' << pr << endl;
				allsum += a[x] * psum;
				cnt += a[x];
				for(auto&nxt:way[x]){
					if(nxt == pr){
						continue;
					}

					self(self, nxt, x, psum + a[x]);
				}
			};

			for(int j = 0; j < (int)way[rt].size(); ++j){
				cnt = 0;
				dfs(dfs, way[rt][j], rt, a[rt]);

				sum[!!(i & 1 << j)] += cnt;
			}

			ans = max(ans, allsum + sum[0] * sum[1]);
		}
	};

	vector<int> sz(n);

	auto dfssz = [&](auto&&self, int x, int pr)->void{
		sz[x] = a[x];
		for(auto&nxt:way[x]){
			if(nxt == pr){
				continue;
			}
			self(self, nxt, x);
			sz[x] += sz[nxt];
		}
	};
	dfssz(dfssz, 0, -1);

	auto getcent = [&](auto&&self, int x, int pr)->int{
		for(auto&nxt:way[x]){
			if(nxt == pr){
				continue;
			}
			if(sz[nxt] * 2 > sz[0]){
				return self(self, nxt, x);
			}
		}
		return x;
	};
	int ct = getcent(getcent, 0, -1);

	sol(ct);
	// for(int i = 0; i < n; ++i){
	// 	sol(i);
	// }

	for(int i = 0; i < n; ++i){
		ans += a[i] * (a[i] - 1);
	}

	cout << ans << '\n';
	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 5 ms 380 KB Output is correct
4 Correct 4 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 3 ms 340 KB Output is correct
8 Correct 6 ms 380 KB Output is correct
9 Correct 1 ms 356 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 7 ms 340 KB Output is correct
14 Correct 7 ms 408 KB Output is correct
15 Correct 7 ms 340 KB Output is correct
16 Correct 7 ms 412 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 5 ms 380 KB Output is correct
4 Correct 4 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 3 ms 340 KB Output is correct
8 Correct 6 ms 380 KB Output is correct
9 Correct 1 ms 356 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 7 ms 340 KB Output is correct
14 Correct 7 ms 408 KB Output is correct
15 Correct 7 ms 340 KB Output is correct
16 Correct 7 ms 412 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Execution timed out 1072 ms 14592 KB Time limit exceeded
19 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 5 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 5 ms 380 KB Output is correct
22 Correct 4 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 3 ms 340 KB Output is correct
26 Correct 6 ms 380 KB Output is correct
27 Correct 1 ms 356 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 2 ms 340 KB Output is correct
31 Correct 7 ms 340 KB Output is correct
32 Correct 7 ms 408 KB Output is correct
33 Correct 7 ms 340 KB Output is correct
34 Correct 7 ms 412 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Execution timed out 1072 ms 14592 KB Time limit exceeded
37 Halted 0 ms 0 KB -