This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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, mxv = 100;
ll p[mx+1], s[mx+1], dpd[mx+1][mxv+1], dpu[mx+1][mxv+1], ans, l;
vi adj[mx+1];
void dfs (int u, int par) {
pll dpd_nxt[mxv+1][2], dpu_here[mxv+1][2];
rep (i,0,mxv) {
if (i) {
dpd[u][i] = s[u]-p[par];
dpu[u][i] = s[u];
}
dpu_here[i][1] = dpd_nxt[i][0] = dpd_nxt[i][1] = {0,0};
dpu_here[i][0] = {dpu[u][i],u};
}
for (int v : adj[u]) {
if (v==par) continue;
dfs (v, u);
rep (i,0,mxv) {
if (dpd[v][i]>=dpd_nxt[i][1].fi) dpd_nxt[i][1] = {dpd[v][i], v};
if (dpd[v][i]>=dpd_nxt[i][0].fi) {
dpd_nxt[i][1] = dpd_nxt[i][0];
dpd_nxt[i][0] = {dpd[v][i], v};
}
dpd[u][i] = max(dpd[u][i], dpd[v][i]);
if (i) {
dpd[u][i] = max(dpd[u][i], s[u]-p[par]+dpd[v][i-1]);
dpd[u][i] = max(dpd[u][i], dpd[u][i-1]);
}
}
rep (i,0,mxv) {
ll sth = i==0?dpu[v][i]:max(dpu[v][i], dpu[v][i-1]+s[u]-p[v]);
if (sth>=dpu_here[i][1].fi) dpu_here[i][1] = {sth, v};
if (sth>=dpu_here[i][0].fi) {
dpu_here[i][1] = dpu_here[i][0];
dpu_here[i][0] = {sth, v};
}
dpu[u][i] = max(dpu[u][i], sth);
if (i) dpu[u][i] = max(dpu[u][i], dpu[u][i-1]);
}
}
rep (i,0,l) {
if (dpd_nxt[i][0].se==dpu_here[l-i][0].se) {
ans = max(ans, dpd_nxt[i][0].fi+dpu_here[l-i][1].fi);
ans = max(ans, dpd_nxt[i][1].fi+dpu_here[l-i][0].fi);
}
else ans = max(ans, dpd_nxt[i][0].fi+dpu_here[l-i][0].fi);
}
}
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) for (int v : adj[i]) s[i] += p[v];
dfs (1,0);
cout << ans << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |