#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;
#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N = (int)1e5 + 1;
const int D = 101;
vector<int> T[N];
ll sol;
ll p[N];
ll dp[N][D];
ll dp2[N][D][2];
int v;
void upd(ll &a, ll b){
a=max(a,b);
}
void dfs(int u, int pp){
for(auto x : T[u]){
if(x==pp) continue;
dfs(x,u);
}
ll sum = 0;
for(auto x : T[u]){
sum += p[x];
}
dp[u][1]=sum;
dp2[u][1][1]=sum;
for(auto x : T[u]){
if(x==pp)continue;
for(int a = 1; a <= v; a ++ ){
for(int b = 1; a + b <= v; b ++ ){
for(int q = 0; q < 2; q ++ ){
sol = max(sol, dp[u][a]+dp2[x][b][q]-(q*p[u]));
sol = max(sol, dp2[u][a][q]+dp[x][b]-(q*p[x]));
}
}
}
// update dp
for(int i = 1; i <= v; i ++ ){
dp[u][i]=max(dp[u][i],dp[x][i]);
if(i + 1 <= v){
dp[u][i+1]=max(dp[u][i+1],dp[x][i]+sum-p[x]);
}
}
for(int b = 0; b < 2; b ++ ){
for(int i = 1; i <= v; i ++ ){
dp2[u][i][0]=max(dp2[u][i][0],dp2[x][i][b]-(b*p[u]));
if(i + 1 <= v){
dp2[u][i+1][1]=max(dp2[u][i+1][1],dp2[x][i][b]+sum-(b*p[u]));
}
}
}
}
for(int a = 0 ; a <= v; a ++ ){
sol = max(sol,dp[u][a]);
for(int b = 0 ; b < 2; b ++ ){
sol = max(sol,dp2[u][a][b]);
}
}
}
int main(){
fastIO;
int n;
cin >> n >> v;
for(int i = 1; i <= n; i ++ ){
cin >> p[i];
}
int u, v;
for(int i = 1; i < n; i ++ ){
cin >> u >> v;
T[u].push_back(v);
T[v].push_back(u);
}
dfs(1,-1);
cout << sol << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2668 KB |
Output is correct |
7 |
Correct |
19 ms |
5228 KB |
Output is correct |
8 |
Correct |
4 ms |
5228 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
19 ms |
5100 KB |
Output is correct |
11 |
Correct |
5 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1730 ms |
253364 KB |
Output is correct |
2 |
Correct |
1724 ms |
255468 KB |
Output is correct |
3 |
Correct |
1684 ms |
246372 KB |
Output is correct |
4 |
Correct |
206 ms |
245996 KB |
Output is correct |
5 |
Correct |
1701 ms |
245996 KB |
Output is correct |
6 |
Correct |
1726 ms |
246124 KB |
Output is correct |
7 |
Correct |
1724 ms |
246144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2668 KB |
Output is correct |
7 |
Correct |
19 ms |
5228 KB |
Output is correct |
8 |
Correct |
4 ms |
5228 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
19 ms |
5100 KB |
Output is correct |
11 |
Correct |
5 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
1730 ms |
253364 KB |
Output is correct |
14 |
Correct |
1724 ms |
255468 KB |
Output is correct |
15 |
Correct |
1684 ms |
246372 KB |
Output is correct |
16 |
Correct |
206 ms |
245996 KB |
Output is correct |
17 |
Correct |
1701 ms |
245996 KB |
Output is correct |
18 |
Correct |
1726 ms |
246124 KB |
Output is correct |
19 |
Correct |
1724 ms |
246144 KB |
Output is correct |
20 |
Correct |
1722 ms |
246124 KB |
Output is correct |
21 |
Correct |
201 ms |
245996 KB |
Output is correct |
22 |
Correct |
1718 ms |
246336 KB |
Output is correct |
23 |
Correct |
204 ms |
245948 KB |
Output is correct |
24 |
Correct |
1768 ms |
246100 KB |
Output is correct |
25 |
Correct |
1678 ms |
245860 KB |
Output is correct |
26 |
Correct |
1687 ms |
255588 KB |
Output is correct |
27 |
Correct |
1692 ms |
255468 KB |
Output is correct |