#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;
}
cev=max(cev,dp[i][m]);
}
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);
| ~~~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
852 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
1108 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
1492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
50 ms |
1908 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
48 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
124 ms |
3500 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
226 ms |
4308 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |