This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int n, k, p[100001];
vector<int> graph[100001];
ll dp[100001][101];
void dfs1(int node = 1, int parent = 0) {
ll c_sum = 0;
for (int i : graph[node]) if (i != parent) c_sum += p[i];
for (int i : graph[node]) if (i != parent) {
dfs1(i, node);
for (int j = 1; j <= k; j++)
dp[node][j] = max({dp[node][j], dp[i][j], dp[i][j - 1] + c_sum});
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> p[i];
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
dfs1();
cout << dp[1][k];
return 0;
}
# | 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... |