#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#define int long long
#define rep(i,n) for(int i = 0; i < n; i++)
using namespace std;
int n,v;
vector<int>p;
vector<vector<int>>mem;
vector<vector<int>>E;
int dfs(int pos, int bc, int parent)
{
if(mem[pos][bc]!=-1) return mem[pos][bc];
if(bc==0) return 0;
int sum = 0;
for(int w : E[pos])
{
if(w==parent) continue;
sum+=p[w];
}
int maxx = 0;
for(int w : E[pos])
{
if(w==parent) continue;
maxx=max(maxx, dfs(w, bc, pos));
maxx=max(maxx, dfs(w, bc-1, pos) + sum);
}
return mem[pos][bc] = maxx;
}
int bruteforce()
{
int maxx=0;
rep(i, n)
{
mem.assign(n+1, vector<int>(v+1, -1));
maxx=max(maxx, dfs(i+1, v, -1));
}
return maxx;
}
signed main()
{
cin>>n>>v;
p.resize(n+1);
rep(i, n) cin>>p[i+1];
E.resize(n+1);
mem.assign(n+1, vector<int>(v+1, -1));
rep(i,n-1)
{
int a,b;
cin>>a>>b;
E[a].push_back(b);
E[b].push_back(a);
}
int maxx = dfs(1, v, -1);
if(n<=1000) maxx = bruteforce();
cout<<maxx<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
288 KB |
Output is correct |
4 |
Correct |
1 ms |
288 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
288 KB |
Output is correct |
4 |
Correct |
1 ms |
288 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
2357 ms |
1232 KB |
Output is correct |
8 |
Correct |
133 ms |
460 KB |
Output is correct |
9 |
Correct |
47 ms |
460 KB |
Output is correct |
10 |
Correct |
352 ms |
1220 KB |
Output is correct |
11 |
Correct |
309 ms |
588 KB |
Output is correct |
12 |
Correct |
300 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
596 ms |
94788 KB |
Output is correct |
2 |
Correct |
584 ms |
94760 KB |
Output is correct |
3 |
Correct |
170 ms |
89400 KB |
Output is correct |
4 |
Correct |
153 ms |
12612 KB |
Output is correct |
5 |
Correct |
782 ms |
89436 KB |
Output is correct |
6 |
Correct |
687 ms |
89360 KB |
Output is correct |
7 |
Correct |
773 ms |
89356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
288 KB |
Output is correct |
4 |
Correct |
1 ms |
288 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
2357 ms |
1232 KB |
Output is correct |
8 |
Correct |
133 ms |
460 KB |
Output is correct |
9 |
Correct |
47 ms |
460 KB |
Output is correct |
10 |
Correct |
352 ms |
1220 KB |
Output is correct |
11 |
Correct |
309 ms |
588 KB |
Output is correct |
12 |
Correct |
300 ms |
460 KB |
Output is correct |
13 |
Correct |
596 ms |
94788 KB |
Output is correct |
14 |
Correct |
584 ms |
94760 KB |
Output is correct |
15 |
Correct |
170 ms |
89400 KB |
Output is correct |
16 |
Correct |
153 ms |
12612 KB |
Output is correct |
17 |
Correct |
782 ms |
89436 KB |
Output is correct |
18 |
Correct |
687 ms |
89360 KB |
Output is correct |
19 |
Correct |
773 ms |
89356 KB |
Output is correct |
20 |
Incorrect |
316 ms |
91436 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |