QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#150682 | #4815. Flower's Land | 08kevin | WA | 5ms | 18768kb | C++17 | 2.1kb | 2023-08-26 00:29:23 | 2023-08-26 00:29:26 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, m;
ll a[200100];
ll d[200100];
ll p[200100];
ll ans[200100];
ll s1[41000][3010];
ll s2[41000][3010];
ll sz[200100];
vector<ll> g[200100];
void dfs1(ll u, ll fa) {
sz[u] = 1;
s1[u][1] = a[u];
for (ll i = 0; i < g[u].size(); i++) {
ll v = g[u][i];
if (v == fa) {
continue;
}
dfs1(v, u);
for (ll j = 0; j <= m; j++) {
d[j] = -1e18;
s2[v][j] = s1[u][j];
}
for (ll j = 1; j <= min(m, sz[u]); j++) {
for (ll k = 0; k <= min(m - j, sz[v]); k++) {
d[j + k] = max(d[j + k], s1[u][j] + s1[v][k]);
}
}
sz[u] += sz[v];
}
s1[u][0] = 0;
return;
}
void dfs2(ll u, ll fa) {
ll x = sz[u];
s2[u][m] = 0;
for (ll i = 0; i <= m; i++) {
p[i] = s2[u][i];
}
reverse(g[u].begin(), g[u].end());
for (ll i = 0; i < g[u].size(); i++) {
ll v = g[u][i];
if (v == fa) {
continue;
}
x -= sz[v];
for (ll j = 0; j <= m; j++) {
d[j] = -1e18;
}
for (ll j = 1; j <= min(m, sz[v]); j++) {
for (ll k = 0; k <= min(m - j, x); k++) {
d[j] = max(d[j], p[j + k] + s2[v][k]);
}
}
for (ll j = 0; j <= m; j++) {
s2[v][j] = d[j];
d[j] = -1e18;
}
for (ll j = 1; j <= min(m, x); j++) {
for (ll k = 0; k <= min(m - j, sz[v]); k++) {
d[j] = max(d[j], p[j + k] + s1[v][k]);
}
}
for (ll j = 0; j <= m; j++) {
p[j] = d[j];
}
}
for (ll i = 1; i <= m; i++) {
ans[u] = max(ans[u], s1[u][i] + s2[u][i]);
}
for (ll i = 0; i < g[u].size(); i++) {
ll v = g[u][i];
if (v == fa) {
continue;
}
dfs2(v, u);
}
return;
}
int main() {
scanf("%lld%lld", &n, &m);
for (ll i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
}
for (ll i = 1; i < n; i++) {
ll u, v;
scanf("%lld%lld", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
for (ll i = 0; i <= n; i++) {
for (ll j = 0; j <= m; j++) {
s1[i][j] = -1e18;
}
}
for (ll i = 0; i <= n; i++) {
for (ll j = 0; j <= m; j++) {
s2[i][j] = -1e18;
}
}
dfs1(1, 0);
dfs2(1, 0);
for (ll i = 1; i <= n; i++) {
printf("%lld ", ans[i]);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 5ms
memory: 18768kb
input:
5 3 6 10 4 3 4 3 4 4 2 2 5 5 1
output:
0 20 17 17 0
result:
wrong answer 1st numbers differ - expected: '20', found: '0'