Submission #824385

# Submission time Handle Problem Language Result Execution time Memory
824385 2023-08-14T05:06:52 Z 이동현(#10361) Winter Driving (CCO19_day1problem3) C++17
10 / 25
1 ms 468 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){
		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]);
			}
		};
		
		vector<int> vc;
		for(int i = 0; i < (int)way[rt].size(); ++i){
			cnt = 0;
			dfs(dfs, way[rt][i], rt, a[rt]);

			vc.push_back(cnt);
		}

		bitset<360004> dp;
		dp[0] = 1;
		for(auto&i:vc){
			dp |= (dp << i);
		}

		int mx = 0;
		int szsum = accumulate(vc.begin(), vc.end(), 0ll);
		for(int i = 0; i < (int)dp.size(); ++i){
			if(dp[i]){
				mx = max(mx, i * (szsum - i));
			}
		}

		ans = allsum + mx;
	};

	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 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 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 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 352 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 468 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 468 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 452 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 448 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 448 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 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 1 ms 340 KB Output is correct
26 Correct 1 ms 444 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 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 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 352 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Incorrect 1 ms 468 KB Output isn't correct
20 Halted 0 ms 0 KB -