#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
using ll = long long;
const int N = 1e5+5;
//--------------------------------------
int n, k, a[N];
vector<int> adj[N];
ll f[N][105][2];
void dfs(int u, int pr) {
ll s = 0;
for (int i = 0; i < adj[u].size(); i++) {
if (adj[u][i] == pr) {
swap(adj[u][i], adj[u].back());
adj[u].pop_back();
}
}
for (auto v : adj[u]) {
dfs(v, u);
s += a[v];
}
for (auto v : adj[u]) {
if (v == pr) continue;
for (int i = 0; i <= k; i++) {
f[u][i][0] = max(f[v][i][0], f[v][i][1] + a[u]);
if (i) {
f[u][i][1] = max(f[v][i-1][0], f[v][i-1][1] + a[u]) + s - a[v];
}
}
}
}
vector<vector<array<int, 2>>> trai, phai;
void dfs2(int u, int pr) {
ll s = 0;
trai.assign(adj[u].size()+1, vector<array<int, 2>>(k+1, {0, 0})); phai = trai;
vector<array<int, 2>> ins; ins.reserve(adj[u].size());
for (int i = 0; i < n; i++) {
// ins.push_back({f[v][]});
}
for (auto v : adj[u]) {
if (v == pr) continue;
for (int i = 0; i <= k; i++) {
f[u][i][0] = max(f[v][i][0], f[v][i][1]);
if (i) {
f[u][i][1] = max(f[v][i-1][0], f[v][i-1][1]) + s - a[v];
}
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i < n; i++) {
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, 1);
ll ans = 0;
for (int i = 0; i <= 100; i++) {
ans = max({ans, f[1][i][0], f[1][i][1]});
}
cout << ans;
}
/*
f(u, i, 0) = f(v, i, 0), f(v, i, 1)
f(u, i, 1) = f(v, i-1, 0) + tong con khac v
= f(v, i-1, 1) + tong con khac v
*/
Compilation message
chase.cpp: In function 'void dfs(int, int)':
chase.cpp:17:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for (int i = 0; i < adj[u].size(); i++) {
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2648 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2648 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
71 ms |
94544 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2648 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |