Submission #824389

# Submission time Handle Problem Language Result Execution time Memory
824389 2023-08-14T05:08:59 Z 이동현(#10361) Winter Driving (CCO19_day1problem3) C++17
0 / 25
202 ms 488 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{
			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;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:93:6: warning: unused variable 'ct' [-Wunused-variable]
   93 |  int ct = getcent(getcent, 0, -1);
      |      ^~
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 202 ms 488 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 202 ms 488 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 10 ms 448 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -