이 제출은 이전 버전의 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;
void fix (int pos, int par) {
if (par) {
adj[pos].erase(find(adj[pos].begin(), adj[pos].end(), par));
}
for (auto j : adj[pos]) {
fix(j, pos);
}
}
ll ans (int pos, int cur) {
ll &ret = dp[pos][cur];
if (ret != -1) {
return ret;
}
ret = 0;
ll sum = 0;
for (auto j : adj[pos]) {
sum += p[j];
}
for (auto i : adj[pos]) {
ret = max(ret, ans(i, cur));
if (cur) ret = max(ret, sum + ans(i, 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);
}
memset(dp, -1, sizeof(dp));
fix(1, 0);
cout << ans(1, v) << '\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... |