#include <bits/stdc++.h>
using namespace std;
typedef long long lo;
#define fi first
#define se second
#define endl "\n"
#define pb push_back
#define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FOR for(int i=1;i<=n;i++)
#define mid ((start+end)/2)
#define ort ((bas+son)/2)
const lo inf = 1000000000;
const lo li = 505;
const lo mod = 1000000007;
int n,m,a[li],k,flag,t,b[li],say,dist[li][li],dp[li][li];
int cev;
string s;
vector<int> v[li];
vector<pair<int,int>> vv;
inline int in(){
int x;
scanf("%d",&x);
return x;
}
inline void dfs(int node,int ata){
b[++say]=node;
for(auto go:v[node]){
if(go==ata)continue;
dfs(go,node);
}
}
inline void dfs2(int node,int ata,int dep,int start){
dist[start][node]=dep;
for(auto go:v[node]){
if(go==ata)continue;
dfs2(go,node,dep+1,start);
}
}
int main(void){
n=in(),m=in();
FOR a[i]=in();
for(int i=1;i<n;i++){
int x=in(),y=in();
v[x].pb(y);
v[y].pb(x);
}
dfs(1,0);
FOR dfs2(i,0,0,i);
FOR{
for(int j=0;j<=m;j++)dp[i][j]=-1000000000;
}
dp[1][0]=0;
dp[1][1]=a[1];
FOR{
vv.pb({b[i],i});
}
sort(vv.begin(),vv.end());
for(int go=0;go<n;go++){
int i=vv[go].se;
for(int j=0;j<=m;j++){
for(int go1=0;go1<go;go1++){
int ii=vv[go1].se;
int at=j-dist[ii][i];
if(at<0)continue;
dp[i][j]=max(dp[i][j],dp[ii][at]);
dp[i][j+1]=max(dp[i][j+1],dp[ii][at]+a[i]);
//~ cout<<i<<" AA "<<ii<<" AA "<<at<<endl;
}
}
}
FOR{
for(int j=0;j<=m;j++){
cev=max(cev,dp[i][j]);
//~ cout<<i<<" ()() "<<j<<" ()() "<<dp[i][j]<<endl;
}
}
printf("%d\n",cev);
return 0;
}
Compilation message
dostavljac.cpp: In function 'int in()':
dostavljac.cpp:28:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | scanf("%d",&x);
| ~~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
724 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
852 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
27 ms |
1128 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
31 ms |
1528 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
121 ms |
1876 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
253 ms |
2304 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |