#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, k, x, y;
vector<int> a, b;
vector<vector<int>> graph, dp;
void dfs(int i, int p) {
for(auto j : graph[i]) {
if(j != p) dfs(j, i);
}
for(int j = 0; j <= k; j++) {
for(auto z : graph[i]) {
if(z != p) {
dp[i][j] = max(dp[i][j], max(dp[z][j], (j == k || dp[z][j + 1] == LLONG_MIN ? LLONG_MIN : dp[z][j + 1] + b[i] - (p == -1 ? 0 : a[p]))));
}
}
}
}
signed main() {
cin >> n >> k;
a = vector<int>(n), b = vector<int>(n), graph = vector<vector<int>>(n);
for(int i = 0; i < n; i++) cin >> a[i];
for(int i = 0; i < n - 1; i++) {
cin >> x >> y;
x--, y--;
graph[x].push_back(y);
graph[y].push_back(x);
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < graph[i].size(); j++) b[i] += a[graph[i][j]];
}
//
dp = vector<vector<int>>(n, vector<int>(k + 1));
for(int i = 0; i < n; i++) dp[i][k] = 0;
dfs(0, -1);
//
x = 0;
for(int j = 0; j <= k; j++) x = max(x, dp[0][j]);
cout << x << "\n";
}
Compilation message
chase.cpp: In function 'int main()':
chase.cpp:34:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for(int j = 0; j < graph[i].size(); j++) b[i] += a[graph[i][j]];
| ~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
170 ms |
92568 KB |
Output is correct |
2 |
Correct |
183 ms |
92500 KB |
Output is correct |
3 |
Correct |
210 ms |
90336 KB |
Output is correct |
4 |
Correct |
87 ms |
13396 KB |
Output is correct |
5 |
Correct |
185 ms |
90196 KB |
Output is correct |
6 |
Correct |
187 ms |
90280 KB |
Output is correct |
7 |
Correct |
182 ms |
90272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |