#include "bits/stdc++.h"
using namespace std;
#define ar array
typedef int64_t ll;
#define int ll
const int N = 1e5 + 5;
vector<int> edges[N];
int a[N];
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n, v; cin>>n>>v;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<n;i++){
int a, b; cin>>a>>b;
edges[a].push_back(b);
edges[b].push_back(a);
}
int res = 0;
for(int i=1;i<=n;i++){
int d[n + 1] {}, pref[n + 1] {};
function<void(int, int)> dfs = [&](int u, int p){
for(auto x : edges[u]){
if(x == p) continue;
pref[u] += a[x];
}
for(auto x : edges[u]){
if(x == p) continue;
pref[x] = pref[u];
d[x] = d[u] + 1;
dfs(x, u);
}
};
pref[i] = a[i];
dfs(i, i);
for(int j=1;j<=n;j++){
if(d[j] < v){
res = max(res, pref[j] - a[i]);
}
}
if(n > 1000) break;
}
cout<<res<<"\n";
}
/*
12 2
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 |
2644 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
46 ms |
13404 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |