#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
// #pragma GCC optimize("Ofast")
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define int long long
#define lowbit(x) x&-x
const int maxn = 1e5 + 5;
const int INF = 4e18;
const int N = 998244353;
int dp[maxn][105][2][2];
vector<int> adj[maxn];
int p[maxn], deg[maxn];
int n, v, ans = 0;
void dfs(int pos, int prev){
dp[pos][1][0][1] = deg[pos];
dp[pos][1][1][1] = deg[pos] - p[pos];
for(int i = 2; i <= v; i++) dp[pos][i][1][1] = dp[pos][i - 1][1][1];
ans = max(ans, dp[pos][1][1][1]);
for(auto x : adj[pos]){
if(x == prev) continue;
dfs(x, pos);
for(int i = 1; i < v; i++){
for(int j = 0; j < 2; j++){
for(int k = 0; k < 2; k++){
ans = max(ans, dp[x][i][0][j] + dp[pos][v - i][1][k] - j * (p[x] + p[pos]));
ans = max(ans, dp[x][i][1][j] + dp[pos][v - i][0][k] - k * (p[x] + p[pos]));
}
}
}
for(int i = 1; i <= v; i++){
for(int j = 0; j < 2; j++){
for(int k = 0; k < 2; k++){
dp[pos][i][0][k] = max(dp[pos][i][0][k], dp[x][i - k][0][j] + (k ? deg[pos] : 0) - j * (p[x] + p[pos]));
dp[pos][i][1][k] = max(dp[pos][i][1][k], dp[x][i - k][1][j] + (k ? deg[pos] : 0) - k * (p[x] + p[pos]));
}
}
for(int j = 0; j < 2; j++){
for(int k = 0; k < 2; k++){
dp[pos][i][j][k] = max(dp[pos][i][j][k], dp[pos][i - 1][j][k]);
}
}
}
}
}
signed main(void){
fastio;
cin>>n>>v;
for(int i = 0; i < n; i++) cin>>p[i], deg[i] += p[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);
deg[a] += p[b], deg[b] += p[a];
}
if(!v){
cout<<0<<"\n";
return 0;
}
dfs(0, -1);
cout<<ans<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
0 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
0 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
3 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
8796 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
1 ms |
8540 KB |
Output is correct |
12 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
218 ms |
345756 KB |
Output is correct |
2 |
Correct |
210 ms |
345724 KB |
Output is correct |
3 |
Correct |
165 ms |
339364 KB |
Output is correct |
4 |
Correct |
95 ms |
338852 KB |
Output is correct |
5 |
Correct |
204 ms |
338772 KB |
Output is correct |
6 |
Correct |
231 ms |
338772 KB |
Output is correct |
7 |
Correct |
213 ms |
338772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
0 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
3 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
8796 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
1 ms |
8540 KB |
Output is correct |
12 |
Correct |
1 ms |
8540 KB |
Output is correct |
13 |
Correct |
218 ms |
345756 KB |
Output is correct |
14 |
Correct |
210 ms |
345724 KB |
Output is correct |
15 |
Correct |
165 ms |
339364 KB |
Output is correct |
16 |
Correct |
95 ms |
338852 KB |
Output is correct |
17 |
Correct |
204 ms |
338772 KB |
Output is correct |
18 |
Correct |
231 ms |
338772 KB |
Output is correct |
19 |
Correct |
213 ms |
338772 KB |
Output is correct |
20 |
Correct |
216 ms |
338768 KB |
Output is correct |
21 |
Correct |
26 ms |
10324 KB |
Output is correct |
22 |
Correct |
208 ms |
338812 KB |
Output is correct |
23 |
Correct |
95 ms |
338768 KB |
Output is correct |
24 |
Correct |
213 ms |
338772 KB |
Output is correct |
25 |
Correct |
157 ms |
338664 KB |
Output is correct |
26 |
Correct |
203 ms |
345912 KB |
Output is correct |
27 |
Correct |
201 ms |
345936 KB |
Output is correct |