-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathABC348E.cpp
46 lines (46 loc) · 1.23 KB
/
ABC348E.cpp
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
#include <bits/extc++.h>
using namespace std;
namespace pbds = __gnu_pbds;
using ui = unsigned int;
using uli = unsigned long long int;
using li = long long int;
using graph = vector<vector<size_t>>;
uli get_childtree_dissum(graph const& mp, vector<uli> const& v, vector<uli>& vs,
size_t p, size_t fa = -1) {
vs[p] = v[p];
uli ans = 0;
for (size_t i : mp[p])
if (i != fa) {
ans += get_childtree_dissum(mp, v, vs, i, p);
ans += vs[i];
vs[p] += vs[i];
}
return ans;
}
void get_dissum(graph const& mp, vector<uli>& ans, vector<uli> const& vs,
size_t p, size_t fa = -1) {
for (size_t i : mp[p])
if (i != fa) {
ans[i] = ans[p] - vs[i] + (vs[0] - vs[i]);
get_dissum(mp, ans, vs, i, p);
}
}
int main(void) {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
size_t n;
cin >> n;
graph mp(n);
for (size_t i = 1; i < n; ++i) {
size_t x, y;
cin >> x >> y;
--x, --y;
mp[x].emplace_back(y), mp[y].emplace_back(x);
}
vector<uli> v(n);
for (uli& i : v) cin >> i;
vector<uli> ans(n), vs(n);
ans[0] = get_childtree_dissum(mp, v, vs, 0);
get_dissum(mp, ans, vs, 0);
cout << *min_element(ans.cbegin(), ans.cend());
return 0;
}