#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;
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;
for (int r = 0; r < n; ++r) {
parent[r] = -1;
depth[r] = 1;
dfs(r);
for (int v = 0; v < n; ++v) {
vector<int> now;
{
int x = v;
while (x != -1) {
now.push_back(x);
x = parent[x];
}
}
assert(sz(now) == depth[v]);
for (int mask = 0; mask < (1 << depth[v]); ++mask) {
if (__builtin_popcount(mask) <= k) {
ll jerry = 0, tom = 0;
memcpy(pp, p, sizeof(pp[0]) * n);
for (int i = 0; i < sz(now); ++i) {
jerry += pp[now[i]];
if (mask & 1 << i) {
for (int to: g[now[i]]) {
pp[now[i]] += pp[to];
pp[to] = 0;
}
}
}
for (int x : now)
tom += pp[x];
ans = max(ans, tom - jerry);
}
}
}
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2680 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2680 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Execution timed out |
4078 ms |
2772 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4070 ms |
11684 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2680 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Execution timed out |
4078 ms |
2772 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |