#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;
ll p[N], ans = 0;
multiset<ll> st;
ll sum_set = 0;
void dfs(int v, int par) {
ll sum = 0;
for (int to : g[v]) {
if (to != par) {
sum += p[to];
}
}
bool add = false;
ll del = -1;
if (sz(st) < k) {
st.insert(sum);
sum_set += sum;
add = true;
} else {
assert(!st.empty());
if (*st.begin() < sum) {
del = *st.begin();
sum_set -= del;
st.erase(st.begin());
add = true;
sum_set += sum;
st.insert(sum);
}
}
ans = max(ans, sum_set);
for (int to: g[v]) {
if (to != par) {
dfs(to, v);
}
}
if (add) {
sum_set -= sum;
st.extract(sum);
}
if (del != -1) {
sum_set += del;
st.insert(del);
}
}
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);
}
if (k == 0) {
cout << 0;
return 0;
}
if (n <= 1000) {
for (int s = 0; s < n; ++s) {
dfs(s, -1);
}
cout << ans;
} else {
dfs(0, -1);
cout << ans;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
92 ms |
2740 KB |
Output is correct |
8 |
Correct |
20 ms |
2644 KB |
Output is correct |
9 |
Correct |
43 ms |
2644 KB |
Output is correct |
10 |
Correct |
82 ms |
2708 KB |
Output is correct |
11 |
Correct |
80 ms |
2700 KB |
Output is correct |
12 |
Correct |
51 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
10536 KB |
Output is correct |
2 |
Correct |
42 ms |
12552 KB |
Output is correct |
3 |
Correct |
34 ms |
9052 KB |
Output is correct |
4 |
Correct |
42 ms |
8700 KB |
Output is correct |
5 |
Correct |
53 ms |
8780 KB |
Output is correct |
6 |
Correct |
53 ms |
8780 KB |
Output is correct |
7 |
Correct |
55 ms |
8784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
2644 KB |
Output is correct |
7 |
Correct |
92 ms |
2740 KB |
Output is correct |
8 |
Correct |
20 ms |
2644 KB |
Output is correct |
9 |
Correct |
43 ms |
2644 KB |
Output is correct |
10 |
Correct |
82 ms |
2708 KB |
Output is correct |
11 |
Correct |
80 ms |
2700 KB |
Output is correct |
12 |
Correct |
51 ms |
2644 KB |
Output is correct |
13 |
Correct |
45 ms |
10536 KB |
Output is correct |
14 |
Correct |
42 ms |
12552 KB |
Output is correct |
15 |
Correct |
34 ms |
9052 KB |
Output is correct |
16 |
Correct |
42 ms |
8700 KB |
Output is correct |
17 |
Correct |
53 ms |
8780 KB |
Output is correct |
18 |
Correct |
53 ms |
8780 KB |
Output is correct |
19 |
Correct |
55 ms |
8784 KB |
Output is correct |
20 |
Incorrect |
49 ms |
8672 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |