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<iostream>
#include<cstdio>
#include<fstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<cstring>
#include<vector>
using namespace std;
#define for1(i,n) for(int i=1;i<=(int)n;i++)
#define for0(i,n) for(int i=0;i<=(int)n;i++)
#define forn(i,n) for(int i=n;i>=1;i--)
#define fo(i,x,y) for(int i=x;i<=(int)y;i++)
#define fr(i,x,y) for(int i=x;i>=(int)y;i--)
#define pb push_back
#define mp make_pair
#define LL long long
const LL Mod=1000*1000*1000+7;
int n,k;
LL P[100002];
LL gum[100002];
vector<vector<int> > g(100002);
void dfs(int v,int p)
{
for0(i,g[v].size()-1)
{
int to=g[v][i];
if(to!=p)
{
dfs(to,v);
gum[v]+=P[to];
}
}
}
LL dp[100002][102];
LL ANS[10002];
void solve(int v,int p)
{
if(v!=1)
{
dp[v][0]=ANS[p];
ANS[v]=ANS[p];
for1(i,k)
{
dp[v][i]=dp[p][i-1]+gum[v];
ANS[v]=max(ANS[v],dp[v][i]);
}
}
for0(i,g[v].size()-1)
{
int to=g[v][i];
if(to!=p)
solve(to,v);
}
}
int main()
{
cin>>n>>k;
for1(i,n)
cin>>P[i];
for1(i,n-1)
{
int x,y;
cin>>x>>y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1,-1);
dp[1][1]=gum[1];
ANS[1]=gum[1];
solve(1,-1);
LL ma=0;
for1(i,n)
ma=max(ma,ANS[i]);
cout<<ma<<endl;
return 0;
}
# | 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... |