QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#648092 | #6423. Fireworks | shabi666# | RE | 0ms | 0kb | C++20 | 1.8kb | 2024-10-17 16:59:38 | 2024-10-17 16:59:39 |
answer
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N = 2e3 + 5;
vector<int> edges[N];
int f[N][N][2], g[N];
int cnt[N];
int sz[N];
int hp[N];
void dfs0(int u) {
g[u] = hp[u];
sz[u] = 1;
for (int v : edges[u]) {
g[u] += hp[v];
}
for (int v : edges[u]) {
dfs0(v);
sz[u] += sz[v];
g[u] += g[v];
}
}
void dfs(int u, int n) {
cnt[u] = 1;
for (int i = 0; i <= n; i++) {
f[u][i][0] = f[u][i][1] = 1e18;
}
f[u][0][0] = g[u];
f[u][sz[u]][1] = 0;
f[u][sz[u] - 1][0] = hp[u];
for (int v : edges[u]) {
dfs(v, n);
for (int j = 0; j <= cnt[u] + cnt[v]; j++) {
for (int k = 0; k <= min(cnt[v], j); k++) {
if (j - k - 1 >= 0)
f[u][j][1] = min(f[u][j][1], f[u][j - k - 1][1] + min(f[v][k][0] + hp[v], f[v][k][1]));
f[u][j][0] = min(f[u][j][0], f[u][j - k][0] + min(f[v][k][0] + hp[v], f[v][k][1]));
}
}
cnt[u] += cnt[v];
}
}
void solve() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
edges[i].clear();
for (int j = 0; j <= n; j++) {
f[i][j][0] = f[i][j][1] = 0;
}
}
for (int i = 2; i <= n; i++) {
int p;
cin >> p;
edges[p].emplace_back(i);
}
for (int i = 1; i <= n; i++) {
cin >> hp[i];
}
dfs0(1);
dfs(1, n);
for (int i = 0; i <= n; i++) {
cout << min(f[1][i][0], f[1][i][1]) << " \n"[i == n];
}
}
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int T = 1;
cin >> T;
while (T--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
3 1 1 5000 1 1 1 1 2 10000