#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long int ll;
const int maxn = 1e5+10;
int n, v, vet[maxn];
ll dp[maxn][110], soma[maxn];
vector < int > grafo[maxn];
ll dfs(int u, int val, int pai)
{
if(val == 0) return 0;
if(dp[u][val]) return dp[u][val];
ll resp1 = 0, resp2 = 0, cont = 0;
for(int i = 0 ; i < (int) grafo[u].size() ; i++)
{
int v = grafo[u][i];
if(v == pai) continue;
dp[u][val] = max({dp[u][val], dfs(v, val, u), dfs(v, val-1, u) + soma[v] - vet[u]});
}
return dp[u][val];
}
void roda(int u, int pai) {
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> v;
for(int i = 1 ; i <= n ; i++) cin >> vet[i];
for(int i = 0 ; i < n-1 ; i++)
{
int a, b;
cin >> a >> b;
grafo[a].pb(b);
grafo[b].pb(a);
}
for(int i = 1; i <= n; i++) {
for(auto v : grafo[i]) {
soma[i] += vet[v];
}
}
ll resp = max(dfs(1, v, 0), dfs(1, v-1, 0) + soma[1]);
if(n <= 1000)
{
for(int i = 2 ; i <= n ; i++)
{
for(int j = 0 ; j <= n ; j++)
for(int k = 0 ; k <= v ; k++) dp[j][k] = 0;
resp = max({resp, dfs(i, v, 0), dfs(i, v-1, 0) + soma[i]});
}
}
cout << resp << endl;
}
Compilation message
chase.cpp: In function 'll dfs(int, int, int)':
chase.cpp:18:5: warning: unused variable 'resp1' [-Wunused-variable]
ll resp1 = 0, resp2 = 0, cont = 0;
^~~~~
chase.cpp:18:16: warning: unused variable 'resp2' [-Wunused-variable]
ll resp1 = 0, resp2 = 0, cont = 0;
^~~~~
chase.cpp:18:27: warning: unused variable 'cont' [-Wunused-variable]
ll resp1 = 0, resp2 = 0, cont = 0;
^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2628 KB |
Output is correct |
2 |
Correct |
4 ms |
2688 KB |
Output is correct |
3 |
Correct |
6 ms |
2688 KB |
Output is correct |
4 |
Correct |
4 ms |
2688 KB |
Output is correct |
5 |
Incorrect |
4 ms |
2688 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2628 KB |
Output is correct |
2 |
Correct |
4 ms |
2688 KB |
Output is correct |
3 |
Correct |
6 ms |
2688 KB |
Output is correct |
4 |
Correct |
4 ms |
2688 KB |
Output is correct |
5 |
Incorrect |
4 ms |
2688 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
313 ms |
54844 KB |
Output is correct |
2 |
Correct |
323 ms |
54800 KB |
Output is correct |
3 |
Correct |
63 ms |
7536 KB |
Output is correct |
4 |
Correct |
175 ms |
77868 KB |
Output is correct |
5 |
Correct |
663 ms |
78920 KB |
Output is correct |
6 |
Correct |
648 ms |
78852 KB |
Output is correct |
7 |
Correct |
656 ms |
78752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2628 KB |
Output is correct |
2 |
Correct |
4 ms |
2688 KB |
Output is correct |
3 |
Correct |
6 ms |
2688 KB |
Output is correct |
4 |
Correct |
4 ms |
2688 KB |
Output is correct |
5 |
Incorrect |
4 ms |
2688 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |