#include <bits/stdc++.h>
using namespace std;
#define rep(i,s,e) for (ll i = s; i <= e; ++i)
#define rrep(i,s,e) for (ll i = s; i >= e; --i)
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define len(a) (ll)a.size()
typedef long long ll;
typedef pair<ll, ll> ii;
typedef pair<ll, ll> pll;
typedef vector<ii> vii;
typedef vector<ll> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
const ll mx = 1e5;
ll p[mx+1], s[mx+1], l;
vi adj[mx+1];
ll dfs (ll u, ll par, ll cur, ll cur2, ll cnt) {
if (cnt==l) return abs(cur-cur2);
cur += s[u] - p[par];
cur2 += p[u];
ll res = abs(cur-cur2);
for (ll v : adj[u]) {
if (v==par) continue;
res = max(res, dfs(v, u, cur, cur2, cnt+1));
}
return res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
ll n; cin >> n >> l;
rep (i,1,n) cin >> p[i];
rep (i,1,n-1) {
ll u, v; cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
rep (i,1,n) {
s[i] = p[i];
for (ll v : adj[i]) s[i] += p[v];
}
ll ans = 0;
rep (i,1,n) ans = max(ans, dfs (i, 0, 0, 0, 0));
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
728 ms |
10076 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |