#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using namespace atcoder; typedef long long ll; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repr(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define repk(i, k, n) for (int i = k; i < (int)(n); i++) #define all(v) v.begin(), v.end() #define mod1 1000000007 #define mod2 998244353 #define mod3 100000007 #define vi vector #define vs vector #define vc vector #define vl vector #define vb vector #define vvi vector> #define vvc vector> #define vvl vector> #define vvb vector> #define vvvi vector>> #define vvvl vector>> #define pii pair #define pil pair #define pli pair #define pll pair #define vpii vector> #define vpll vector> #define vvpii vector>> #define vvpll vector>> using mint = modint998244353; template void debug(T e) { cerr << e << endl; } template void debug(vector &v) { rep(i, v.size()) { cerr << v[i] << " "; } cerr << endl; } template void debug(vector> &v) { rep(i, v.size()) { rep(j, v[i].size()) { cerr << v[i][j] << " "; } cerr << endl; } } template void debug(vector> &v) { rep(i, v.size()) { cerr << v[i].first << " " << v[i].second << endl; } } template void debug(set &st) { for (auto itr = st.begin(); itr != st.end(); itr++) { cerr << *itr << " "; } cerr << endl; } template void debug(multiset &ms) { for (auto itr = ms.begin(); itr != ms.end(); itr++) { cerr << *itr << " "; } cerr << endl; } template void debug(map &mp) { for (auto itr = mp.begin(); itr != mp.end(); itr++) { cerr << itr->first << " " << itr->second << endl; } } void debug_out() { cerr << endl; } template void debug_out(Head H, Tail... T) { cerr << H << " "; debug_out(T...); } ll my_pow(ll x, ll n, ll mod) { //  繰り返し二乗法.x^nをmodで割った余り. ll ret; if (n == 0) { ret = 1; } else if (n % 2 == 1) { ret = (x * my_pow((x * x) % mod, n / 2, mod)) % mod; } else { ret = my_pow((x * x) % mod, n / 2, mod); } return ret; } ll inv(ll x, ll mod) { return my_pow(x, mod - 2, mod); } struct BMS { // 2 個の multiset を用いて中央値を取得する機構 multiset ms1; multiset ms2; ll sum1 = 0; ll sum2 = 0; void balance() { if (ms1.size() < ms2.size()) { auto itr = ms2.begin(); ms2.erase(itr); sum2 -= *itr; ms1.insert(*itr); sum1 += *itr; } if (ms1.size() >= ms2.size() + 2) { auto itr = ms1.end(); itr--; ms1.erase(itr); sum1 -= *itr; ms2.insert(*itr); sum2 += *itr; } } void insert(ll v) { if (v >= *(ms2.begin())){ ms2.insert(v); sum2 += v; } else{ ms1.insert(v); sum1 += v; } balance(); } void erase(ll v) { // 「ひとつだけ」削除する if (ms1.find(v) != ms1.end()) { ms1.erase(ms1.find(v)); sum1 -= v; } else if (ms2.find(v) != ms2.end()) { ms2.erase(ms2.find(v)); sum2 -= v; } else { return; } balance(); } ll get_size() { ll siz1 = ms1.size(); ll siz2 = ms2.size(); return siz1 + siz2; } ll get_mean() { ll ret; if (ms1.size() > ms2.size()) { ret = *(ms1.rbegin()); } else { ll ms1max = *(ms1.rbegin()); ll ms2min = *(ms2.begin()); ret = (ms1max + ms2min) / 2; } return ret; } ll get_score() { ll mean = get_mean(); ll score = (mean * ms1.size() - sum1) + (sum2 - mean * ms2.size()); return score; } ll get_min_val() { return *(ms1.begin()); } ll get_max_val() { return *(ms2.rbegin()); } }; ll INF = 1000000000000000002; void dfs(vector> &graph, vector &isvisited, vector &ans, BMS &bms, vector &A, ll v) { isvisited[v] = true; bms.insert(A[v]); // この状態で問題を解く if (bms.get_size() <= 2) { ans[v] = -1; } else { ll anssub = INF; ll min_val = bms.get_min_val(); bms.erase(min_val); anssub = min(anssub, bms.get_score()); bms.insert(min_val); ll max_val = bms.get_max_val(); bms.erase(max_val); anssub = min(anssub, bms.get_score()); bms.insert(max_val); ans[v] = anssub; } for (ll i = 0; i < graph[v].size(); i++) { ll x = graph[v][i]; if (isvisited[x]) { continue; } dfs(graph, isvisited, ans, bms, A, graph[v][i]); } bms.erase(A[v]); } int main() { ll N; cin >> N; vector A(N + 1); for (ll i = 0; i < N + 1; i++) { cin >> A[i]; } vector u(N); vector v(N); for (ll i = 0; i < N; i++) { cin >> u[i] >> v[i]; } vector> graph(N + 1, vector(0)); for (ll i = 0; i < N; i++) { graph[u[i]].push_back(v[i]); graph[v[i]].push_back(u[i]); } BMS bms; vector ans(N + 1, -1); vector isvisited(N + 1, false); dfs(graph, isvisited, ans, bms, A, 0); for (ll i = 1; i <= N; i++) { cout << ans[i] << endl; } }