#include <bits/stdc++.h>
#define int long long
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
using namespace std;
const int N=20;
vector <int> g[N];
int p[N];
void dfs(int v,int par){
p[v]=par;
for(auto to : g[v]){
if(to!=par)dfs(to,v);
}
}
signed main(){
ios_base::sync_with_stdio();
cin.tie(0);cout.tie(0);
int n,m;
cin>>n>>m;
vector <int> x(n);
for(int i=0;i<n;i++)cin>>x[i];
for(int i=0;i<n-1;i++){
int u,v;
cin>>u>>v;
u--;v--;
g[v].pb(u);
g[u].pb(v);
}
int ans=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)p[j]=0;
dfs(i,-1);
for(int j=0;j<n;j++){
int s=j;
vector <int> ver;
ver.pb(j);
while(s!=i){
s=p[s];
ver.pb(s);
}
reverse(all(ver));
int sz=ver.size();
for(int mask=0;mask<(1<<sz);mask++){
vector <int> val=x;
int res1=0,res2=0;
int cnt=0;
for(int k=0;k<sz;k++){
if((mask & (1<<k))!=0){
cnt++;
res1+=val[ver[k]];
for(auto to : g[ver[k]]){
val[ver[k]]+=val[to];
val[to]=0;
}
}
else res1+=val[ver[k]];
}
for(int k=0;k<sz;k++){
res2+=val[ver[k]];
}
if(cnt<=m)ans=max(ans,res2-res1);
}
}
}
cout<<ans<<"\n";
}
/*
12 2
0 1 2 3 4 5 6 7 8 9 10 11
2 3 3 8 1 5 6 7 8 3 5 4
2 1
2 7
3 4
4 7
7 6
5 6
6 8
6 9
7 10
10 11
10 12
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
436 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
436 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
30 ms |
3132 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
436 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
8 |
Halted |
0 ms |
0 KB |
- |