#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define pb push_back
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define mp make_pair
const int maxn5 = 1e5 + 10;
const int ssq = 320;
const ll inf = 1e18;
ll h[maxn5], have[maxn5];
ll a[maxn5], lim, ans = 0;
vector <int> adj[maxn5];
inline void dfs(int v, int par){
for(auto u : adj[v]) if(u != par)
have[v] += a[u];
have[v] += a[v];
if(h[v] + 1 == lim)
ans = max(ans, have[v]);
for(auto u : adj[v]) if(u != par){
h[u] = h[v] + 1;
have[u] = have[v] - a[u];
dfs(u, v);
}
//cout << v << ' ' << h[v] << ' ' << have[v] << endl;
return;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n; cin >> n >> lim;
for(int i = 0; i < n; i++)
cin >> a[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);
}
dfs(0, -1);
cout << ans - a[0] << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
45 ms |
9676 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |