이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 25;
ll dp[MAXN][101], p[MAXN];
vector <int> adj[MAXN];
int n, v;
ll ans (int pos, int par, int cur) {
ll &ret = dp[pos][cur];
if (ret != -1) {
return ret;
}
ret = 0;
ll sum = 0;
for (auto j : adj[pos]) {
if (j != par) {
sum += p[j];
}
}
for (auto i : adj[pos]) {
if (i != par) {
ret = max(ret, ans(i, pos, cur));
if (cur) ret = max(ret, sum + ans(i, pos, cur - 1));
}
}
return ret;
}
void solve () {
cin >> n >> v;
for (int i = 1; i <= n; i++) {
cin >> p[i];
}
for (int i = 1; i < n; i++) {
int a, b; cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
ll mx = 0;
for (int i = 1; i <= n; i++) {
memset(dp, -1, sizeof(dp));
mx = max(mx, ans(i, -1, v));
}
cout << mx << '\n';
}
signed main () {
ios::sync_with_stdio(0); cin.tie(0);
int tc = 1; //cin >> tc;
while (tc--) solve();
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |