#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#include <bits/stdc++.h>
using namespace std;
#define sqr 547
#define mid (l+r)/2
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define era erase
#define C continue
#define mem(dp,i) memset(dp,i,sizeof(dp))
#define mset multiset
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef short int si;
typedef long double ld;
typedef pair<int,int> pi;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
const ll inf=1e18;
const ll mod=1e9+7;
const ld pai=acos(-1);
int n , m ;
vi v[509] ;
int a[509] ;
int dp[509][509][2] ;
void dfs ( int node , int p ) {
for ( int i = 1 ; i <= m ; i ++ ) dp[node][i][0] = dp[node][i][1] = a[node] ;
for ( auto u : v[node] ) {
if ( u == p ) C ;
dfs ( u , node ) ;
for ( int i = m ; i >= 1 ; i -- ) {
for ( int j = 0 ; j <= i ; j ++ ) {
if ( i-j-1 >= 0 ) dp[node][i][0] = max ( dp[node][i][0] , max ( dp[u][j][1] , dp[u][j][0] ) + dp[node][i-j-1][1] ) ;
if ( i-j-2 >= 0 ) dp[node][i][0] = max ( dp[node][i][0] , dp[u][j][1] + dp[node][i-j-2][0] ) ,
dp[node][i][1] = max ( dp[node][i][1] , dp[u][j][1] + dp[node][i-j-2][1] ) ;
}
}
}
}
int main () {
cin >> n >> m ;
for ( int i = 0 ; i < n ; i ++ ) cin >> a[i] ;
for ( int i = 0 ; i < n-1 ; i ++ ) {
int a , b ;
cin >> a >> b ;
a -- , b -- ;
v[a] .pb ( b ) ;
v[b] .pb ( a ) ;
}
dfs ( 0 , 0 ) ;
cout << max ( dp[0][m][0] , dp[0][m][1] ) << endl ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
768 KB |
Output is correct |
2 |
Correct |
8 ms |
768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
896 KB |
Output is correct |
2 |
Correct |
49 ms |
1016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
1152 KB |
Output is correct |
2 |
Correct |
30 ms |
1184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
1536 KB |
Output is correct |
2 |
Correct |
197 ms |
1528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
167 ms |
1912 KB |
Output is correct |
2 |
Correct |
70 ms |
1912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
317 ms |
2296 KB |
Output is correct |
2 |
Correct |
37 ms |
2296 KB |
Output is correct |