#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)
{
if(x==p)
{
for(int i=0;i<=k;i++)f[x][i]=0;
f[x][1]=ch[x];
mx=max(mx,f[x][1]);
}
else
{
for(int i=0;i<=k;i++)
{
f[x][i]=0;
if(!i)f[x][i]=f[p][i];
else
{
f[x][i]=max(f[p][i-1]+ch[x],f[p][i]);
}
mx=max(mx,f[x][i]);
}
}
for(int i=0;i<a[x].size();i++)
{
int y=a[x][i];
if(y==p)continue;
dfs(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;
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);
}
if(n<=1000)
{
for(i=1;i<=n;i++)
{
pre(i,i);
dfs(i,i);
}
}
else
{
for(i=1;i<=min(n,15);i++)
{
pre(i,i);
dfs(i,i);
}
}
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:57:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<a[x].size();i++)
~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2684 KB |
Output is correct |
2 |
Correct |
4 ms |
2676 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Incorrect |
4 ms |
2680 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2684 KB |
Output is correct |
2 |
Correct |
4 ms |
2676 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Incorrect |
4 ms |
2680 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
884 ms |
94356 KB |
Output is correct |
2 |
Correct |
913 ms |
94376 KB |
Output is correct |
3 |
Correct |
450 ms |
91492 KB |
Output is correct |
4 |
Correct |
361 ms |
91256 KB |
Output is correct |
5 |
Correct |
1074 ms |
91372 KB |
Output is correct |
6 |
Correct |
1057 ms |
91224 KB |
Output is correct |
7 |
Correct |
1002 ms |
91272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2684 KB |
Output is correct |
2 |
Correct |
4 ms |
2676 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Incorrect |
4 ms |
2680 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |