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>
#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 (stderr)
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 |
---|
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... |