Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <assert.h>
- #include <bits/stdc++.h>
- using namespace std;
- #define dbg(...) logger(#__VA_ARGS__, __VA_ARGS__)
- template <typename... Args> void logger(string vars, Args &&...values)
- {
- cerr << vars << " = ";
- string delim = "";
- (..., (cerr << delim << values, delim = ", "));
- cerr << endl;
- }
- template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
- using ll = long long;
- using pii = pair<int, int>;
- using pll = pair<ll, ll>;
- int n;
- vector<int> g[100003];
- int val[100003];
- int vis[100003];
- pll dfs(int now)
- {
- // op_cnt, op_offset
- vis[now] = 1;
- int out_cnt = 0;
- ll op_acc = 0;
- ll op_offset = 0;
- for (auto out : g[now]) {
- if (vis[out] == 0) {
- auto op = dfs(out);
- op_acc += op.first;
- op_offset += op.second;
- ++out_cnt;
- }
- }
- if (out_cnt == 0) {
- return {abs(val[now]), -val[now]};
- }
- ll new_offset = -(op_offset + val[now]);
- ll new_op_acc = op_acc + abs(new_offset);
- // return {op_acc + abs(val[now] + op_offset), -val[now] + op_offset};
- return {new_op_acc, new_offset};
- }
- int main(int argc, char **argv)
- {
- scanf("%d", &n);
- int a, b;
- for (int i = 1; i < n; ++i) {
- scanf("%d%d", &a, &b);
- g[a].push_back(b);
- g[b].push_back(a);
- }
- for (int i = 1; i <= n; ++i)
- scanf("%d", val + i);
- auto ret = dfs(1);
- printf("%lld\n", ret.first);
- return 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement