#include <bits/stdc++.h>
#define vi vector<int>
#define ve vector
#define ll long long
#define vl vector<ll>
#define vll vector<pair<ll,ll>>
#define onbit __builtin_popcount
#define ii pair<int,int>
#define vvi vector<vi>
#define vii vector<ii>
#define gii greater<ii>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF 1e18
#define eps 1e-7
#define eps1 1e-2
#define optimise ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define MAX_A 1e5+5
using namespace std;
const int MOD = 1e9+7;
const int nax = 2e5+5;
const int MAX_VAL = 1e6;
double PI=3.14159265359;
int arx[8]={1,1,0,-1,-1,-1, 0, 1};
int ary[8]={0,1,1, 1, 0,-1,-1,-1};
typedef complex<int> Point;
#define X real()
#define Y imag()
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
vector<int> adj[nax];
ll sum[nax];
int tab[nax];
ll dp[nax][102][2];
ll cur[102][2];
int n,k;
ll ans;
void dfs(int x,int p){
dp[x][1][0]=sum[x];
for(auto u:adj[x]){
if(u==p) continue;
dfs(u,x);
for (int i = 1; i <= k; ++i)
{
ll down=max(dp[u][i][0],dp[u][i-1][0]+sum[x]-tab[u]);
ll up=max(dp[u][i][1],dp[u][i-1][1]+sum[u]-tab[x]);
cur[i][0]=down;
cur[i][1]=up;
if(i!=k){
ans=max(ans,max(dp[x][k-i][0]+up,dp[x][k-i][1]+down));
ans=max(ans,dp[u][i][1]+sum[x]);
}
}
for (int i = 1; i <= k; ++i)
{
dp[x][i][0]=max(dp[x][i][0],cur[i][0]);
dp[x][i][1]=max(dp[x][i][1],cur[i][1]);
ans=max(ans,dp[x][i][0]);
}
}
return;
}
int main(){
optimise;
//setIO("closing");
cin>>n>>k;
for (int i = 0; i < n; ++i)
{
cin>>tab[i];
}
for (int i = 0; i < n-1; ++i)
{
int x,y;
cin>>x>>y;
x--;y--;
sum[x]+=tab[y];
sum[y]+=tab[x];
adj[x].pb(y);
adj[y].pb(x);
}
if(!k){
cout << 0<<endl;
return 0;
}
dfs(0,-1);
cout <<ans<<endl;
}
Compilation message
chase.cpp: In function 'void setIO(std::string)':
chase.cpp:32:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
chase.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8536 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8536 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
10840 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
3 ms |
10844 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
159 ms |
180560 KB |
Output is correct |
2 |
Correct |
157 ms |
180560 KB |
Output is correct |
3 |
Correct |
122 ms |
173768 KB |
Output is correct |
4 |
Correct |
96 ms |
173392 KB |
Output is correct |
5 |
Correct |
166 ms |
173396 KB |
Output is correct |
6 |
Correct |
152 ms |
173392 KB |
Output is correct |
7 |
Correct |
150 ms |
173396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8536 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
10840 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
3 ms |
10844 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10844 KB |
Output is correct |
13 |
Correct |
159 ms |
180560 KB |
Output is correct |
14 |
Correct |
157 ms |
180560 KB |
Output is correct |
15 |
Correct |
122 ms |
173768 KB |
Output is correct |
16 |
Correct |
96 ms |
173392 KB |
Output is correct |
17 |
Correct |
166 ms |
173396 KB |
Output is correct |
18 |
Correct |
152 ms |
173392 KB |
Output is correct |
19 |
Correct |
150 ms |
173396 KB |
Output is correct |
20 |
Correct |
159 ms |
173488 KB |
Output is correct |
21 |
Correct |
25 ms |
13916 KB |
Output is correct |
22 |
Correct |
151 ms |
173592 KB |
Output is correct |
23 |
Correct |
90 ms |
173468 KB |
Output is correct |
24 |
Correct |
147 ms |
173468 KB |
Output is correct |
25 |
Correct |
122 ms |
173516 KB |
Output is correct |
26 |
Correct |
142 ms |
180560 KB |
Output is correct |
27 |
Correct |
136 ms |
180476 KB |
Output is correct |