#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include<bits/stdc++.h>
//#include "boxes.h"
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define mkp make_pair
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
typedef long long ll;
typedef long double ld;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll llinf=(1LL<<61);
const int inf=(1<<30);
const int nmax=1e5+50;
const int mod=1e9+7;
using namespace std;
int n,i,x,y,k;
ll f[nmax][105],c[nmax],ch[nmax],mx;
vector<int>a[nmax];
void pre(int x,int p)
{
ch[x]=0;
for(int i=0;i<a[x].size();i++)
{
int y=a[x][i];
if(y==p)continue;
pre(y,x);
ch[x]+=c[y];
}
}
void dfs(int x,int p)
{
for(int i=0;i<a[x].size();i++)
{
int y=a[x][i];
if(y==p)continue;
dfs(y,x);
for(int j=0;j<=k;j++)
{
if(!j)f[x][j]=max(f[y][j],f[x][j]);
else
{
f[x][j]=max(f[x][j],max(f[y][j-1]+ch[x],f[y][j]));
}
mx=max(mx,f[x][j]);
}
}
}
/*void dff(int x,int p)
{
if(x!=p)
{
}
for(int i=0;i<a[x].size();i++)
{
int y=a[x][i];
if(y==p)continue;
dff(y,x);
}
}*/
int main()
{
//freopen("sol.in","r",stdin);
//freopen("sol.out","w",stdout);
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
cin>>n>>k;
if(!k)rc(0);
for(i=1;i<=n;i++)cin>>c[i];
for(i=1;i<n;i++)
{
cin>>x>>y;
a[x].pb(y);
a[y].pb(x);
}
pre(1,1);
dfs(1,1);
cout<<mx<<endl;
return 0;
}
Compilation message
chase.cpp:2:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization("O3")
chase.cpp:3:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization("unroll-loops")
chase.cpp: In function 'void pre(int, int)':
chase.cpp:28:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<a[x].size();i++)
~^~~~~~~~~~~~
chase.cpp: In function 'void dfs(int, int)':
chase.cpp:38:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<a[x].size();i++)
~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
2680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
2680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
170 ms |
54648 KB |
Output is correct |
2 |
Correct |
172 ms |
54520 KB |
Output is correct |
3 |
Correct |
100 ms |
10164 KB |
Output is correct |
4 |
Correct |
204 ms |
77692 KB |
Output is correct |
5 |
Correct |
233 ms |
79924 KB |
Output is correct |
6 |
Correct |
229 ms |
79804 KB |
Output is correct |
7 |
Correct |
231 ms |
79608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
2680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |