#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, k, x, y, ia, ib, va, vb, ic, id;
bool flag = true;
vector<int> a, b;
vector<vector<int>> graph, dp, dpe;
void dfs(int i, int p) {
for(auto j : graph[i]) {
if(j != p) dfs(j, i);
}
//
y = 0;
for(int j = 0; j <= k; j++) {
ia = -1, ib = -1, va = 0, vb = 0, ic = -1, id = -1;
for(auto z : graph[i]) {
dp[i][j] = max(dp[i][j], max(dp[z][j], (j == k ? 0 : (z == p ? 0 : dp[z][j + 1]) + b[i] - (p == -1 ? 0 : a[p]))));
dpe[i][j] = max(dpe[i][j], max(dpe[z][j], (j == k ? 0 : (z == p ? 0 : dpe[z][j + 1]) + b[i] - (j + 1 == k ? 0 : a[z]))));
//
y = max(y, max(dp[i][j], (j == k ? 0 : (z == p ? 0 : dp[z][j + 1]) + b[i])));
y = max(y, dpe[i][j]);
//
int temp = max((z == p ? 0 : dpe[z][j]), (j == k ? 0 : (z == p ? 0 : dpe[z][j + 1]) + b[i] - a[z]));
if(ia == -1 || temp > va) {
ib = ia, vb = va, ia = z, va = temp;
} else if(ib == -1 || temp > vb) {
ib = z, vb = temp;
}
//
if(ic == -1 || dp[z][k - j] > dp[ic][k - j]) {
id = ic, ic = z;
} else if(id == -1 || dp[z][k - j] > dp[id][k - j]) {
id = z;
}
}
if(ia != ic && ia != -1 && ic != -1) y = max(y, va + dp[ic][k - j]);
if(ib != ic && ib != -1 && ic != -1) y = max(y, vb + dp[ic][k - j]);
if(ia != id && ia != -1 && id != -1) y = max(y, va + dp[id][k - j]);
}
x = max(x, y);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
a = vector<int>(n), b = vector<int>(n), graph = vector<vector<int>>(n);
int sum = 0, ind = 1;
for(int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
if(i != 0 && a[ind] > a[i]) {
ind = i;
}
}
//cout << sum - a[ind] << "\n";
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)), dpe = vector<vector<int>>(n, vector<int>(k + 1));
x = 0;
dfs(0, -1);
//
cout << x << "\n";
//cout << dpe[0][99] << " " << dpe[0][98] << " " << ind << " " << dpe[ind][99] << "\n";
//for(auto j : graph[ind]) cout << j << "\n";
}
Compilation message
chase.cpp: In function 'int main()':
chase.cpp:67: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]
67 | for(int j = 0; j < graph[i].size(); j++) b[i] += a[graph[i][j]];
| ~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
2140 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
2136 KB |
Output is correct |
11 |
Correct |
3 ms |
856 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
359 ms |
179476 KB |
Output is correct |
2 |
Correct |
354 ms |
179284 KB |
Output is correct |
3 |
Correct |
679 ms |
172740 KB |
Output is correct |
4 |
Correct |
90 ms |
19068 KB |
Output is correct |
5 |
Correct |
394 ms |
172484 KB |
Output is correct |
6 |
Correct |
382 ms |
172500 KB |
Output is correct |
7 |
Correct |
374 ms |
172496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
2140 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
2136 KB |
Output is correct |
11 |
Correct |
3 ms |
856 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
359 ms |
179476 KB |
Output is correct |
14 |
Correct |
354 ms |
179284 KB |
Output is correct |
15 |
Correct |
679 ms |
172740 KB |
Output is correct |
16 |
Correct |
90 ms |
19068 KB |
Output is correct |
17 |
Correct |
394 ms |
172484 KB |
Output is correct |
18 |
Correct |
382 ms |
172500 KB |
Output is correct |
19 |
Correct |
374 ms |
172496 KB |
Output is correct |
20 |
Correct |
381 ms |
172380 KB |
Output is correct |
21 |
Correct |
65 ms |
19072 KB |
Output is correct |
22 |
Correct |
372 ms |
172492 KB |
Output is correct |
23 |
Correct |
77 ms |
19068 KB |
Output is correct |
24 |
Correct |
409 ms |
172488 KB |
Output is correct |
25 |
Correct |
628 ms |
172484 KB |
Output is correct |
26 |
Correct |
358 ms |
179384 KB |
Output is correct |
27 |
Correct |
394 ms |
179792 KB |
Output is correct |