#include <bits/stdc++.h>
using namespace std;
const int nx=1e5+5, kx=105;
#define ll long long
ll n, k, b[nx], u, v, sm[nx], dp[nx][kx], res;
vector<ll> d[nx];
void dfs(int u, int p)
{
sm[u]=0;
for (auto v:d[u]) if (v!=p) sm[u]+=b[v];
for (int i=1; i<=k; i++) dp[u][i]=max(dp[p][i], dp[p][i-1]+sm[u]), res=max(res, dp[u][i]); //cout<<"debug "<<u<<' '<<i<<' '<<dp[u][i]<<'\n';
for (auto v:d[u]) if (v!=p) dfs(v, u);
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>k;
for (int i=1; i<=n; i++) cin>>b[i];
for (int i=1; i<n; i++) cin>>u>>v, d[u].push_back(v), d[v].push_back(u);
for (int rt=1; rt<=((n<=1000)?n:1); rt++)
{
for (int i=0; i<=k; i++) for (int j=1; j<=n; j++) dp[j][i]=0;
//cout<<"root "<<rt<<'\n';
dfs(rt, 0);
}
cout<<res;
}
/*
12 2
2 3 3 8 1 5 6 7 8 3 5 4
2 1
2 7
3 4
4 7
7 6
5 6
6 8
6 9
7 10
10 11
10 12
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4696 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4696 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
197 ms |
4812 KB |
Output is correct |
8 |
Correct |
27 ms |
4700 KB |
Output is correct |
9 |
Correct |
19 ms |
4816 KB |
Output is correct |
10 |
Correct |
231 ms |
4700 KB |
Output is correct |
11 |
Correct |
71 ms |
4696 KB |
Output is correct |
12 |
Correct |
40 ms |
4792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
92572 KB |
Output is correct |
2 |
Correct |
122 ms |
94488 KB |
Output is correct |
3 |
Correct |
71 ms |
92352 KB |
Output is correct |
4 |
Correct |
55 ms |
92292 KB |
Output is correct |
5 |
Correct |
117 ms |
92240 KB |
Output is correct |
6 |
Correct |
152 ms |
92240 KB |
Output is correct |
7 |
Correct |
133 ms |
92248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4696 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
197 ms |
4812 KB |
Output is correct |
8 |
Correct |
27 ms |
4700 KB |
Output is correct |
9 |
Correct |
19 ms |
4816 KB |
Output is correct |
10 |
Correct |
231 ms |
4700 KB |
Output is correct |
11 |
Correct |
71 ms |
4696 KB |
Output is correct |
12 |
Correct |
40 ms |
4792 KB |
Output is correct |
13 |
Correct |
95 ms |
92572 KB |
Output is correct |
14 |
Correct |
122 ms |
94488 KB |
Output is correct |
15 |
Correct |
71 ms |
92352 KB |
Output is correct |
16 |
Correct |
55 ms |
92292 KB |
Output is correct |
17 |
Correct |
117 ms |
92240 KB |
Output is correct |
18 |
Correct |
152 ms |
92240 KB |
Output is correct |
19 |
Correct |
133 ms |
92248 KB |
Output is correct |
20 |
Incorrect |
107 ms |
92244 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |