#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];
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++)
{
memset(dp, 0, sizeof dp);
resp = max({resp, dfs(i, v, 0), dfs(i, v-1, 0) + soma[i]});
}
}
cout << resp << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
215 ms |
88824 KB |
Output is correct |
2 |
Correct |
222 ms |
88864 KB |
Output is correct |
3 |
Correct |
221 ms |
88800 KB |
Output is correct |
4 |
Correct |
223 ms |
88876 KB |
Output is correct |
5 |
Incorrect |
226 ms |
88884 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
215 ms |
88824 KB |
Output is correct |
2 |
Correct |
222 ms |
88864 KB |
Output is correct |
3 |
Correct |
221 ms |
88800 KB |
Output is correct |
4 |
Correct |
223 ms |
88876 KB |
Output is correct |
5 |
Incorrect |
226 ms |
88884 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
324 ms |
54900 KB |
Output is correct |
2 |
Correct |
326 ms |
54856 KB |
Output is correct |
3 |
Correct |
64 ms |
7536 KB |
Output is correct |
4 |
Correct |
150 ms |
77944 KB |
Output is correct |
5 |
Correct |
666 ms |
79004 KB |
Output is correct |
6 |
Correct |
598 ms |
78944 KB |
Output is correct |
7 |
Correct |
776 ms |
78812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
215 ms |
88824 KB |
Output is correct |
2 |
Correct |
222 ms |
88864 KB |
Output is correct |
3 |
Correct |
221 ms |
88800 KB |
Output is correct |
4 |
Correct |
223 ms |
88876 KB |
Output is correct |
5 |
Incorrect |
226 ms |
88884 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |