//Rochy'.'
#include <bits/stdc++.h>
#define fo(i,a,b) for(int i=a;i<=b;++i)
#define fd(i,a,b) for(int i=a;i>=b;--i)
#define fl(i,a,b) for(int i=a;i<b;++i)
#define fa(i,a) for(auto (i):(a))
#define F first
#define S second
#define all(a) a.begin(),a.end()
#define vi vector <int>
#define ii pair <int,int>
#define pb push_back
using namespace std;
template <typename T> inline void read(T &x){char c;bool nega=0;while((!isdigit(c=getchar()))&&(c!='-'));
if(c=='-'){nega=1;c=getchar();}x=c-48;while(isdigit(c=getchar()))x=x*10+c-48;if(nega)x=-x;}
template <typename T> inline void writep(T x){if(x>9)writep(x/10);putchar(x%10+48);}
template <typename T> inline void write(T x){if(x<0){putchar('-');x=-x;}writep(x);putchar(' ');}
template <typename T> inline void writeln(T x){write(x);putchar('\n');}
template <typename R, typename D> inline void Min(R &a, D b){if(a>b) a=b;}
template <typename D, typename R> inline void Max(D &a, R b){if(a<b) a=b;}
const int N=502,mod=1e9+7;
int n,m,dp[N][N][2],w[N];
vi a[N];
void dfs(int u,int pa){
fa(v,a[u]) if(v!=pa){
dfs(v,u);
fd(i,m,1){
fo(j,2,i){
Max(dp[u][i][0],dp[u][i-j][0]+dp[v][j-2][1]);
Max(dp[u][i][1],dp[u][i-j][1]+dp[v][j-2][1]);
}
fo(j,1,i) Max(dp[u][i][0],dp[u][i-j][1]+dp[v][j-1][0]);
}
}
fd(i,m,1){
Max(dp[u][i][0],dp[u][i-1][0]+w[u]);
Max(dp[u][i][1],dp[u][i-1][1]+w[u]);
}
}
int main(){
ios_base::sync_with_stdio(NULL);
cin. tie(NULL); cout. tie(NULL);
// freopen(".inp" , "r", stdin);
// freopen(".out", "w", stdout);
cin>>n>>m;
fo(i,1,n) cin>>w[i];
fl(i,1,n){
int u,v;cin>>u>>v;
a[u].pb(v);
a[v].pb(u);
}
dfs(1,0);
cout<<dp[1][m][0];
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
764 KB |
Output is correct |
2 |
Correct |
4 ms |
760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
888 KB |
Output is correct |
2 |
Correct |
25 ms |
1052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
1144 KB |
Output is correct |
2 |
Correct |
17 ms |
1212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
1528 KB |
Output is correct |
2 |
Correct |
93 ms |
1644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
82 ms |
1884 KB |
Output is correct |
2 |
Correct |
38 ms |
1912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
155 ms |
2296 KB |
Output is correct |
2 |
Correct |
20 ms |
2424 KB |
Output is correct |