#include <bits/stdc++.h>
#include <cassert>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define MAX 2020
#define MAXS 20
#define INF 1000000010
#define bb ' '
#define ln '\n'
#define Ln '\n'
#define MOD 998244353
vector<int> adj[MAX];
int mask;
int A[MAX];
int dfs(int x, int p = 0) {
int ans = A[x];
for (auto v : adj[x]) {
if (v == p) continue;
int mv = max(x, v);
if (mask >> (mv - 2) & 1) {
if (mv == v) continue;
}
else {
if (mv == x) continue;
}
ans += dfs(v, x);
}
return ans;
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
int N;
cin >> N;
int i, p;
for (i = 1; i <= N; i++) cin >> A[i];
for (i = 2; i <= N; i++) cin >> p, adj[p].push_back(i), adj[i].push_back(p);
int mx = 0;
for (mask = 0; mask < 1 << (N - 1); mask++) {
int sum = 0;
for (i = 1; i <= N; i++) sum += A[i] * (dfs(i) - A[i]);
mx = max(mx, sum);
}
for (i = 1; i <= N; i++) mx += (A[i] - 1) * A[i];
cout << mx;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
272 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
372 KB |
Output is correct |
7 |
Correct |
1 ms |
372 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 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1070 ms |
376 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1070 ms |
376 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 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 |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
272 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
372 KB |
Output is correct |
7 |
Correct |
1 ms |
372 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 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |