#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
#define int int64_t
using namespace std;
vector<int> nearbyPigeons;
vector<int> pigeons;
vector<vector<int>> graph;
int crumbs;
void findNearPigeons(int v, int p) {
if (p!=-1) nearbyPigeons[v] += pigeons[p];
for (int i: graph[v]) {
if (i==p) continue;
nearbyPigeons[v] += pigeons[i];
findNearPigeons(i, v);
}
}
vector<int> options; // all paths
map<int, int> localOptions; // one path for selecting crumbs
void findBestOptions(int v, int p) {
int addToLocalOptions;
if (p==-1) addToLocalOptions = nearbyPigeons[v];
else addToLocalOptions = nearbyPigeons[v]-pigeons[p];
for (int i: graph[v]) {
if (i==p) continue;
localOptions[addToLocalOptions]++;
findBestOptions(i, v);
localOptions[addToLocalOptions]--;
if (localOptions[addToLocalOptions] == 0) localOptions.erase(addToLocalOptions);
}
localOptions[addToLocalOptions]++;
int addToOptions = 0;
auto pos = localOptions.end();
auto limit = localOptions.begin();
pos--;
int used = crumbs;
while (used>0 && pos!=limit) {
addToOptions+=(*pos).first*min((*pos).second, used);
used -= min((*pos).second, used);
pos--;
}
if (used) {
addToOptions+=(*pos).first*min((*pos).second, used);
used -= min((*pos).second, used);
}
assert(used >= 0);
options.push_back(addToOptions);
localOptions[addToLocalOptions]--;
if (localOptions[addToLocalOptions] == 0) localOptions.erase(addToLocalOptions);
}
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
int n; cin >> n >> crumbs;
pigeons.assign(n, 0);
nearbyPigeons.assign(n, 0);
for (int i = 0; i<n; i++) cin >> pigeons[i];
graph.assign(n, vector<int>(0));
for (int i = 1; i<n; i++) {
int a, b; cin >> a >> b;
a--; b--;
graph[a].push_back(b);
graph[b].push_back(a);
}
findNearPigeons(0, -1);
// for (int i: nearbyPigeons) {
// cout << i << " ";
// }
// cout << "\n";
int best = 0;
for (int i = 0; i<1; i++) {
options.clear();
findBestOptions(i, -1);
// for (int i: options) {
// cout << i << " ";
// }
// cout << "\n";
best = max(best, *max_element(options.begin(), options.end()));
}
cout << best << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
273 ms |
17820 KB |
Output is correct |
2 |
Correct |
268 ms |
19472 KB |
Output is correct |
3 |
Correct |
58 ms |
11256 KB |
Output is correct |
4 |
Correct |
93 ms |
10684 KB |
Output is correct |
5 |
Correct |
120 ms |
10628 KB |
Output is correct |
6 |
Correct |
121 ms |
10628 KB |
Output is correct |
7 |
Correct |
124 ms |
10684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |