#include <bits/stdc++.h>
#ifdef LOCAL
#define dbg(x) cerr << "dgb: " << x << "\n";
#else
#define dbg(x)
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,tune=native")
#endif
using namespace std;
#define int long long
int n;
vector<int> c;
vector<int> sum;
vector<vector<int>> g;
int f(int v, int p, int k) {
if (k == 0)
return 0;
int maxi = 0;
for (int w : g[v]) {
if (w == p)
continue;
maxi = max(maxi, f(w, v, k - 1) + sum[v] - ((p == -1) ? 0 : c[p]));
maxi = max(maxi, f(w, v, k));
}
return maxi;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int k;
cin >> n >> k;
c.resize(n);
g.resize(n);
sum.assign(n, 0);
for (int &x : c)
cin >> x;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
g[b].push_back(a);
g[a].push_back(b);
sum[a] += c[b];
sum[b] += c[a];
}
if (n <= 10) {
int best = 0;
for (int root = 0; root < n; root++)
best = max(best, f(root, -1, k));
cout << best << "\n";
} else {
cout << f(1, -1, k) << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Execution timed out |
4069 ms |
332 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4081 ms |
13356 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Execution timed out |
4069 ms |
332 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |