#include <bits/stdc++.h>
using namespace std ;
const int MAX = 500 + 10 ;
int arr[MAX] , in[MAX] , out[MAX] , nxt[MAX+MAX] ;
int dp[MAX+MAX][MAX] ;
int n , m ;
vector< vector<int> >adj(MAX) ;
vector<int>eul ;
void dfs(int node , int par)
{
in[node] = eul.size() ;
eul.push_back(node) ;
for(auto &child : adj[node])
{
if(child == par)
continue ;
dfs(child , node) ;
nxt[in[child]] = eul.size() ;
eul.push_back(node) ;
}
out[node] = eul.size()-1 ;
return ;
}
int solve(int idx, int rem)
{
if(rem == 0)
return 0 ;
if(idx == eul.size())
return 0 ;
int &ret = dp[idx][rem] ;
if(ret != -1)
return ret ;
int node = eul[idx] ;
ret = 0 ;
if(in[node] == idx)
{
ret = max(ret , arr[node]) ;
if(rem > 1)
{
ret = max(ret , solve(idx+1 , rem-2) + arr[node]) ;
int to = -1 ;
if(idx < eul.size()-1)
to = eul[idx+1] ;
if(to != -1 && in[to] >= in[node] && out[to] <= out[node])
ret = max(ret , solve(nxt[idx+1] , rem-1) + arr[node]) ;
}
}
ret = max(ret , solve(idx+1 , rem-1)) ;
int to = -1 ;
if(idx < eul.size()-1)
to = eul[idx+1] ;
if(to != -1 && in[to] >= in[node] && out[to] <= out[node])
ret = max(ret , solve(nxt[idx+1] , rem)) ;
return ret ;
}
int main()
{
memset(dp , -1 , sizeof(dp)) ;
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n>>m ;
for(int i = 1 ; i <= n ; ++i)
cin>>arr[i] ;
for(int i = 0 ; i < n-1 ; ++i)
{
int x , y ;
cin>>x>>y ;
adj[x].push_back(y) ;
adj[y].push_back(x) ;
}
dfs(1 , -1) ;
return cout<<solve(0 , m)<<"\n" , 0 ;
}
Compilation message
dostavljac.cpp: In function 'int solve(int, int)':
dostavljac.cpp:33:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(idx == eul.size())
~~~~^~~~~~~~~~~~~
dostavljac.cpp:47:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(idx < eul.size()-1)
~~~~^~~~~~~~~~~~~~
dostavljac.cpp:55:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(idx < eul.size()-1)
~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
2432 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
2432 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
2432 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
2432 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
2432 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
2432 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
2432 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
2432 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
2432 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |