#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
// #pragma GCC optimize("Ofast")
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define int long long
#define lowbit(x) x&-x
const int maxn = 1e5 + 5;
const int INF = 4e18;
const int N = 998244353;
vector<int> adj[maxn];
int p[maxn], deg[maxn], dep[maxn], dp[maxn];
int n, v, ans = 0;
void dfs(int pos, int prev){
dp[pos] += deg[pos];
ans = max(ans, dp[pos]);
for(auto x : adj[pos]){
if(x == prev) continue;
dep[x] = dep[pos] + 1;
if(dep[x] <= v){
dp[x] = dp[pos] - p[pos];
dfs(x, pos);
}
}
}
signed main(void){
fastio;
cin>>n>>v;
for(int i = 0; i < n; i++) cin>>p[i];
for(int i = 0; i < n - 1; i++){
int a, b;
cin>>a>>b;
a--, b--;
adj[a].pb(b), adj[b].pb(a);
deg[a] += p[b], deg[b] += p[a];
}
if(!v){
cout<<0<<"\n";
return 0;
}
for(int i = 0; i < n; i++){
dp[i] = 0;
dep[i] = 1;
dfs(i, -1);
}
cout<<ans<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5724 KB |
Output is correct |
2 |
Incorrect |
1 ms |
5724 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5724 KB |
Output is correct |
2 |
Incorrect |
1 ms |
5724 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
452 ms |
9636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5724 KB |
Output is correct |
2 |
Incorrect |
1 ms |
5724 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |