#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
typedef vector<long long> vi;
vector<vi> g;
vector<vector<vector<long long> > > m;
vi d;
//gives the max difference
long long solve(long long n, long long p, long long v){
if(v==0)
return 0;
// if(m[n][p][v]!=-1)
// return m[n][p][v];
long long sum = 0;
for(long long x=0; x<g[n].size(); x++){
if(g[n][x]!=p){
sum += d[g[n][x]];
}
}
long long sol = sum;
for(long long x=0; x<g[n].size(); x++){
if(g[n][x]!=p){
if(m[n][x][v]==-1)
m[n][x][v] = solve(g[n][x], n, v);
if(m[n][x][v-1]==-1)
m[n][x][v-1] = solve(g[n][x], n, v-1);
sol = max(sol, max(sum+m[n][x][v-1], m[n][x][v]));
}
}
return sol;//m[n][p][v] = sol;
}
int main(){
iostream::sync_with_stdio(false); cin.tie(0);
// memset(m, -1, sizeof(m));
long long N, V; cin>>N>>V;
g.assign(N+1, vi());
d.assign(N+1, 0);
long long A, B;
for(long long n=1; n<=N; n++)
cin>>d[n];
for(long long n=1; n<N; n++){
cin>>A>>B;
g[A].push_back(B);
g[B].push_back(A);
}
m.assign(N+1, vector<vector<long long> >());
for(int x=1; x<=N; x++)
m[x].assign(g[x].size(), vector<long long>(110, -1));
long long sol = 0;
sol = solve(1, 0, V);
for(long long x=2; x<=N&&N<10000; x++)
sol = max(sol, solve(x, 0, V));
cout << sol << endl;
return 0;
}
Compilation message
chase.cpp: In function 'long long int solve(long long int, long long int, long long int)':
chase.cpp:19:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(long long x=0; x<g[n].size(); x++){
~^~~~~~~~~~~~
chase.cpp:25:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(long long x=0; x<g[n].size(); x++){
~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
488 KB |
Output is correct |
3 |
Correct |
2 ms |
660 KB |
Output is correct |
4 |
Correct |
2 ms |
660 KB |
Output is correct |
5 |
Correct |
2 ms |
692 KB |
Output is correct |
6 |
Correct |
2 ms |
692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
488 KB |
Output is correct |
3 |
Correct |
2 ms |
660 KB |
Output is correct |
4 |
Correct |
2 ms |
660 KB |
Output is correct |
5 |
Correct |
2 ms |
692 KB |
Output is correct |
6 |
Correct |
2 ms |
692 KB |
Output is correct |
7 |
Correct |
9 ms |
2508 KB |
Output is correct |
8 |
Correct |
6 ms |
2552 KB |
Output is correct |
9 |
Correct |
18 ms |
2552 KB |
Output is correct |
10 |
Correct |
5 ms |
2552 KB |
Output is correct |
11 |
Correct |
7 ms |
2552 KB |
Output is correct |
12 |
Correct |
6 ms |
2552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
677 ms |
197836 KB |
Output is correct |
2 |
Correct |
659 ms |
197880 KB |
Output is correct |
3 |
Correct |
251 ms |
197880 KB |
Output is correct |
4 |
Correct |
297 ms |
197880 KB |
Output is correct |
5 |
Correct |
997 ms |
197880 KB |
Output is correct |
6 |
Correct |
907 ms |
197880 KB |
Output is correct |
7 |
Correct |
937 ms |
197880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
488 KB |
Output is correct |
3 |
Correct |
2 ms |
660 KB |
Output is correct |
4 |
Correct |
2 ms |
660 KB |
Output is correct |
5 |
Correct |
2 ms |
692 KB |
Output is correct |
6 |
Correct |
2 ms |
692 KB |
Output is correct |
7 |
Correct |
9 ms |
2508 KB |
Output is correct |
8 |
Correct |
6 ms |
2552 KB |
Output is correct |
9 |
Correct |
18 ms |
2552 KB |
Output is correct |
10 |
Correct |
5 ms |
2552 KB |
Output is correct |
11 |
Correct |
7 ms |
2552 KB |
Output is correct |
12 |
Correct |
6 ms |
2552 KB |
Output is correct |
13 |
Correct |
677 ms |
197836 KB |
Output is correct |
14 |
Correct |
659 ms |
197880 KB |
Output is correct |
15 |
Correct |
251 ms |
197880 KB |
Output is correct |
16 |
Correct |
297 ms |
197880 KB |
Output is correct |
17 |
Correct |
997 ms |
197880 KB |
Output is correct |
18 |
Correct |
907 ms |
197880 KB |
Output is correct |
19 |
Correct |
937 ms |
197880 KB |
Output is correct |
20 |
Incorrect |
384 ms |
197880 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |