Repository navigation
Expand file tree
/
Copy path1466d.cpp
More file actions
73 lines (53 loc) · 1.39 KB
/
Copy path1466d.cpp
File metadata and controls
73 lines (53 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <bits/stdc++.h>
using namespace std;
#define ll long long
// For a colour k: there should only be one connected component with that
// colour (greedy). When you add a colour, we want to maximize the number
// of times a vertex with the maximum value shows up. How many times can
// we do that? The number of children it has. Use a map for implementation.
void solve() {
int n;
cin >> n;
vector<int> w(n + 2);
ll ans = 0;
for (int i = 1; i <= n; i++) {
cin >> w[i];
ans += w[i];
}
vector<vector<int>> adj(n + 2);
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
map<int, int, greater<>> cnt;
auto dfs = [&] (auto dfs, int u, int par) -> void {
cnt[w[u]] += adj[u].size() - 1;
for (auto v : adj[u]) {
if (v != par) {
dfs(dfs, v, u);
}
}
};
dfs(dfs, 1, -1);
cout << ans << ' ';
for (int i = 2; i < n; i++) {
while ((*cnt.begin()).second == 0) {
cnt.erase(cnt.begin());
}
ans += (ll) (*cnt.begin()).first;
(*cnt.begin()).second--;
cout << ans << ' ';
}
cout << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
cin >> t;
while (t--) {
solve();
}
}