#include <bits/stdc++.h>
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int i_inf = 1e9;
const ll inf = 1e18;
const ll mod = 1e9+7;
const ld eps = 1e-13;
const ld pi = 3.14159265359;
mt19937 _rand(time(NULL));
clock_t z;
const int mxn = 2e5+5;
int n, v;
ll a[mxn];
ll nsum[mxn];
vector<int> g[mxn];
ll ans = 0;
ll dp1[mxn][101][2];
ll dp2[mxn][101][2];
void dfs(int u, int p){
for(auto e : g[u]){
if(e == p) continue;
dfs(e, u);
}
fr(k, 1, 101){
for(auto e : g[u]){
dp1[u][k][0] = max(dp1[u][k][0], max(dp1[e][k][0], dp1[e][k][1] - a[u]));
dp1[u][k][1] = max(dp1[u][k][1], max(dp1[e][k-1][0], dp1[e][k-1][1]-a[u])+nsum[u]);
}
}
ll MX = 0;
fr(k, 0, 101){
MX = max(MX, dp1[u][k][0]);
dp1[u][k][0] = MX;
}
MX = 0;
fr(k, 0, 101){
MX = max(MX, dp1[u][k][1]);
dp1[u][k][1] = MX;
}
fr(k, 1, 101){
for(auto e : g[u]){
ll mx1 = max(dp2[e][k][0], dp2[e][k][1]);
ll mx2 = max(dp2[e][k-1][0], dp2[e][k-1][1]);
dp2[u][k][0] = max(dp2[u][k][0], mx1);
dp2[u][k][1] = max(dp2[u][k][1], mx2 + nsum[u] - a[e]);
}
}
MX = 0;
fr(k, 0, 101){
MX = max(MX, dp2[u][k][0]);
dp2[u][k][0] = MX;
}
MX = 0;
fr(k, 0, 101){
MX = max(MX, dp2[u][k][1]);
dp2[u][k][1] = MX;
}
}
ll f(int u){
ll ret = 0;
fr(i, 0, v+1){
ret = max(ret, dp1[u][i][0] + dp2[u][v-i][0]);
}
fr(i, 0, v+1){
ll tmp = dp1[u][i][1] + dp2[u][v-i+1][1];
ret = max(ret, dp1[u][i][1] + dp2[u][v-i+1][1] - nsum[u]);
}
return ret;
}
void solve(){
cin >> n >> v;
fr(i, 0, n){
cin >> a[i];
}
fr(i, 0, n-1){
int u, v;
cin >> u >> v;
--u, --v;
g[u].pb(v);
g[v].pb(u);
}
fr(i, 0, n){
nsum[i] = 0;
for(auto u : g[i]){
nsum[i] += a[u];
}
}
dfs(0, 0);
ll ans = 0;
fr(i, 0, v+1) ans = max(ans, dp1[0][i][0]);
fr(i, 0, v+1) ans = max(ans, dp1[0][i][1]);
cout<<ans<<endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}/*
12 2
2 3 3 8 1 5 6 7 8 3 5 4
2 1
2 7
3 4
4 7
7 6
5 6
6 8
6 9
7 10
10 11
10 12
ll ans = 0;
fr(i, 0, n){
ans = max(ans, f(i));
}
cout<<ans<<endl;
*/
Compilation message
chase.cpp: In function 'll f(int)':
chase.cpp:91:6: warning: unused variable 'tmp' [-Wunused-variable]
91 | ll tmp = dp1[u][i][1] + dp2[u][v-i+1][1];
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
5068 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
5068 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
638 ms |
331384 KB |
Output is correct |
2 |
Correct |
568 ms |
331444 KB |
Output is correct |
3 |
Correct |
895 ms |
326432 KB |
Output is correct |
4 |
Correct |
626 ms |
326148 KB |
Output is correct |
5 |
Correct |
606 ms |
326292 KB |
Output is correct |
6 |
Correct |
612 ms |
326284 KB |
Output is correct |
7 |
Correct |
622 ms |
326256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
5068 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |