#include<bits/stdc++.h>
typedef long long ll;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
ll n,k,i,a[101010],ta,tb,x[101010],has;
vector<ll> v[101010];
ll d[100010][110];
ll depe(ll aa,ll bb,ll cc)
{
if(d[aa][bb]==-1)
{
d[aa][bb]=0;
ll ii;
for(ii=0;ii<v[aa].size();ii++)
{
if(v[aa][ii]==cc)continue;
d[aa][bb]=max(d[aa][bb],depe(v[aa][ii],bb,aa));
if(bb)
d[aa][bb]=max(d[aa][bb],depe(v[aa][ii],bb-1,aa)+x[aa]-a[cc]);
}
}
return d[aa][bb];
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>k;
for(i=1;i<=n;i++)
cin>>a[i];
for(i=1;i<n;i++)
{
cin>>ta>>tb;
v[ta].pb(tb);
v[tb].pb(ta);
x[ta]+=a[tb];
x[tb]+=a[ta];
}
for(i=1;i<=n;i++)
{
memset(d,-1,sizeof(d));
has=max(has,depe(i,k,0));
if(n>1000)break;
}
cout<<has<<"\n";
}
Compilation message
chase.cpp: In function 'll depe(ll, ll, ll)':
chase.cpp:17:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(ii=0;ii<v[aa].size();ii++)
~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
211 ms |
88824 KB |
Output is correct |
2 |
Correct |
209 ms |
88884 KB |
Output is correct |
3 |
Correct |
210 ms |
88824 KB |
Output is correct |
4 |
Correct |
207 ms |
88880 KB |
Output is correct |
5 |
Correct |
208 ms |
88876 KB |
Output is correct |
6 |
Correct |
241 ms |
88880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
211 ms |
88824 KB |
Output is correct |
2 |
Correct |
209 ms |
88884 KB |
Output is correct |
3 |
Correct |
210 ms |
88824 KB |
Output is correct |
4 |
Correct |
207 ms |
88880 KB |
Output is correct |
5 |
Correct |
208 ms |
88876 KB |
Output is correct |
6 |
Correct |
241 ms |
88880 KB |
Output is correct |
7 |
Execution timed out |
4011 ms |
88952 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
438 ms |
98040 KB |
Output is correct |
2 |
Correct |
436 ms |
100132 KB |
Output is correct |
3 |
Correct |
129 ms |
96488 KB |
Output is correct |
4 |
Correct |
153 ms |
96248 KB |
Output is correct |
5 |
Correct |
618 ms |
96264 KB |
Output is correct |
6 |
Correct |
469 ms |
96324 KB |
Output is correct |
7 |
Correct |
473 ms |
96400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
211 ms |
88824 KB |
Output is correct |
2 |
Correct |
209 ms |
88884 KB |
Output is correct |
3 |
Correct |
210 ms |
88824 KB |
Output is correct |
4 |
Correct |
207 ms |
88880 KB |
Output is correct |
5 |
Correct |
208 ms |
88876 KB |
Output is correct |
6 |
Correct |
241 ms |
88880 KB |
Output is correct |
7 |
Execution timed out |
4011 ms |
88952 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |