#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 |
828 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
1108 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
1492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
37 ms |
1916 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
32 ms |
2708 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
114 ms |
3508 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
245 ms |
4308 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |