#include <bits/stdc++.h>
using namespace std;
#define sz(x) ((int)size(x))
#define all(x) begin(x), end(x)
#define trace(x) cout << #x << ": " << (x) << endl;
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l, int r) { return (int) ((ll) rnd() % (r - l + 1)) + l; }
const int N = 100001;
const ll infL = 3e18;
vector<int> g[N];
int n, k, parent[N], depth[N];
ll p[N], pp[N];
void dfs(int v) {
for (int to: g[v]) {
if (to != parent[v]) {
parent[to] = v;
depth[to] = depth[v] + 1;
dfs(to);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> k;
for (int i = 0; i < n; ++i) {
cin >> p[i];
}
for (int i = 1; i < n; ++i) {
int a, b;
cin >> a >> b;
--a, --b;
g[a].push_back(b);
g[b].push_back(a);
}
ll ans = 0;
if (n <= 1000) {
for (int s = 0; s < n; ++s) {
parent[s] = -1;
depth[s] = 1;
dfs(s);
for (int t = 0; t < n; ++t) {
vector<int> now;
{
int x = t;
while (x != -1) {
now.push_back(x);
x = parent[x];
}
}
assert(sz(now) == depth[t]);
vector<ll> dp[2], dq[2];
for (int i = 0; i < 2; ++i)
dp[i].resize(k + 1, -infL), dq[i].resize(k + 1, -infL);
for (int i = 0; i < sz(now); ++i) {
fill(all(dp[0]), 0);
fill(all(dp[1]), 0);
int v = now[i];
ll sum = 0;
for (int to: g[v]) {
if (i == 0 || to != now[i - 1])
sum += p[to];
}
dp[1][1] = sum;
dp[1][0] = -infL;
// if (i) {
for (int cnt = 1; cnt <= k; ++cnt) {
// {
// //took previous
// dp[1][cnt] = dq[1][cnt - 1] + sum;
// dp[0][cnt] = dq[1][cnt];
// }
// {
// //didn't take previous
// dp[1][cnt] = dq[0][cnt - 1]
// }
dp[0][cnt] = max(dq[0][cnt], dq[1][cnt]);
dp[1][cnt] = max(dp[1][cnt], max(dq[0][cnt - 1], dq[1][cnt - 1]) + sum);
}
// }
for (int j = 0; j < 2; ++j)
swap(dp[j], dq[j]);
}
for (int cnt = 0; cnt <= k; ++cnt) {
ans = max({ans, dq[1][cnt], dq[0][cnt]});
}
}
}
cout << ans;
} else {
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2660 KB |
Output is correct |
2 |
Correct |
2 ms |
2692 KB |
Output is correct |
3 |
Correct |
2 ms |
2656 KB |
Output is correct |
4 |
Correct |
2 ms |
2656 KB |
Output is correct |
5 |
Correct |
2 ms |
2656 KB |
Output is correct |
6 |
Correct |
2 ms |
2656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2660 KB |
Output is correct |
2 |
Correct |
2 ms |
2692 KB |
Output is correct |
3 |
Correct |
2 ms |
2656 KB |
Output is correct |
4 |
Correct |
2 ms |
2656 KB |
Output is correct |
5 |
Correct |
2 ms |
2656 KB |
Output is correct |
6 |
Correct |
2 ms |
2656 KB |
Output is correct |
7 |
Execution timed out |
4083 ms |
2728 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
39 ms |
8636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2660 KB |
Output is correct |
2 |
Correct |
2 ms |
2692 KB |
Output is correct |
3 |
Correct |
2 ms |
2656 KB |
Output is correct |
4 |
Correct |
2 ms |
2656 KB |
Output is correct |
5 |
Correct |
2 ms |
2656 KB |
Output is correct |
6 |
Correct |
2 ms |
2656 KB |
Output is correct |
7 |
Execution timed out |
4083 ms |
2728 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |