이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |