/**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]);
if (i - j - 2 >= 0) dp[u][i][1] = max(dp[u][i][1],dp[u][j][1] + 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 |
592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
632 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
872 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
984 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
1116 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
1268 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
66 ms |
1656 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
24 ms |
2100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
121 ms |
2464 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
248 ms |
2888 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |