/**Lucky Boy**/
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define maxc 1000000007
#define maxn 505
#define maxm 500005
#define pii pair <int,int>
#define Task "Dosta"
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');}
using namespace std;
int n,t,val[maxn],dp[maxn][maxn][2],ans;
vector <int> a[maxn];
void Dfs(int u,int par)
{
dp[u][0][0] = dp[u][0][1] = 0;
dp[u][1][0] = dp[u][1][1] = val[u];
for (int v : a[u])
if (v != par)
{
Dfs(v,u);
FORD(i,t,1)
FOR(j,0,i)
{
if (i - j - 1 >= 0)
{
dp[u][i][0] = max(dp[u][i][0],dp[u][j][1] + dp[v][i-j-1][0]);
//dp[u][i][0] = max(dp[u][i][0],dp[u][j][0] + dp[v][i-j-1][1]);
}
if (i - j - 2 >= 0)
{
dp[u][i][1] = max(dp[u][i][1],dp[u][j][1] + dp[v][i-j-2][1]);
dp[u][i][0] = max(dp[u][i][0],dp[u][j][0] + dp[v][i-j-2][1]);
}
}
}
}
int main()
{
//ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//freopen(Task".inp", "r",stdin);
//freopen(Task".out", "w",stdout);
read(n),read(t);
FOR(i,1,n) FOR(j,0,t) dp[i][j][0] = dp[i][j][1] = -maxc;
FOR(i,1,n) read(val[i]);
FOR(i,1,n-1)
{
int x,y;
read(x),read(y);
a[x].pb(y);
a[y].pb(x);
}
Dfs(1,0);
int ans = 0;
FOR(i,1,t) ans = max(ans,max(dp[1][i][0],dp[1][i][1]));
write(ans);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
900 KB |
Output is correct |
2 |
Correct |
4 ms |
1028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1224 KB |
Output is correct |
2 |
Correct |
39 ms |
1344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
1384 KB |
Output is correct |
2 |
Correct |
24 ms |
1428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
1868 KB |
Output is correct |
2 |
Correct |
168 ms |
1924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
132 ms |
2320 KB |
Output is correct |
2 |
Correct |
54 ms |
2384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
282 ms |
2696 KB |
Output is correct |
2 |
Correct |
27 ms |
2696 KB |
Output is correct |