#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];
vector<pll> qs;
void dfs (int u, int par) {
for (int v : adj[u]) {
if (v==par) continue;
dfs (v, u);
}
qs.pb({u,par});
}
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);
set<pll,greater<pll>> dpd_nxt[mxv+1], dpu_here[mxv+1];
for (auto [u, par] : qs) {
rep (i,0,mxv) {
if (i) {
dpd[u][i] = s[u]-p[par];
dpu[u][i] = s[u];
}
dpd_nxt[i].clear();
dpu_here[i].clear();
dpd_nxt[i].insert({0,0});
dpu_here[i].insert({dpu[u][i], u});
}
for (int v : adj[u]) {
if (v==par) continue;
dfs (v, u);
rep (i,0,mxv) {
dpd_nxt[i].insert({dpd[v][i], v});
if (len(dpd_nxt[i])>2) dpd_nxt[i].erase(--dpd_nxt[i].end());
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) {
dpu_here[i].insert({i==0?dpu[v][i]:max(dpu[v][i], dpu[v][i-1]+s[u]-p[v]), v});
if (len(dpu_here[i])>2) dpu_here[i].erase(--dpu_here[i].end());
dpu[u][i] = max(dpu[u][i], dpu[v][i]);
if (i) {
dpu[u][i] = max(dpu[u][i], dpu[v][i-1]+s[u]-p[v]);
dpu[u][i] = max(dpu[u][i], dpu[u][i-1]);
}
}
}
rep (i,0,l) {
auto it1 = dpu_here[i].begin();
auto it2 = dpd_nxt[l-i].begin();
if ((*it1).se==(*it2).se) {
++it2;
ans = max(ans, (*it1).fi+(*it2).fi);
--it2;
++it1;
}
ans = max(ans, (*it1).fi+(*it2).fi);
}
}
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Runtime error |
6 ms |
5792 KB |
Execution killed with signal 11 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
77 ms |
33076 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Runtime error |
6 ms |
5792 KB |
Execution killed with signal 11 |
8 |
Halted |
0 ms |
0 KB |
- |