#include <bits/stdc++.h>
using namespace std;
typedef long long lo;
#define fi first
#define se second
#define endl "\n"
#define int long long
#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("%lld",&x);
return x;
}
inline void dfs(int node,int ata){
b[node]=++say;
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);
}
}
int32_t 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]=-1000000000000000000;
}
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("%lld\n",cev);
return 0;
}
Compilation message
dostavljac.cpp: In function 'long long int in()':
dostavljac.cpp:29:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | scanf("%lld",&x);
| ~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
852 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
1136 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
1492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
20 ms |
1924 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
23 ms |
2728 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
86 ms |
3540 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
166 ms |
4308 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |