// #pragma GCC target("avx2") // #pragma GCC optimize("O3") // #pragma GCC optimize("unroll-loops") #define CONSTANTS #define _USE_MATH_DEFINES #include using namespace std; constexpr long long INF = 1e18; // constexpr long long INF = LONG_LONG_MAX; constexpr int MOD = 1000000007; // constexpr int MOD = 998244353; constexpr long double EPS = 1e-10; constexpr long double PI = M_PI; using ll = long long; using ull = unsigned long long; using ld = long double; using pll = pair; using pii = pair; using pil = pair; using vvl = vector>; using vvi = vector>; using vvpll = vector>; using vvpil = vector>; #define name4(i, a, b, c, d, e, ...) e #define rep(...) name4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rep1(i, a) for (ll i = 0, _aa = a; i < _aa; i++) #define rep2(i, a, b) for (ll i = a, _bb = b; i < _bb; i++) #define rep3(i, a, b, c) for (ll i = a, _bb = b; (c > 0 && a <= i && i < _bb) or (c < 0 && a >= i && i > _bb); i += c) #define rrep(i, a, b) for (ll i=(a); i>(b); i--) #define pb push_back #define eb emplace_back #define mkp make_pair #define ALL(A) A.begin(), A.end() #define UNIQUE(A) sort(ALL(A)), A.erase(unique(ALL(A)), A.end()) #define elif else if #define tostr to_string #ifndef CONSTANTS constexpr ll INF = 1e18; constexpr int MOD = 1000000007; constexpr ld EPS = 1e-10; constexpr ld PI = M_PI; #endif template struct ModInt { int x; ModInt() : x(0) {} ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator++() { x++; if (x == mod) x = 0; return *this; } ModInt &operator--() { if (x == 0) x = mod; x--; return *this; } ModInt &operator+=(const ModInt &p) { if((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p) { if((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x = (int) (1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator++(int) { ModInt result = *this; ++*this; return result; } ModInt operator--(int) { ModInt result = *this; --*this; return result; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while(b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModInt(u); } ModInt pow(int64_t n) const { ModInt ret(1), mul(x); while(n > 0) { if(n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; } friend istream &operator>>(istream &is, ModInt &a) { int64_t t; is >> t; a = ModInt< mod >(t); return (is); } static int get_mod() { return mod; } }; using mint = ModInt; template vector> list2d(int N, int M, T init) { return vector>(N, vector(M, init)); } template vector>> list3d(int N, int M, int L, T init) { return vector>>(N, vector>(M, vector(L, init))); } template vector>>> list4d(int N, int M, int L, int O, T init) { return vector>>>(N, vector>>(M, vector>(L, vector(O, init)))); } template vector LIST(ll N) { vector A(N); rep(i, N) { cin >> A[i]; } return A; } void print() { cout << '\n'; } template void print(T out) { cout << out << '\n'; } template void print(const pair &p) { cout << p.first << ' ' << p.second << '\n'; } template void print(const tuple &tp) { cout << get<0>(tp) << ' ' << get<1>(tp) << ' ' << get<2>(tp) << '\n'; } template void print(const tuple &tp) { cout << get<0>(tp) << ' ' << get<1>(tp) << ' ' << get<2>(tp) << ' ' << get<3>(tp) << '\n'; } template void print(const vector> &V) { for (auto& p : V) print(p); } template void print(const vector &V) { rep(i, V.size()) { cout << V[i]; if (i != V.size()-1) cout << ' '; } cout << '\n'; } template void print(const array &arr) { rep(i, arr.size()) { cout << arr[i]; if (i != arr.size()-1) cout << ' '; } cout << '\n'; } template void print(const vector> &V) { for (auto& arr : V) print(arr); } template void print(const deque &que) { vector V(ALL(que)); print(V); } template void print(const set &se) { vector V(ALL(se)); print(V); } #define debug(x) (cout << #x << ": ", print(x)); void Yes() { print("Yes"); } void No() { print("No"); } void YES() { print("YES"); } void NO() { print("NO"); } ll toint(string s) { assert(s.size() < 20); ll res = 0; for (char &c : s) { res *= 10; res += c - '0'; } return res; } int toint(char num) { return num - '0'; } char tochar(int num) { return '0' + num; } template T ceil(T a, T b) { if (a >= 0) return (a+b-1) / b; else return a / b; } template T modulo(T a, T b) { return ((a % b) + b) % b; } template pair divmod(T a, T b) { T d = a / b; T m = a % b; return {d, m}; } template bool chmin(T &x, T y) { return (y < x) ? x = y, true : false; } template bool chmax(T &x, T y) { return (y > x) ? x = y, true : false; } template T sum(const vector &A) { return accumulate(ALL(A), (T)0); } template val sum(const map &mp) { val res = 0; for (auto& [k, v] : mp) res += v; return res; } template T min(const vector &A) { return *min_element(ALL(A)); } template val min(const map &mp) { val res = numeric_limits::max(); for (auto& [k, v] : mp) chmin(res, v); return res; } template T max(const vector &A) { return *max_element(ALL(A)); } template val max(const map &mp) { val res = numeric_limits::min(); for (auto& [k, v] : mp) chmax(res, v); return res; } ll pow(ll x, ll n) { ll res = 1; rep(_, n) res *= x; return res; } ll pow(int x, ll n) { return pow((ll)x, n); } ll pow(ll x, int n) { return pow(x, (ll)n); } ll pow(int x, int n) { return pow((ll)x, (ll)n); } ll pow(ll x, ll n, int mod) { x %= mod; ll res = 1; while (n > 0) { if (n & 1) { res = (res * x) % mod; } x = (x * x) % mod; n >>= 1; } return res; } int popcount(ll S) { return __builtin_popcountll(S); } int bit_length(ll x) { return x != 0 ? floor(log2((ld)x))+1 : 0; } template int bisect_left(const vector &A, T val, int lo=0) { return lower_bound(A.begin()+lo, A.end(), val) - A.begin(); } template int bisect_right(const vector &A, T val, int lo=0) { return upper_bound(A.begin()+lo, A.end(), val) - A.begin(); } template map Counter(const vector &A) { map res; for (T a : A) { res[a]++; } return res; } template vector Counter(const vector &A, T mx) { vector res(mx+1); for (T a : A) { res[a]++; } return res; } map Counter(const string &S) { map res; for (char c : S) { res[c]++; } return res; } template ll bisearch_min(ll mn, ll mx, const F &func) { ll ok = mx; ll ng = mn; while (ng+1 < ok) { ll mid = (ok+ng) / 2; if (func(mid)) { ok = mid; } else { ng = mid; } } return ok; } template ll bisearch_max(ll mn, ll mx, const F &func) { ll ok = mn; ll ng = mx; while (ok+1 < ng) { ll mid = (ok+ng) / 2; if (func(mid)) { ok = mid; } else { ng = mid; } } return ok; } template pair, vector> zip(const vector> &A) { int N = A.size(); pair, vector> res = {vector(N), vector(N)}; rep(i, N) { res.first[i] = A[i].first; res.second[i] = A[i].second; } return res; } template tuple, vector, vector> zip(const vector> &A) { int N = A.size(); tuple, vector, vector> res = {vector(N), vector(N), vector(N)}; rep(i, N) { get<0>(res)[i] = get<0>(A[i]); get<1>(res)[i] = get<1>(A[i]); get<2>(res)[i] = get<2>(A[i]); } return res; } template struct Compress { int N; vector dat; Compress(vector A) { sort(A.begin(), A.end()); A.erase(unique(A.begin(), A.end()), A.end()); N = A.size(); dat = A; } int zip(T x) { return bisect_left(dat, x); } T unzip(int x) { return dat[x]; } int operator[](T x) { return zip(x); } int size() { return dat.size(); } vector zip(const vector &A) { int M = A.size(); vector res(M); rep(i, M) res[i] = zip(A[i]); return res; } }; template vector> RLE(const vector &A) { if (A.empty()) return {}; int N = A.size(); vector> res; T cur = A[0]; int cnt = 1; rep(i, 1, N) { if (A[i] == A[i-1]) { cnt++; } else { res.pb({cur, cnt}); cnt = 1; cur = A[i]; } } res.pb({cur, cnt}); return res; } vector> RLE(const string &S) { if (S.empty()) return {}; int N = S.size(); vector> res; char cur = S[0]; int cnt = 1; rep(i, 1, N) { if (S[i] == S[i-1]) { cnt++; } else { res.pb({cur, cnt}); cnt = 1; cur = S[i]; } } res.pb({cur, cnt}); return res; } template bool mul_overflow(T x, T y) { T z; return __builtin_mul_overflow(x, y, &z); } vector split(const string &S, char separator) { int N = S.size(); vector res; string cur; rep(i, N) { if (S[i] == separator) { res.eb(toint(cur)); cur = ""; } else { cur += S[i]; } } if (cur.size()) res.eb(toint(cur)); return res; } string to_string(const string &S) { return S; } string to_string(char c) { return {c}; } template string join(const vector &A, char separator=0) { int N = A.size(); string res; rep(i, N) { res += tostr(A[i]); if (separator != 0 and i != N-1) res += separator; } return res; } template vector sorted(vector A, bool reverse=false) { sort(ALL(A)); if (reverse) std::reverse(ALL(A)); return A; } template vector reversed(vector A) { reverse(ALL(A)); return A; } template struct ModTools { int MAX; vector fact, factinv; ModTools() {}; ModTools(int mx) { build(mx); } void build(int mx) { MAX = ++mx; fact.resize(MAX); factinv.resize(MAX); fact[0] = fact[1] = 1; rep(i, 2, MAX) { fact[i] = fact[i-1] * i; } factinv[MAX-1] = (Mint)1 / fact[MAX-1]; rep(i, MAX-2, -1, -1) { factinv[i] = factinv[i+1] * (i+1); } } Mint factorial(int x) { return fact[x]; } Mint inverse(int x) { return factinv[x]; } Mint nCr(int n, int r) { if (n < r or r < 0) return 0; r = min(r, n-r); Mint num = fact[n]; Mint den = factinv[r] * factinv[n-r]; return num * den; } Mint nHr(int n, int r) { return nCr(r+n-1, r); } Mint nPr(int n, int r) { if (n < r or r < 0) return 0; return fact[n] * factinv[n-r]; } }; template vector> permutations(const vector &A, int N=-1) { if (N == -1) N = A.size(); int M = A.size(); assert(N <= M); vector> comb; rep(bit, 1< res; rep(i, M) { if (bit>>i & 1) { res.pb(A[i]); } } comb.pb(res); } vector> res; for (auto &perm : comb) { sort(ALL(perm)); do { res.pb(perm); } while (next_permutation(ALL(perm))); } return res; } template vector> combinations(const vector &A, int N) { int M = A.size(); vector> res; auto rec = [&](auto&& f, vector &cur, ll x, ll n) -> void { if (n == N) { res.pb(cur); return; } rep(i, x, M) { cur.pb(A[i]); f(f, cur, i+1, n+1); cur.pop_back(); } }; vector cur; rec(rec, cur, 0, 0); return res; } ll nC2(ll n) { if (n < 2) return 0; return n*(n-1)/2; } template T factorial(int x) { T res = 1; for (int i=1; i<=x; i++) res *= i; return res; } struct UnionFind { int n, groupcnt; vector par, rank, sz; vector tree; UnionFind(int n) : n(n) { par.assign(n, 0); rank.assign(n, 0); sz.assign(n, 1); tree.assign(n, true); rep(i, n) par[i] = i; groupcnt = n; } UnionFind() {} void resize(int _n) { n = _n; par.assign(n, 0); rank.assign(n, 0); sz.assign(n, 1); tree.assign(n, true); rep(i, n) par[i] = i; groupcnt = n; } int find(int x) { if (par[x] == x) { return x; } else { par[x] = find(par[x]); return par[x]; } } int merge(int a, int b) { int x = find(a); int y = find(b); if (x == y) { tree[x] = false; return x; } if (!tree[x] or !tree[y]) { tree[x] = tree[y] = false; } groupcnt--; if (rank[x] < rank[y]) { par[x] = y; sz[y] += sz[x]; return y; } else { par[y] = x; sz[x] += sz[y]; if (rank[x] == rank[y]) { rank[x]++; } return x; } } bool same(int a, int b) { return find(a) == find(b); } ll size(int x) { return sz[find(x)]; } int size() { return groupcnt; } bool is_tree(int x) { return tree[find(x)]; } set get_roots() { set res; rep(i, n) { res.insert(find(i)); } return res; } }; const vector dir4 = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; #define directions dir4 ll gridtoid(ll i, ll j, ll W) { return i*W+j; } pll idtogrid(ll id, ll W) { return divmod(id, W); } template struct defaultdict : map<_Key, _Tp> { const _Tp init; defaultdict() : init(_Tp()) {}; defaultdict(_Tp init) : init(init) {} _Tp& operator[](const _Key& k) { if (this->count(k)) { return map<_Key, _Tp>::operator[](k); } else { return map<_Key, _Tp>::operator[](k) = init; } } _Tp& operator[](_Key&& k) { if (this->count(k)) { return map<_Key, _Tp>::operator[](k); } else { return map<_Key, _Tp>::operator[](k) = init; } } }; ll gcd(ll a, ll b) { return __gcd(a, b); } template T gcd(const vector &A) { T res = 0; for (auto a : A) res = gcd(res, a); return res; } ll lcm(ll x, ll y) { return x/gcd(x, y)*y; } template T lcm(const vector &A) { T res = 1; for (auto a : A) res = lcm(res, a); return res; } template vector> factorize(T n) { vector> ret; for (T i=2; i*i<=n; i++) { int cnt = 0; while (n % i == 0) { n /= i; cnt++; } if (cnt) ret.emplace_back(i, cnt); } if (n > 1) ret.emplace_back(n, 1); return ret; } template vector divisors(T n) { vector res; for (T i=1; i*i<=n; i++) { if (n%i == 0) { res.eb(i); if (n/i != i) res.eb(n/i); } } return res; } ll isqrt(ll n, bool ceil=false) { ll ok = 0; ll ng = 3037000500; while (ng - ok > 1) { ll m = ok + (ng - ok) / 2; if (m * m <= n) { ok = m; } else { ng = m; } } if (ceil and ok*ok != n) ok++; return ok; } template struct Accumulate { vector acc; int N; Accumulate() {} Accumulate(int N) : N(N) { acc.resize(N); } Accumulate(const vector &A) { N = A.size(); acc = A; build(); } void set(int i, T a) { acc[i] = a; } void build() { rep(i, N-1) { acc[i+1] += acc[i]; } acc.insert(acc.begin(), 0); } T query(int l, int r) { assert(0 <= l and l <= N and 0 <= r and r <= N); return acc[r]-acc[l]; } T get(int i) { return query(i, i+1); } T operator[](int i){ return query(i, i+1); } ll bisearch_fore(int l, int r, ll x) { if (l > r) return -1; ll l_sm = query(0, l); int ok = r + 1; int ng = l - 1; while (ng+1 < ok) { int mid = (ok+ng) / 2; if (query(0, mid+1) - l_sm >= x) { ok = mid; } else { ng = mid; } } if (ok != r+1) { return ok; } else { return -1; } } ll bisearch_back(int l, int r, ll x) { if (l > r) return -1; ll r_sm = query(0, r+1); int ok = l - 1; int ng = r + 1; while (ok+1 < ng) { int mid = (ok+ng) / 2; if (r_sm - query(0, mid) >= x) { ok = mid; } else { ng = mid; } } if (ok != l-1) { return ok; } else { return -1; } } }; template struct BIT { int sz; vector tree; BIT(int n) { n++; sz = 1; while (sz < n) { sz *= 2; } tree.resize(sz); } T sum(int i) { T s = 0; i++; while (i > 0) { s += tree[i-1]; i -= i & -i; } return s; } void add(int i, T x) { i++; while (i <= sz) { tree[i-1] += x; i += i & -i; } } T query(int l, int r) { return sum(r-1) - sum(l-1); } T get(int i) { return query(i, i+1); } void update(int i, T x) { add(i, x - get(i)); } T operator[](int i) { return query(i, i+1); } void print(int n) { rep(i, n) { cout << query(i, i+1); if (i == n-1) cout << endl; else cout << ' '; } } ll bisearch_fore(int l, int r, ll x) { if (l > r) return -1; ll l_sm = sum(l-1); int ok = r + 1; int ng = l - 1; while (ng+1 < ok) { int mid = (ok+ng) / 2; if (sum(mid) - l_sm >= x) { ok = mid; } else { ng = mid; } } if (ok != r+1) { return ok; } else { return -1; } } ll bisearch_back(int l, int r, ll x) { if (l > r) return -1; ll r_sm = sum(r); int ok = l - 1; int ng = r + 1; while (ok+1 < ng) { int mid = (ok+ng) / 2; if (r_sm - sum(mid-1) >= x) { ok = mid; } else { ng = mid; } } if (ok != l-1) { return ok; } else { return -1; } } }; template struct SegmentTree { int sz; vector seg; const F f; const Monoid M1; SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) { sz = 1; while(sz < n) sz <<= 1; seg.assign(2 * sz, M1); } SegmentTree(const F f, const Monoid &M1) : f(f), M1(M1) {} void resize(int n) { sz = 1; while(sz < n) sz <<= 1; seg.resize(2 * sz, M1); } void clear() { seg.clear(); } void set(int k, const Monoid &x) { seg[k+sz] = x; } void build() { for(int k = sz - 1; k > 0; k--) { seg[k] = f(seg[2*k], seg[2*k+1]); } } void build(const vector &A) { int n = A.size(); resize(n); rep(i, n) set(i, A[i]); build(); } void update(int k, const Monoid &x) { k += sz; seg[k] = x; while(k >>= 1) { seg[k] = f(seg[2*k], seg[2*k+1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if(a & 1) L = f(L, seg[a++]); if(b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k+sz]; } Monoid all() { return seg[1]; } void print(int n) { for (int i=0; i int find_subtree(int a, const C &check, Monoid &M, bool type) { while(a < sz) { Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]); if(check(nxt)) a = 2 * a + type; else M = nxt, a = 2 * a + 1 - type; } return a - sz; } template int find_first(int a, const C &check) { Monoid L = M1; if(a <= 0) { if(check(f(L, seg[1]))) return find_subtree(1, check, L, false); return -1; } int b = sz; for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if(a & 1) { Monoid nxt = f(L, seg[a]); if(check(nxt)) return find_subtree(a, check, L, false); L = nxt; ++a; } } return -1; } template int find_last(int b, const C &check) { Monoid R = M1; if(b >= sz) { if(check(f(seg[1], R))) return find_subtree(1, check, R, true); return -1; } int a = sz; for(b += sz; a < b; a >>= 1, b >>= 1) { if(b & 1) { Monoid nxt = f(seg[--b], R); if(check(nxt)) return find_subtree(b, check, R, true); R = nxt; } } return -1; } }; template SegmentTree get_segment_tree(int N, const F& f, const Monoid& M1) { return {N, f, M1}; } template SegmentTree get_segment_tree(const F& f, const Monoid& M1) { return {f, M1}; } const string digits = "0123456789"; const string ascii_lowercase = "abcdefghijklmnopqrstuvwxyz"; const string ascii_uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; string zfill(string str, int len) { string zeros; int n = str.size(); rep(i, len-n) zeros += '0'; return zeros+str; } string bin(ll x) { string res; while (x) { if (x & 1) { res += '1'; } else { res += '0'; } x >>= 1; } reverse(ALL(res)); if (res == "") res += '0'; return res; } struct HeavyLightDecomposition { public: vvi g; vector< int > sz, in, out, head, rev, par, dep; void build() { sz.assign(g.size(), 0); in.assign(g.size(), 0); out.assign(g.size(), 0); head.assign(g.size(), 0); rev.assign(g.size(), 0); par.assign(g.size(), 0); dep.assign(g.size(), 0); dfs_sz(0, -1, 0); int t = 0; dfs_hld(0, -1, t); } int la(int v, int k) { while(1) { int u = head[v]; if(in[v] - k >= in[u]) return rev[in[v] - k]; k -= in[v] - in[u] + 1; v = par[u]; } } int lca(int u, int v) const { for(;; v = par[head[v]]) { if(in[u] > in[v]) swap(u, v); if(head[u] == head[v]) return u; } } int dist(int u, int v) const { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; } template< typename E, typename Q, typename F, typename S > E query(int u, int v, const E &ti, const Q &q, const F &f, const S &s, bool edge = false) { E l = ti, r = ti; for(;; v = par[head[v]]) { if(in[u] > in[v]) swap(u, v), swap(l, r); if(head[u] == head[v]) break; l = f(q(in[head[v]], in[v] + 1), l); } return s(f(q(in[u] + edge, in[v] + 1), l), r); } template< typename E, typename Q, typename F > E query(int u, int v, const E &ti, const Q &q, const F &f, bool edge = false) { return query(u, v, ti, q, f, f, edge); } template< typename Q > void add(int u, int v, const Q &q, bool edge = false) { for(;; v = par[head[v]]) { if(in[u] > in[v]) swap(u, v); if(head[u] == head[v]) break; q(in[head[v]], in[v] + 1); } q(in[u] + edge, in[v] + 1); } /* {parent, child} */ vector< pair< int, int > > compress(vector< int > &remark) { auto cmp = [&](int a, int b) { return in[a] < in[b]; }; sort(begin(remark), end(remark), cmp); remark.erase(unique(begin(remark), end(remark)), end(remark)); int K = (int) remark.size(); for(int k = 1; k < K; k++) remark.emplace_back(lca(remark[k - 1], remark[k])); sort(begin(remark), end(remark), cmp); remark.erase(unique(begin(remark), end(remark)), end(remark)); vector< pair< int, int > > es; stack< int > st; for(auto &k : remark) { while(!st.empty() && out[st.top()] <= in[k]) st.pop(); if(!st.empty()) es.emplace_back(st.top(), k); st.emplace(k); } return es; } explicit HeavyLightDecomposition(vvi &g) : g(g) {} private: void dfs_sz(int idx, int p, int d) { dep[idx] = d; par[idx] = p; sz[idx] = 1; if(g[idx].size() && g[idx][0] == p) swap(g[idx][0], g[idx].back()); for(auto &to : g[idx]) { if(to == p) continue; dfs_sz(to, idx, d + 1); sz[idx] += sz[to]; if(sz[g[idx][0]] < sz[to]) swap(g[idx][0], to); } } void dfs_hld(int idx, int p, int ×) { in[idx] = times++; rev[in[idx]] = idx; for(auto &to : g[idx]) { if(to == p) continue; head[to] = (g[idx][0] == to ? head[idx] : to); dfs_hld(to, idx, times); } out[idx] = times; } }; template struct SparseTable { const F f; const E e; vector> dat; vector height; SparseTable(const F f) : f(f) {} SparseTable(const vector &A, const F f, const E e) : f(f), e(e) { build(A); } void build(const vector &A) { int N = A.size(); int h = 0; while ((1<>1] + 1; } rep(i, N) { dat[0][i] = A[i]; } rep(i, 1, h) { for (int j=0; j= r) return e; int a = height[r-l]; return f(dat[a][l], dat[a][r-(1< int bisearch_fore(int l, int r, T x, const G &compare) { int ok = r + 1; int ng = l - 1; while (ng+1 < ok) { int mid = (ok+ng) / 2; if (compare(query(l, mid+1), x)) { ok = mid; } else { ng = mid; } } if (ok != r + 1) { return ok; } else { return -1; } } template int bisearch_back(int l, int r, T x, const G &compare) { int ok = l - 1; int ng = r + 1; while (ok+1 < ng) { int mid = (ok+ng) / 2; if (compare(query(mid, r+1), x)) { ok = mid; } else { ng = mid; } } if (ok != l - 1) { return ok; } else { return -1; } } }; template SparseTable get_sparse_table(vector &A, const F& f, const E& e) { return {A, f, e}; } void solve() { ll N; cin >> N; vector> edges; vvi nodes(N); ll mxi = -1, mx = 0; rep(i, N-1) { ll u, v, c; cin >> u >> v >> c; u--; v--; nodes[u].eb(v); nodes[v].eb(u); edges.pb({u, v, c}); if (chmax(mx, c)) { mxi = i; } } // 最大の辺をつなぐ頂点を根にしておく ll root = get<0>(edges[mxi]); swap(nodes[0], nodes[root]); rep(u, N) { for (int& v : nodes[u]) { if (v == 0) { v = root; } else if (v == root) { v = 0; } } } vvpll nodes2(N); for (auto& [u, v, c] : edges) { if (u == 0) { u = root; } else if (u == root) { u = 0; } if (v == 0) { v = root; } else if (v == root) { v = 0; } nodes2[u].eb(v, c); nodes2[v].eb(u, c); } HeavyLightDecomposition hld(nodes); hld.build(); vector tmp(N); for (auto [u, v, c] : edges) { if (hld.dep[u] < hld.dep[v]) { tmp[hld.in[v]] = c; } else { tmp[hld.in[u]] = c; } } auto st = get_sparse_table(tmp, [](ll a, ll b) -> ll { return max(a, b); }, -INF); ll res = bisearch_min(-1, mx, [&](ll d) { auto [v1, v2, _] = edges[mxi]; auto dfs = [&](auto&& f, ll u, ll prv) -> bool { ll subcnt = 0; for (auto [v, c] : nodes2[u]) { if (v == prv) continue; // 部分木のmaxがd以下ならこの先は取り込む必要なし if (hld.dep[u] < hld.dep[v]) { if (max(st.query(hld.in[v]+1, hld.out[v]), c) <= d) continue; } else { assert(0); } // 含む辺のminが小さくてmxを減らし切れない if (mx-c > d) { return false; } subcnt++; if (!f(f, v, u)) { return false; } } // 取り込んだ部分木が2つ以上に枝分かれしていたら無理 return subcnt < 2; }; return dfs(dfs, v1, v2) and dfs(dfs, v2, v1); }); print(res); } int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); // single test case solve(); // multi test cases // int T; // cin >> T; // while (T--) solve(); return 0; }