답안 #824390

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
824390 2023-08-14T05:10:01 Z 이동현(#10361) Winter Driving (CCO19_day1problem3) C++17
10 / 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 = max(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);
      |      ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 3 ms 340 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 3 ms 340 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 3 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 3 ms 340 KB Output is correct
14 Correct 3 ms 444 KB Output is correct
15 Correct 3 ms 448 KB Output is correct
16 Correct 3 ms 340 KB Output is correct
17 Correct 3 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 202 ms 468 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 202 ms 468 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 444 KB Output is correct
2 Correct 14 ms 452 KB Output is correct
3 Correct 10 ms 448 KB Output is correct
4 Correct 10 ms 448 KB Output is correct
5 Correct 10 ms 448 KB Output is correct
6 Correct 10 ms 340 KB Output is correct
7 Correct 10 ms 340 KB Output is correct
8 Correct 10 ms 340 KB Output is correct
9 Correct 10 ms 340 KB Output is correct
10 Correct 10 ms 340 KB Output is correct
11 Correct 10 ms 340 KB Output is correct
12 Correct 10 ms 440 KB Output is correct
13 Correct 10 ms 444 KB Output is correct
14 Correct 10 ms 340 KB Output is correct
15 Correct 10 ms 340 KB Output is correct
16 Correct 10 ms 444 KB Output is correct
17 Correct 10 ms 452 KB Output is correct
18 Correct 10 ms 340 KB Output is correct
19 Correct 10 ms 448 KB Output is correct
20 Correct 10 ms 448 KB Output is correct
21 Correct 10 ms 340 KB Output is correct
22 Correct 10 ms 340 KB Output is correct
23 Correct 10 ms 448 KB Output is correct
24 Correct 10 ms 488 KB Output is correct
25 Correct 10 ms 452 KB Output is correct
26 Correct 10 ms 448 KB Output is correct
27 Correct 10 ms 444 KB Output is correct
28 Correct 10 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 3 ms 340 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 3 ms 340 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 3 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 3 ms 340 KB Output is correct
14 Correct 3 ms 444 KB Output is correct
15 Correct 3 ms 448 KB Output is correct
16 Correct 3 ms 340 KB Output is correct
17 Correct 3 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Incorrect 202 ms 468 KB Output isn't correct
20 Halted 0 ms 0 KB -