結果

問題 No.2654 [Cherry 6th Tune] Re: start! (Black Sheep)
ユーザー KudeKude
提出日時 2024-02-23 23:23:26
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 341 ms / 7,000 ms
コード長 2,261 bytes
コンパイル時間 4,949 ms
コンパイル使用メモリ 284,140 KB
実行使用メモリ 59,340 KB
最終ジャッジ日時 2024-02-23 23:23:44
合計ジャッジ時間 15,590 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 230 ms
17,616 KB
testcase_04 AC 207 ms
16,832 KB
testcase_05 AC 109 ms
11,356 KB
testcase_06 AC 32 ms
6,676 KB
testcase_07 AC 88 ms
10,108 KB
testcase_08 AC 120 ms
12,128 KB
testcase_09 AC 246 ms
17,788 KB
testcase_10 AC 31 ms
6,676 KB
testcase_11 AC 156 ms
13,100 KB
testcase_12 AC 24 ms
6,676 KB
testcase_13 AC 158 ms
12,708 KB
testcase_14 AC 319 ms
21,140 KB
testcase_15 AC 277 ms
20,420 KB
testcase_16 AC 140 ms
11,428 KB
testcase_17 AC 270 ms
18,396 KB
testcase_18 AC 94 ms
9,108 KB
testcase_19 AC 285 ms
20,816 KB
testcase_20 AC 130 ms
12,640 KB
testcase_21 AC 166 ms
13,456 KB
testcase_22 AC 148 ms
13,188 KB
testcase_23 AC 2 ms
6,676 KB
testcase_24 AC 2 ms
6,676 KB
testcase_25 AC 2 ms
6,676 KB
testcase_26 AC 2 ms
6,676 KB
testcase_27 AC 2 ms
6,676 KB
testcase_28 AC 341 ms
22,152 KB
testcase_29 AC 318 ms
22,024 KB
testcase_30 AC 331 ms
22,024 KB
testcase_31 AC 334 ms
22,152 KB
testcase_32 AC 316 ms
22,020 KB
testcase_33 AC 262 ms
59,340 KB
testcase_34 AC 149 ms
22,620 KB
testcase_35 AC 270 ms
22,152 KB
testcase_36 AC 180 ms
59,340 KB
testcase_37 AC 318 ms
22,152 KB
testcase_38 AC 229 ms
59,340 KB
testcase_39 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  n++;
  VI a(n);
  rep(i, n) cin >> a[i];
  VVI to(n);
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    to[u].emplace_back(v);
    to[v].emplace_back(u);
  }
  vector<int> ord(n);
  iota(ord.begin(), ord.end(), 0);
  sort(ord.begin(), ord.end(), [&](int i, int j) {
    return a[i] < a[j];
  });
  VI pos(n);
  rep(i, n) pos[ord[i]] = i;
  segtree<int, [](int x, int y) { return x + y; }, []() { return 0; }> seg(n);
  fenwick_tree<ll> ft(n);
  VL ans(n);
  auto dfs = [&](auto&& self, int u, int p, int d) -> void {
    seg.set(pos[u], 1);
    ft.add(pos[u], a[u]);
    if (d >= 2) {
      int l0 = seg.max_right(0, [&](int x) { return x <= 0; });
      int l1 = seg.max_right(0, [&](int x) { return x <= 1; });
      int r0 = seg.min_left(n, [&](int x) { return x <= 0; });
      int r1 = seg.min_left(n, [&](int x) { return x <= 1; });
      if (a[ord[l0]] == a[ord[r0 - 1]]) {
        ans[u] = 1;
      } else {
        ans[u] = 4e18;
        for (auto [l, r] : {pair(l0, r1), pair(l1, r0)}) {
          int cnt = d + 1 - 1;
          int c = seg.max_right(l, [&](int x) { return x <= cnt / 2; });
          chmin(ans[u], ft.sum(c, r) - ft.sum(l, c) + (- ((cnt + 1) / 2) + cnt / 2) * a[ord[c]]);
        }
      }
    } else {
      ans[u] = -1;
    }
    for (int v : to[u]) if (v != p) self(self, v, u, d + 1);
    ft.add(pos[u], -a[u]);
    seg.set(pos[u], 0);
  };
  dfs(dfs, 0, -1, 0);
  rep(i, n) if (i) cout << ans[i] << '\n';
}
0