Submission #824428

# Submission time Handle Problem Language Result Execution time Memory
824428 2023-08-14T06:09:12 Z 반딧불(#10358) Winter Driving (CCO19_day1problem3) C++17
10 / 25
1000 ms 16100 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);

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

    	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];
    		}
    		//printf("%d %d: %lld\n", i, d, a * (sum-arr[i]-a) + b);
    		ans = max(ans, a * (sum-arr[i]-a) + 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]);
      |                             ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 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 3 ms 4952 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 3 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 4948 KB Output is correct
17 Correct 2 ms 4948 KB Output is correct
18 Correct 2 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 4948 KB Output is correct
2 Correct 4 ms 4948 KB Output is correct
3 Correct 7 ms 4948 KB Output is correct
4 Correct 10 ms 5076 KB Output is correct
5 Correct 8 ms 4948 KB Output is correct
6 Correct 9 ms 5076 KB Output is correct
7 Correct 5 ms 4948 KB Output is correct
8 Correct 8 ms 4948 KB Output is correct
9 Correct 5 ms 5028 KB Output is correct
10 Correct 8 ms 5064 KB Output is correct
11 Correct 13 ms 5080 KB Output is correct
12 Correct 6 ms 4948 KB Output is correct
13 Correct 14 ms 5080 KB Output is correct
14 Correct 14 ms 5088 KB Output is correct
15 Correct 16 ms 5308 KB Output is correct
16 Correct 14 ms 5080 KB Output is correct
17 Correct 11 ms 5152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 4948 KB Output is correct
2 Correct 4 ms 4948 KB Output is correct
3 Correct 7 ms 4948 KB Output is correct
4 Correct 10 ms 5076 KB Output is correct
5 Correct 8 ms 4948 KB Output is correct
6 Correct 9 ms 5076 KB Output is correct
7 Correct 5 ms 4948 KB Output is correct
8 Correct 8 ms 4948 KB Output is correct
9 Correct 5 ms 5028 KB Output is correct
10 Correct 8 ms 5064 KB Output is correct
11 Correct 13 ms 5080 KB Output is correct
12 Correct 6 ms 4948 KB Output is correct
13 Correct 14 ms 5080 KB Output is correct
14 Correct 14 ms 5088 KB Output is correct
15 Correct 16 ms 5308 KB Output is correct
16 Correct 14 ms 5080 KB Output is correct
17 Correct 11 ms 5152 KB Output is correct
18 Execution timed out 1081 ms 16100 KB Time limit exceeded
19 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 4948 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 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 3 ms 4952 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 3 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 4948 KB Output is correct
17 Correct 2 ms 4948 KB Output is correct
18 Correct 2 ms 4948 KB Output is correct
19 Correct 8 ms 4948 KB Output is correct
20 Correct 4 ms 4948 KB Output is correct
21 Correct 7 ms 4948 KB Output is correct
22 Correct 10 ms 5076 KB Output is correct
23 Correct 8 ms 4948 KB Output is correct
24 Correct 9 ms 5076 KB Output is correct
25 Correct 5 ms 4948 KB Output is correct
26 Correct 8 ms 4948 KB Output is correct
27 Correct 5 ms 5028 KB Output is correct
28 Correct 8 ms 5064 KB Output is correct
29 Correct 13 ms 5080 KB Output is correct
30 Correct 6 ms 4948 KB Output is correct
31 Correct 14 ms 5080 KB Output is correct
32 Correct 14 ms 5088 KB Output is correct
33 Correct 16 ms 5308 KB Output is correct
34 Correct 14 ms 5080 KB Output is correct
35 Correct 11 ms 5152 KB Output is correct
36 Execution timed out 1081 ms 16100 KB Time limit exceeded
37 Halted 0 ms 0 KB -