QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#649615#3. Fireworksmaspy55 980ms37484kbC++2347.9kb2024-10-18 03:33:142024-10-18 03:33:15

Judging History

你现在查看的是最新测评结果

  • [2024-10-18 03:33:15]
  • 评测
  • 测评结果:55
  • 用时:980ms
  • 内存:37484kb
  • [2024-10-18 03:33:14]
  • 提交

answer

#line 1 "/home/maspy/compro/library/my_template.hpp"
#if defined(LOCAL)
#include <my_template_compiled.hpp>
#else

// https://codeforces.com/blog/entry/96344
#pragma GCC optimize("Ofast,unroll-loops")
// いまの CF だとこれ入れると動かない?
// #pragma GCC target("avx2,popcnt")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;

template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'010'000'000;
template <>
constexpr ll infty<ll> = 2'020'000'000'000'000'000;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * 2'000'000'000'000'000'000;
template <>
constexpr double infty<double> = infty<ll>;
template <>
constexpr long double infty<long double> = infty<ll>;

using pi = pair<ll, ll>;
using vi = vector<ll>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;

#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...) \
  vector<vector<vector<vector<type>>>> name(a, vector<vector<vector<type>>>(b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))

// https://trap.jp/post/1224/
#define FOR1(a) for (ll _ = 0; _ < ll(a); ++_)
#define FOR2(i, a) for (ll i = 0; i < ll(a); ++i)
#define FOR3(i, a, b) for (ll i = a; i < ll(b); ++i)
#define FOR4(i, a, b, c) for (ll i = a; i < ll(b); i += (c))
#define FOR1_R(a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR2_R(i, a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR3_R(i, a, b) for (ll i = (b)-1; i >= ll(a); --i)
#define overload4(a, b, c, d, e, ...) e
#define overload3(a, b, c, d, ...) d
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FOR_R(...) overload3(__VA_ARGS__, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__)

#define FOR_subset(t, s) for (ll t = (s); t >= 0; t = (t == 0 ? -1 : (t - 1) & (s)))
#define all(x) x.begin(), x.end()
#define len(x) ll(x.size())
#define elif else if

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second

#define stoi stoll

int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }
int popcnt_mod_2(int x) { return __builtin_parity(x); }
int popcnt_mod_2(u32 x) { return __builtin_parity(x); }
int popcnt_mod_2(ll x) { return __builtin_parityll(x); }
int popcnt_mod_2(u64 x) { return __builtin_parityll(x); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)
int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }

template <typename T>
T floor(T a, T b) {
  return a / b - (a % b && (a ^ b) < 0);
}
template <typename T>
T ceil(T x, T y) {
  return floor(x + y - 1, y);
}
template <typename T>
T bmod(T x, T y) {
  return x - y * floor(x, y);
}
template <typename T>
pair<T, T> divmod(T x, T y) {
  T q = floor(x, y);
  return {q, x - q * y};
}

template <typename T, typename U>
T SUM(const vector<U> &A) {
  T sm = 0;
  for (auto &&a: A) sm += a;
  return sm;
}

#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()

template <typename T>
T POP(deque<T> &que) {
  T a = que.front();
  que.pop_front();
  return a;
}
template <typename T>
T POP(pq<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(pqg<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(vc<T> &que) {
  T a = que.back();
  que.pop_back();
  return a;
}

template <typename F>
ll binary_search(F check, ll ok, ll ng, bool check_ok = true) {
  if (check_ok) assert(check(ok));
  while (abs(ok - ng) > 1) {
    auto x = (ng + ok) / 2;
    (check(x) ? ok : ng) = x;
  }
  return ok;
}
template <typename F>
double binary_search_real(F check, double ok, double ng, int iter = 100) {
  FOR(iter) {
    double x = (ok + ng) / 2;
    (check(x) ? ok : ng) = x;
  }
  return (ok + ng) / 2;
}

template <class T, class S>
inline bool chmax(T &a, const S &b) {
  return (a < b ? a = b, 1 : 0);
}
template <class T, class S>
inline bool chmin(T &a, const S &b) {
  return (a > b ? a = b, 1 : 0);
}

// ? は -1
vc<int> s_to_vi(const string &S, char first_char) {
  vc<int> A(S.size());
  FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }
  return A;
}

template <typename T, typename U>
vector<T> cumsum(vector<U> &A, int off = 1) {
  int N = A.size();
  vector<T> B(N + 1);
  FOR(i, N) { B[i + 1] = B[i] + A[i]; }
  if (off == 0) B.erase(B.begin());
  return B;
}

// stable sort
template <typename T>
vector<int> argsort(const vector<T> &A) {
  vector<int> ids(len(A));
  iota(all(ids), 0);
  sort(all(ids), [&](int i, int j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
  return ids;
}

// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vc<int> &I) {
  vc<T> B(len(I));
  FOR(i, len(I)) B[i] = A[I[i]];
  return B;
}

template <typename T, typename... Vectors>
void concat(vc<T> &first, const Vectors &... others) {
  vc<T> &res = first;
  (res.insert(res.end(), others.begin(), others.end()), ...);
}
#endif
#line 1 "/home/maspy/compro/library/other/io.hpp"
#define FASTIO
#include <unistd.h>

// https://judge.yosupo.jp/submission/21623
namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf
uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
  char num[10000][4];
  constexpr Pre() : num() {
    for (int i = 0; i < 10000; i++) {
      int n = i;
      for (int j = 3; j >= 0; j--) {
        num[i][j] = n % 10 | '0';
        n /= 10;
      }
    }
  }
} constexpr pre;

inline void load() {
  memcpy(ibuf, ibuf + pil, pir - pil);
  pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
  pil = 0;
  if (pir < SZ) ibuf[pir++] = '\n';
}

inline void flush() {
  fwrite(obuf, 1, por, stdout);
  por = 0;
}

void rd(char &c) {
  do {
    if (pil + 1 > pir) load();
    c = ibuf[pil++];
  } while (isspace(c));
}

void rd(string &x) {
  x.clear();
  char c;
  do {
    if (pil + 1 > pir) load();
    c = ibuf[pil++];
  } while (isspace(c));
  do {
    x += c;
    if (pil == pir) load();
    c = ibuf[pil++];
  } while (!isspace(c));
}

template <typename T>
void rd_real(T &x) {
  string s;
  rd(s);
  x = stod(s);
}

template <typename T>
void rd_integer(T &x) {
  if (pil + 100 > pir) load();
  char c;
  do
    c = ibuf[pil++];
  while (c < '-');
  bool minus = 0;
  if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
    if (c == '-') { minus = 1, c = ibuf[pil++]; }
  }
  x = 0;
  while ('0' <= c) { x = x * 10 + (c & 15), c = ibuf[pil++]; }
  if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
    if (minus) x = -x;
  }
}

void rd(int &x) { rd_integer(x); }
void rd(ll &x) { rd_integer(x); }
void rd(i128 &x) { rd_integer(x); }
void rd(u32 &x) { rd_integer(x); }
void rd(u64 &x) { rd_integer(x); }
void rd(u128 &x) { rd_integer(x); }
void rd(double &x) { rd_real(x); }
void rd(long double &x) { rd_real(x); }
void rd(f128 &x) { rd_real(x); }

template <class T, class U>
void rd(pair<T, U> &p) {
  return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T>
void rd_tuple(T &t) {
  if constexpr (N < std::tuple_size<T>::value) {
    auto &x = std::get<N>(t);
    rd(x);
    rd_tuple<N + 1>(t);
  }
}
template <class... T>
void rd(tuple<T...> &tpl) {
  rd_tuple(tpl);
}

template <size_t N = 0, typename T>
void rd(array<T, N> &x) {
  for (auto &d: x) rd(d);
}
template <class T>
void rd(vc<T> &x) {
  for (auto &d: x) rd(d);
}

void read() {}
template <class H, class... T>
void read(H &h, T &... t) {
  rd(h), read(t...);
}

void wt(const char c) {
  if (por == SZ) flush();
  obuf[por++] = c;
}
void wt(const string s) {
  for (char c: s) wt(c);
}
void wt(const char *s) {
  size_t len = strlen(s);
  for (size_t i = 0; i < len; i++) wt(s[i]);
}

template <typename T>
void wt_integer(T x) {
  if (por > SZ - 100) flush();
  if (x < 0) { obuf[por++] = '-', x = -x; }
  int outi;
  for (outi = 96; x >= 10000; outi -= 4) {
    memcpy(out + outi, pre.num[x % 10000], 4);
    x /= 10000;
  }
  if (x >= 1000) {
    memcpy(obuf + por, pre.num[x], 4);
    por += 4;
  } else if (x >= 100) {
    memcpy(obuf + por, pre.num[x] + 1, 3);
    por += 3;
  } else if (x >= 10) {
    int q = (x * 103) >> 10;
    obuf[por] = q | '0';
    obuf[por + 1] = (x - q * 10) | '0';
    por += 2;
  } else
    obuf[por++] = x | '0';
  memcpy(obuf + por, out + outi + 4, 96 - outi);
  por += 96 - outi;
}

template <typename T>
void wt_real(T x) {
  ostringstream oss;
  oss << fixed << setprecision(15) << double(x);
  string s = oss.str();
  wt(s);
}

void wt(int x) { wt_integer(x); }
void wt(ll x) { wt_integer(x); }
void wt(i128 x) { wt_integer(x); }
void wt(u32 x) { wt_integer(x); }
void wt(u64 x) { wt_integer(x); }
void wt(u128 x) { wt_integer(x); }
void wt(double x) { wt_real(x); }
void wt(long double x) { wt_real(x); }
void wt(f128 x) { wt_real(x); }

template <class T, class U>
void wt(const pair<T, U> val) {
  wt(val.first);
  wt(' ');
  wt(val.second);
}
template <size_t N = 0, typename T>
void wt_tuple(const T t) {
  if constexpr (N < std::tuple_size<T>::value) {
    if constexpr (N > 0) { wt(' '); }
    const auto x = std::get<N>(t);
    wt(x);
    wt_tuple<N + 1>(t);
  }
}
template <class... T>
void wt(tuple<T...> tpl) {
  wt_tuple(tpl);
}
template <class T, size_t S>
void wt(const array<T, S> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}
template <class T>
void wt(const vector<T> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}

void print() { wt('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
  wt(head);
  if (sizeof...(Tail)) wt(' ');
  print(forward<Tail>(tail)...);
}

// gcc expansion. called automaticall after main.
void __attribute__((destructor)) _d() { flush(); }
} // namespace fastio
using fastio::read;
using fastio::print;
using fastio::flush;

#if defined(LOCAL)
#define SHOW(...) SHOW_IMPL(__VA_ARGS__, SHOW6, SHOW5, SHOW4, SHOW3, SHOW2, SHOW1)(__VA_ARGS__)
#define SHOW_IMPL(_1, _2, _3, _4, _5, _6, NAME, ...) NAME
#define SHOW1(x) print(#x, "=", (x)), flush()
#define SHOW2(x, y) print(#x, "=", (x), #y, "=", (y)), flush()
#define SHOW3(x, y, z) print(#x, "=", (x), #y, "=", (y), #z, "=", (z)), flush()
#define SHOW4(x, y, z, w) print(#x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w)), flush()
#define SHOW5(x, y, z, w, v) print(#x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", (v)), flush()
#define SHOW6(x, y, z, w, v, u) print(#x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", (v), #u, "=", (u)), flush()
#else
#define SHOW(...)
#endif

#define INT(...)   \
  int __VA_ARGS__; \
  read(__VA_ARGS__)
#define LL(...)   \
  ll __VA_ARGS__; \
  read(__VA_ARGS__)
#define U32(...)   \
  u32 __VA_ARGS__; \
  read(__VA_ARGS__)
#define U64(...)   \
  u64 __VA_ARGS__; \
  read(__VA_ARGS__)
#define STR(...)      \
  string __VA_ARGS__; \
  read(__VA_ARGS__)
#define CHAR(...)   \
  char __VA_ARGS__; \
  read(__VA_ARGS__)
#define DBL(...)      \
  double __VA_ARGS__; \
  read(__VA_ARGS__)

#define VEC(type, name, size) \
  vector<type> name(size);    \
  read(name)
#define VV(type, name, h, w)                     \
  vector<vector<type>> name(h, vector<type>(w)); \
  read(name)

void YES(bool t = 1) { print(t ? "YES" : "NO"); }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void no(bool t = 1) { yes(!t); }
#line 3 "main.cpp"

#line 2 "/home/maspy/compro/library/graph/tree.hpp"

#line 2 "/home/maspy/compro/library/graph/base.hpp"

template <typename T>
struct Edge {
  int frm, to;
  T cost;
  int id;
};

template <typename T = int, bool directed = false>
struct Graph {
  static constexpr bool is_directed = directed;
  int N, M;
  using cost_type = T;
  using edge_type = Edge<T>;
  vector<edge_type> edges;
  vector<int> indptr;
  vector<edge_type> csr_edges;
  vc<int> vc_deg, vc_indeg, vc_outdeg;
  bool prepared;

  class OutgoingEdges {
  public:
    OutgoingEdges(const Graph* G, int l, int r) : G(G), l(l), r(r) {}

    const edge_type* begin() const {
      if (l == r) { return 0; }
      return &G->csr_edges[l];
    }

    const edge_type* end() const {
      if (l == r) { return 0; }
      return &G->csr_edges[r];
    }

  private:
    const Graph* G;
    int l, r;
  };

  bool is_prepared() { return prepared; }

  Graph() : N(0), M(0), prepared(0) {}
  Graph(int N) : N(N), M(0), prepared(0) {}

  void build(int n) {
    N = n, M = 0;
    prepared = 0;
    edges.clear();
    indptr.clear();
    csr_edges.clear();
    vc_deg.clear();
    vc_indeg.clear();
    vc_outdeg.clear();
  }

  void add(int frm, int to, T cost = 1, int i = -1) {
    assert(!prepared);
    assert(0 <= frm && 0 <= to && to < N);
    if (i == -1) i = M;
    auto e = edge_type({frm, to, cost, i});
    edges.eb(e);
    ++M;
  }

#ifdef FASTIO
  // wt, off
  void read_tree(bool wt = false, int off = 1) { read_graph(N - 1, wt, off); }

  void read_graph(int M, bool wt = false, int off = 1) {
    for (int m = 0; m < M; ++m) {
      INT(a, b);
      a -= off, b -= off;
      if (!wt) {
        add(a, b);
      } else {
        T c;
        read(c);
        add(a, b, c);
      }
    }
    build();
  }
#endif

  void build() {
    assert(!prepared);
    prepared = true;
    indptr.assign(N + 1, 0);
    for (auto&& e: edges) {
      indptr[e.frm + 1]++;
      if (!directed) indptr[e.to + 1]++;
    }
    for (int v = 0; v < N; ++v) { indptr[v + 1] += indptr[v]; }
    auto counter = indptr;
    csr_edges.resize(indptr.back() + 1);
    for (auto&& e: edges) {
      csr_edges[counter[e.frm]++] = e;
      if (!directed)
        csr_edges[counter[e.to]++] = edge_type({e.to, e.frm, e.cost, e.id});
    }
  }

  OutgoingEdges operator[](int v) const {
    assert(prepared);
    return {this, indptr[v], indptr[v + 1]};
  }

  vc<int> deg_array() {
    if (vc_deg.empty()) calc_deg();
    return vc_deg;
  }

  pair<vc<int>, vc<int>> deg_array_inout() {
    if (vc_indeg.empty()) calc_deg_inout();
    return {vc_indeg, vc_outdeg};
  }

  int deg(int v) {
    if (vc_deg.empty()) calc_deg();
    return vc_deg[v];
  }

  int in_deg(int v) {
    if (vc_indeg.empty()) calc_deg_inout();
    return vc_indeg[v];
  }

  int out_deg(int v) {
    if (vc_outdeg.empty()) calc_deg_inout();
    return vc_outdeg[v];
  }

#ifdef FASTIO
  void debug() {
    print("Graph");
    if (!prepared) {
      print("frm to cost id");
      for (auto&& e: edges) print(e.frm, e.to, e.cost, e.id);
    } else {
      print("indptr", indptr);
      print("frm to cost id");
      FOR(v, N) for (auto&& e: (*this)[v]) print(e.frm, e.to, e.cost, e.id);
    }
  }
#endif

  vc<int> new_idx;
  vc<bool> used_e;

  // G における頂点 V[i] が、新しいグラフで i になるようにする
  // {G, es}
  // sum(deg(v)) の計算量になっていて、
  // 新しいグラフの n+m より大きい可能性があるので注意
  Graph<T, directed> rearrange(vc<int> V, bool keep_eid = 0) {
    if (len(new_idx) != N) new_idx.assign(N, -1);
    int n = len(V);
    FOR(i, n) new_idx[V[i]] = i;
    Graph<T, directed> G(n);
    vc<int> history;
    FOR(i, n) {
      for (auto&& e: (*this)[V[i]]) {
        if (len(used_e) <= e.id) used_e.resize(e.id + 1);
        if (used_e[e.id]) continue;
        int a = e.frm, b = e.to;
        if (new_idx[a] != -1 && new_idx[b] != -1) {
          history.eb(e.id);
          used_e[e.id] = 1;
          int eid = (keep_eid ? e.id : -1);
          G.add(new_idx[a], new_idx[b], e.cost, eid);
        }
      }
    }
    FOR(i, n) new_idx[V[i]] = -1;
    for (auto&& eid: history) used_e[eid] = 0;
    G.build();
    return G;
  }

  Graph<T, true> to_directed_tree(int root = -1) {
    if (root == -1) root = 0;
    assert(!is_directed && prepared && M == N - 1);
    Graph<T, true> G1(N);
    vc<int> par(N, -1);
    auto dfs = [&](auto& dfs, int v) -> void {
      for (auto& e: (*this)[v]) {
        if (e.to == par[v]) continue;
        par[e.to] = v, dfs(dfs, e.to);
      }
    };
    dfs(dfs, root);
    for (auto& e: edges) {
      int a = e.frm, b = e.to;
      if (par[a] == b) swap(a, b);
      assert(par[b] == a);
      G1.add(a, b, e.cost);
    }
    G1.build();
    return G1;
  }

private:
  void calc_deg() {
    assert(vc_deg.empty());
    vc_deg.resize(N);
    for (auto&& e: edges) vc_deg[e.frm]++, vc_deg[e.to]++;
  }

  void calc_deg_inout() {
    assert(vc_indeg.empty());
    vc_indeg.resize(N);
    vc_outdeg.resize(N);
    for (auto&& e: edges) { vc_indeg[e.to]++, vc_outdeg[e.frm]++; }
  }
};
#line 4 "/home/maspy/compro/library/graph/tree.hpp"

// HLD euler tour をとっていろいろ。
template <typename GT>
struct Tree {
  using Graph_type = GT;
  GT &G;
  using WT = typename GT::cost_type;
  int N;
  vector<int> LID, RID, head, V, parent, VtoE;
  vc<int> depth;
  vc<WT> depth_weighted;

  Tree(GT &G, int r = 0, bool hld = 1) : G(G) { build(r, hld); }

  void build(int r = 0, bool hld = 1) {
    if (r == -1) return; // build を遅延したいとき
    N = G.N;
    LID.assign(N, -1), RID.assign(N, -1), head.assign(N, r);
    V.assign(N, -1), parent.assign(N, -1), VtoE.assign(N, -1);
    depth.assign(N, -1), depth_weighted.assign(N, 0);
    assert(G.is_prepared());
    int t1 = 0;
    dfs_sz(r, -1, hld);
    dfs_hld(r, t1);
  }

  void dfs_sz(int v, int p, bool hld) {
    auto &sz = RID;
    parent[v] = p;
    depth[v] = (p == -1 ? 0 : depth[p] + 1);
    sz[v] = 1;
    int l = G.indptr[v], r = G.indptr[v + 1];
    auto &csr = G.csr_edges;
    // 使う辺があれば先頭にする
    for (int i = r - 2; i >= l; --i) {
      if (hld && depth[csr[i + 1].to] == -1) swap(csr[i], csr[i + 1]);
    }
    int hld_sz = 0;
    for (int i = l; i < r; ++i) {
      auto e = csr[i];
      if (depth[e.to] != -1) continue;
      depth_weighted[e.to] = depth_weighted[v] + e.cost;
      VtoE[e.to] = e.id;
      dfs_sz(e.to, v, hld);
      sz[v] += sz[e.to];
      if (hld && chmax(hld_sz, sz[e.to]) && l < i) { swap(csr[l], csr[i]); }
    }
  }

  void dfs_hld(int v, int &times) {
    LID[v] = times++;
    RID[v] += LID[v];
    V[LID[v]] = v;
    bool heavy = true;
    for (auto &&e: G[v]) {
      if (depth[e.to] <= depth[v]) continue;
      head[e.to] = (heavy ? head[v] : e.to);
      heavy = false;
      dfs_hld(e.to, times);
    }
  }

  vc<int> heavy_path_at(int v) {
    vc<int> P = {v};
    while (1) {
      int a = P.back();
      for (auto &&e: G[a]) {
        if (e.to != parent[a] && head[e.to] == v) {
          P.eb(e.to);
          break;
        }
      }
      if (P.back() == a) break;
    }
    return P;
  }

  int heavy_child(int v) {
    int k = LID[v] + 1;
    if (k == N) return -1;
    int w = V[k];
    return (parent[w] == v ? w : -1);
  }

  int e_to_v(int eid) {
    auto e = G.edges[eid];
    return (parent[e.frm] == e.to ? e.frm : e.to);
  }
  int v_to_e(int v) { return VtoE[v]; }
  int get_eid(int u, int v) {
    if (parent[u] != v) swap(u, v);
    assert(parent[u] == v);
    return VtoE[u];
  }

  int ELID(int v) { return 2 * LID[v] - depth[v]; }
  int ERID(int v) { return 2 * RID[v] - depth[v] - 1; }

  // 目標地点へ進む個数が k
  int LA(int v, int k) {
    assert(k <= depth[v]);
    while (1) {
      int u = head[v];
      if (LID[v] - k >= LID[u]) return V[LID[v] - k];
      k -= LID[v] - LID[u] + 1;
      v = parent[u];
    }
  }
  int la(int u, int v) { return LA(u, v); }

  int LCA(int u, int v) {
    for (;; v = parent[head[v]]) {
      if (LID[u] > LID[v]) swap(u, v);
      if (head[u] == head[v]) return u;
    }
  }

  int meet(int a, int b, int c) { return LCA(a, b) ^ LCA(a, c) ^ LCA(b, c); }
  int lca(int u, int v) { return LCA(u, v); }

  int subtree_size(int v, int root = -1) {
    if (root == -1) return RID[v] - LID[v];
    if (v == root) return N;
    int x = jump(v, root, 1);
    if (in_subtree(v, x)) return RID[v] - LID[v];
    return N - RID[x] + LID[x];
  }

  int dist(int a, int b) {
    int c = LCA(a, b);
    return depth[a] + depth[b] - 2 * depth[c];
  }

  WT dist_weighted(int a, int b) {
    int c = LCA(a, b);
    return depth_weighted[a] + depth_weighted[b] - WT(2) * depth_weighted[c];
  }

  // a is in b
  bool in_subtree(int a, int b) { return LID[b] <= LID[a] && LID[a] < RID[b]; }

  int jump(int a, int b, ll k) {
    if (k == 1) {
      if (a == b) return -1;
      return (in_subtree(b, a) ? LA(b, depth[b] - depth[a] - 1) : parent[a]);
    }
    int c = LCA(a, b);
    int d_ac = depth[a] - depth[c];
    int d_bc = depth[b] - depth[c];
    if (k > d_ac + d_bc) return -1;
    if (k <= d_ac) return LA(a, k);
    return LA(b, d_ac + d_bc - k);
  }

  vc<int> collect_child(int v) {
    vc<int> res;
    for (auto &&e: G[v])
      if (e.to != parent[v]) res.eb(e.to);
    return res;
  }

  vc<int> collect_light(int v) {
    vc<int> res;
    bool skip = true;
    for (auto &&e: G[v])
      if (e.to != parent[v]) {
        if (!skip) res.eb(e.to);
        skip = false;
      }
    return res;
  }

  vc<pair<int, int>> get_path_decomposition(int u, int v, bool edge) {
    // [始点, 終点] の"閉"区間列。
    vc<pair<int, int>> up, down;
    while (1) {
      if (head[u] == head[v]) break;
      if (LID[u] < LID[v]) {
        down.eb(LID[head[v]], LID[v]);
        v = parent[head[v]];
      } else {
        up.eb(LID[u], LID[head[u]]);
        u = parent[head[u]];
      }
    }
    if (LID[u] < LID[v]) down.eb(LID[u] + edge, LID[v]);
    elif (LID[v] + edge <= LID[u]) up.eb(LID[u], LID[v] + edge);
    reverse(all(down));
    up.insert(up.end(), all(down));
    return up;
  }

  // 辺の列の情報 (frm,to,str)
  // str = "heavy_up", "heavy_down", "light_up", "light_down"
  vc<tuple<int, int, string>> get_path_decomposition_detail(int u, int v) {
    vc<tuple<int, int, string>> up, down;
    while (1) {
      if (head[u] == head[v]) break;
      if (LID[u] < LID[v]) {
        if (v != head[v]) down.eb(head[v], v, "heavy_down"), v = head[v];
        down.eb(parent[v], v, "light_down"), v = parent[v];
      } else {
        if (u != head[u]) up.eb(u, head[u], "heavy_up"), u = head[u];
        up.eb(u, parent[u], "light_up"), u = parent[u];
      }
    }
    if (LID[u] < LID[v]) down.eb(u, v, "heavy_down");
    elif (LID[v] < LID[u]) up.eb(u, v, "heavy_up");
    reverse(all(down));
    concat(up, down);
    return up;
  }

  vc<int> restore_path(int u, int v) {
    vc<int> P;
    for (auto &&[a, b]: get_path_decomposition(u, v, 0)) {
      if (a <= b) {
        FOR(i, a, b + 1) P.eb(V[i]);
      } else {
        FOR_R(i, b, a + 1) P.eb(V[i]);
      }
    }
    return P;
  }

  // path [a,b] と [c,d] の交わり. 空ならば {-1,-1}.
  // https://codeforces.com/problemset/problem/500/G
  pair<int, int> path_intersection(int a, int b, int c, int d) {
    int ab = lca(a, b), ac = lca(a, c), ad = lca(a, d);
    int bc = lca(b, c), bd = lca(b, d), cd = lca(c, d);
    int x = ab ^ ac ^ bc, y = ab ^ ad ^ bd; // meet(a,b,c), meet(a,b,d)
    if (x != y) return {x, y};
    int z = ac ^ ad ^ cd;
    if (x != z) x = -1;
    return {x, x};
  }
};
#line 2 "/home/maspy/compro/library/ds/splaytree/splaytree.hpp"

/*
update でちゃんと prod が計算されてくれれば prod は op(lprod,x,rprod) でなくてもよい.
*/

// Node 型を別に定義して使う
template <typename Node>
struct SplayTree {
  Node *pool;
  const int NODES;
  int pid;
  using np = Node *;
  using X = typename Node::value_type;
  using A = typename Node::operator_type;
  vc<np> FREE;

  SplayTree(int NODES) : NODES(NODES), pid(0) { pool = new Node[NODES]; }
  ~SplayTree() { delete[] pool; }

  void free_subtree(np c) {
    if (!c) return;
    auto dfs = [&](auto &dfs, np c) -> void {
      if (c->l) dfs(dfs, c->l);
      if (c->r) dfs(dfs, c->r);
      FREE.eb(c);
    };
    dfs(dfs, c);
  }

  void reset() {
    pid = 0;
    FREE.clear();
  }

  np new_root() { return nullptr; }

  np new_node(const X &x) {
    assert(!FREE.empty() || pid < NODES);
    np n = (FREE.empty() ? &(pool[pid++]) : POP(FREE));
    Node::new_node(n, x);
    return n;
  }

  np new_node(const vc<X> &dat) {
    auto dfs = [&](auto &dfs, int l, int r) -> np {
      if (l == r) return nullptr;
      if (r == l + 1) return new_node(dat[l]);
      int m = (l + r) / 2;
      np l_root = dfs(dfs, l, m);
      np r_root = dfs(dfs, m + 1, r);
      np root = new_node(dat[m]);
      root->l = l_root, root->r = r_root;
      if (l_root) l_root->p = root;
      if (r_root) r_root->p = root;
      root->update();
      return root;
    };
    return dfs(dfs, 0, len(dat));
  }

  u32 get_size(np root) { return (root ? root->size : 0); }

  np merge(np l_root, np r_root) {
    if (!l_root) return r_root;
    if (!r_root) return l_root;
    assert((!l_root->p) && (!r_root->p));
    splay_kth(r_root, 0); // splay したので prop 済
    r_root->l = l_root;
    l_root->p = r_root;
    r_root->update();
    return r_root;
  }
  np merge3(np a, np b, np c) { return merge(merge(a, b), c); }
  np merge4(np a, np b, np c, np d) { return merge(merge(merge(a, b), c), d); }

  pair<np, np> split(np root, u32 k) {
    assert(!root || !root->p);
    if (k == 0) return {nullptr, root};
    if (k == (root->size)) return {root, nullptr};
    splay_kth(root, k - 1);
    np right = root->r;
    root->r = nullptr, right->p = nullptr;
    root->update();
    return {root, right};
  }
  tuple<np, np, np> split3(np root, u32 l, u32 r) {
    np nm, nr;
    tie(root, nr) = split(root, r);
    tie(root, nm) = split(root, l);
    return {root, nm, nr};
  }
  tuple<np, np, np, np> split4(np root, u32 i, u32 j, u32 k) {
    np d;
    tie(root, d) = split(root, k);
    auto [a, b, c] = split3(root, i, j);
    return {a, b, c, d};
  }

  // 部分木が区間 [l,r) に対応するようなノードを作って返す
  // そのノードが root になるわけではないので、
  // このノードを参照した後にすぐに splay して根に持ち上げること
  void goto_between(np &root, u32 l, u32 r) {
    if (l == 0 && r == root->size) return;
    if (l == 0) {
      splay_kth(root, r);
      root = root->l;
      return;
    }
    if (r == root->size) {
      splay_kth(root, l - 1);
      root = root->r;
      return;
    }
    splay_kth(root, r);
    np rp = root;
    root = rp->l;
    root->p = nullptr;
    splay_kth(root, l - 1);
    root->p = rp;
    rp->l = root;
    rp->update();
    root = root->r;
  }

  vc<X> get_all(const np &root) {
    vc<X> res;
    auto dfs = [&](auto &dfs, np root) -> void {
      if (!root) return;
      root->prop();
      dfs(dfs, root->l);
      res.eb(root->get());
      dfs(dfs, root->r);
    };
    dfs(dfs, root);
    return res;
  }

  X get(np &root, u32 k) {
    assert(root == nullptr || !root->p);
    splay_kth(root, k);
    return root->get();
  }

  void set(np &root, u32 k, const X &x) {
    assert(root != nullptr && !root->p);
    splay_kth(root, k);
    root->set(x);
  }

  void multiply(np &root, u32 k, const X &x) {
    assert(root != nullptr && !root->p);
    splay_kth(root, k);
    root->multiply(x);
  }

  X prod(np &root, u32 l, u32 r) {
    assert(root == nullptr || !root->p);
    using Mono = typename Node::Monoid_X;
    if (l == r) return Mono::unit();
    assert(0 <= l && l < r && r <= root->size);
    goto_between(root, l, r);
    X res = root->prod;
    splay(root, true);
    return res;
  }

  X prod(np &root) {
    assert(root == nullptr || !root->p);
    using Mono = typename Node::Monoid_X;
    return (root ? root->prod : Mono::unit());
  }

  void apply(np &root, u32 l, u32 r, const A &a) {
    if (l == r) return;
    assert(0 <= l && l < r && r <= root->size);
    goto_between(root, l, r);
    root->apply(a);
    splay(root, true);
  }
  void apply(np &root, const A &a) {
    if (!root) return;
    root->apply(a);
  }

  void reverse(np &root, u32 l, u32 r) {
    assert(root == nullptr || !root->p);
    if (l == r) return;
    assert(0 <= l && l < r && r <= root->size);
    goto_between(root, l, r);
    root->reverse();
    splay(root, true);
  }
  void reverse(np root) {
    if (!root) return;
    root->reverse();
  }

  void rotate(Node *n) {
    // n を根に近づける。prop, update は rotate の外で行う。
    Node *pp, *p, *c;
    p = n->p;
    pp = p->p;
    if (p->l == n) {
      c = n->r;
      n->r = p;
      p->l = c;
    } else {
      c = n->l;
      n->l = p;
      p->r = c;
    }
    if (pp && pp->l == p) pp->l = n;
    if (pp && pp->r == p) pp->r = n;
    n->p = pp;
    p->p = n;
    if (c) c->p = p;
  }

  void prop_from_root(np c) {
    if (!c->p) {
      c->prop();
      return;
    }
    prop_from_root(c->p);
    c->prop();
  }

  void splay(Node *me, bool prop_from_root_done) {
    // これを呼ぶ時点で、me の祖先(me を除く)は既に prop 済であることを仮定
    // 特に、splay 終了時点で me は upd / prop 済である
    if (!prop_from_root_done) prop_from_root(me);
    me->prop();
    while (me->p) {
      np p = me->p;
      np pp = p->p;
      if (!pp) {
        rotate(me);
        p->update();
        break;
      }
      bool same = (p->l == me && pp->l == p) || (p->r == me && pp->r == p);
      if (same) rotate(p), rotate(me);
      if (!same) rotate(me), rotate(me);
      pp->update(), p->update();
    }
    // me の update は最後だけでよい
    me->update();
  }

  void splay_kth(np &root, u32 k) {
    assert(0 <= k && k < (root->size));
    while (1) {
      root->prop();
      u32 sl = (root->l ? root->l->size : 0);
      if (k == sl) break;
      if (k < sl)
        root = root->l;
      else {
        k -= sl + 1;
        root = root->r;
      }
    }
    splay(root, true);
  }

  // check(x), 左側のノード全体が check を満たすように切る
  template <typename F>
  pair<np, np> split_max_right(np root, F check) {
    if (!root) return {nullptr, nullptr};
    assert(!root->p);
    np c = find_max_right(root, check);
    if (!c) {
      splay(root, true);
      return {nullptr, root};
    }
    splay(c, true);
    np right = c->r;
    if (!right) return {c, nullptr};
    right->p = nullptr;
    c->r = nullptr;
    c->update();
    return {c, right};
  }

  // check(x, cnt), 左側のノード全体が check を満たすように切る
  template <typename F>
  pair<np, np> split_max_right_cnt(np root, F check) {
    if (!root) return {nullptr, nullptr};
    assert(!root->p);
    np c = find_max_right_cnt(root, check);
    if (!c) {
      splay(root, true);
      return {nullptr, root};
    }
    splay(c, true);
    np right = c->r;
    if (!right) return {c, nullptr};
    right->p = nullptr;
    c->r = nullptr;
    c->update();
    return {c, right};
  }

  // 左側のノード全体の prod が check を満たすように切る
  template <typename F>
  pair<np, np> split_max_right_prod(np root, F check) {
    if (!root) return {nullptr, nullptr};
    assert(!root->p);
    np c = find_max_right_prod(root, check);
    if (!c) {
      splay(root, true);
      return {nullptr, root};
    }
    splay(c, true);
    np right = c->r;
    if (!right) return {c, nullptr};
    right->p = nullptr;
    c->r = nullptr;
    c->update();
    return {c, right};
  }

  template <typename F>
  np find_max_right(np root, const F &check) {
    // 最後に見つけた ok の点、最後に探索した点
    np last_ok = nullptr, last = nullptr;
    while (root) {
      last = root;
      root->prop();
      if (check(root->x)) {
        last_ok = root;
        root = root->r;
      } else {
        root = root->l;
      }
    }
    splay(last, true);
    return last_ok;
  }

  template <typename F>
  np find_max_right_cnt(np root, const F &check) {
    // 最後に見つけた ok の点、最後に探索した点
    np last_ok = nullptr, last = nullptr;
    ll n = 0;
    while (root) {
      last = root;
      root->prop();
      ll ns = (root->l ? root->l->size : 0);
      if (check(root->x, n + ns + 1)) {
        last_ok = root;
        n += ns + 1;
        root = root->r;
      } else {
        root = root->l;
      }
    }
    splay(last, true);
    return last_ok;
  }

  template <typename F>
  np find_max_right_prod(np root, const F &check) {
    using Mono = typename Node::Monoid_X;
    X prod = Mono::unit();
    // 最後に見つけた ok の点、最後に探索した点
    np last_ok = nullptr, last = nullptr;
    while (root) {
      last = root;
      root->prop();
      np tmp = root->r;
      root->r = nullptr;
      root->update();
      X lprod = Mono::op(prod, root->prod);
      root->r = tmp;
      root->update();
      if (check(lprod)) {
        prod = lprod;
        last_ok = root;
        root = root->r;
      } else {
        root = root->l;
      }
    }
    splay(last, true);
    return last_ok;
  }
};
#line 2 "/home/maspy/compro/library/alg/monoid/add_pair.hpp"

template <typename E>
struct Monoid_Add_Pair {
  using value_type = pair<E, E>;
  using X = value_type;
  static constexpr X op(const X &x, const X &y) {
    return {x.fi + y.fi, x.se + y.se};
  }
  static constexpr X inverse(const X &x) { return {-x.fi, -x.se}; }
  static constexpr X unit() { return {0, 0}; }
  static constexpr bool commute = true;
};
#line 3 "/home/maspy/compro/library/convex/slope_super.hpp"

namespace SLOPE_TRICK_SUPER {
/*
傾きと座標が全部 T.
(x0,y0,a0) / 傾き変化を splay tree で持つ.
末尾には必ず infty が入っているようにする.
(0,10),(1,6),(3,4),(6,7)
->
(x0,y0,a0)=(0,10,-4)
dat = ([1,3],[3,2])

f(x) の計算, (min,argmin) の計算
加法, 畳み込み

加法: easy
f(x) の計算: sum(a), sum(xa) が要る
畳み込み: x->x+c が要る
*/

template <typename T>
struct Node {
  using value_type = pair<T, T>;
  using operator_type = T;
  using np = Node *;
  using Monoid_X = Monoid_Add_Pair<T>;

  np p, l, r;
  bool rev;
  u32 size;
  pair<T, T> x;    // (x,a)
  pair<T, T> prod; // (a sum, xa sum)
  T add_x;

  static void new_node(np n, const pair<T, T> &x) {
    n->p = n->l = n->r = nullptr, n->rev = 0, n->size = 1;
    n->x = x, n->prod = {x.se, x.fi * x.se}, n->add_x = 0;
  }

  void update() {
    size = 1;
    if (l) { size += l->size; }
    if (r) { size += r->size; }
    prod = {x.se, x.fi * x.se};
    if (l) prod = Monoid_X::op(prod, l->prod);
    if (r) prod = Monoid_X::op(prod, r->prod);
  }

  void prop() {
    assert(!rev);
    if (add_x == 0) return;
    if (l) l->x.fi += add_x, l->prod.se += l->prod.fi * add_x, l->add_x += add_x;
    if (r) r->x.fi += add_x, r->prod.se += r->prod.fi * add_x, r->add_x += add_x;
    add_x = 0;
  }

  void apply(T a) { x.fi += a, prod.se += a * prod.fi, add_x += a; }

  // update, prop 以外で呼ばれるものは、splay 後であることが想定されている。
  // したがってその時点で update, prop 済であることを仮定してよい。
  pair<T, T> get() { return x; }
  void set(const pair<T, T> &xx) {
    x = xx;
    update();
  }
};

// 関数は破壊的な変更にされる
template <typename T>
struct Slope_Trick_Super {
  SplayTree<Node<T>> ST;
  using np = Node<T> *;

  struct FUNC {
    np root; // 定義域がこわれていたら root == empty
    T x0, x1, a0, y0;
    int size() { return (root ? root->size : 0); }
  };

  Slope_Trick_Super(int NODES) : ST(NODES) {}

  // (L,R,a,b) : [L,R] で y=ax+b
  FUNC segment_func(T L, T R, T a, T b) { return {nullptr, L, R, a, a * L + b}; }
  FUNC from_points(vc<pair<T, T>> &point) {
    return from_points(len(point), [&](int i) -> pair<T, T> { return point[i]; });
  }
  template <typename F>
  FUNC from_points(int N, F f) {
    vc<T> X(N), Y(N);
    FOR(i, N) tie(X[i], Y[i]) = f(i);
    if (N == 1) return segment_func(X[0], X[0], 0, Y[0]);
    T a0 = (Y[1] - Y[0]) / (X[1] - X[0]);
    T x0 = X[0], x1 = X.back();
    vc<pair<T, T>> dat;
    T a = a0;
    FOR(i, 1, N - 1) {
      T a1 = (Y[i + 1] - Y[i]) / (X[i + 1] - X[i]);
      dat.eb(X[i], a1 - a), a = a1;
    }
    return FUNC{ST.new_node(dat), x0, x1, a0, Y[0]};
  }

  pair<T, T> domain(FUNC &f) { return {f.x0, f.x1}; }
  T eval(FUNC &f, T x) {
    auto [x0, x1] = domain(f);
    if (!(x0 <= x && x <= x1)) return infty<T>;
    auto [l, r] = ST.split_max_right(f.root, [&](auto dat) -> bool { return dat.fi <= x; });
    auto [a_sum, xa_sum] = ST.prod(l);
    f.root = ST.merge(l, r);
    return f.y0 + f.a0 * (x - x0) + a_sum * x - xa_sum;
  }
  FUNC restrict_domain(FUNC &f, T L, T R) {
    auto [x0, x1] = domain(f);
    chmax(L, x0), chmin(R, x1);
    if (L > R) {
      ST.free_subtree(f.root), f.root = nullptr;
      f.root = nullptr;
      f.x0 = infty<T>, f.x1 = -infty<T>;
      return f;
    }
    // まずは右側をちぢめる. R 以上の傾き変化を消してしまえばよい
    auto [l, r] = ST.split_max_right(f.root, [&](auto dat) -> bool { return dat.fi < R; });
    ST.free_subtree(r);
    // 左側をちぢめる.
    tie(l, r) = ST.split_max_right(l, [&](auto dat) -> bool { return dat.fi <= L; });
    auto [a_sum, xa_sum] = ST.prod(l);
    T new_a0 = f.a0 + a_sum;
    T new_y0 = f.y0 + f.a0 * (L - x0) + a_sum * L - xa_sum;
    ST.free_subtree(l);
    f.root = r, f.x0 = L, f.x1 = R, f.a0 = new_a0, f.y0 = new_y0;
    return f;
  }
  FUNC add(FUNC &f, FUNC &g) {
    T x0 = max(f.x0, g.x0);
    T x1 = min(f.x1, g.x1);
    restrict_domain(f, x0, x1), restrict_domain(g, x0, x1);
    if (x0 > x1) return f;
    T y0 = f.y0 + g.y0, a0 = f.a0 + g.a0;

    if (len(f) < len(g)) swap(f, g);
    auto tmp = ST.get_all(g.root);
    ST.free_subtree(g.root);
    f.x0 = x0, f.x1 = x1, f.a0 = a0, f.y0 = y0;
    if (!f.root) {
      f.root = ST.new_node(tmp);
      return f;
    }
    // あとは単に tmp を挿入していけばいい
    auto dfs = [&](auto &dfs, np root, int l, int r) -> void {
      if (l == r) return;
      root->prop();
      T x = root->x.fi;
      // [l,m),[m,r)
      int m = binary_search([&](int i) -> bool { return tmp[i].fi >= x; }, r, l - 1, 0);
      if (l < m) {
        if (!root->l) {
          root->l = ST.new_node({tmp.begin() + l, tmp.begin() + m});
        } else {
          dfs(dfs, root->l, l, m);
        }
        root->l->p = root;
      }
      if (m < r) {
        if (!root->r) {
          root->r = ST.new_node({tmp.begin() + m, tmp.begin() + r});
        } else {
          dfs(dfs, root->r, m, r);
        }
        root->r->p = root;
      }
      root->update();
    };
    dfs(dfs, f.root, 0, len(tmp));
    return f;
  }
  FUNC sum_all(vc<FUNC> &funcs) {
    assert(len(funcs) >= 1);
    T x0 = funcs[0].x0, x1 = funcs[0].x1;
    for (auto &g: funcs) chmax(x0, g.x0), chmin(x1, g.x1);
    if (x0 > x1) {
      for (auto &f: funcs) { ST.free_subtree(f.root); }
      return {nullptr, infty<T>, -infty<T>, 0, 0};
    }
    for (auto &f: funcs) f = restrict_domain(f, x0, x1);
    int idx = 0;
    FOR(i, 1, len(funcs)) if (len(funcs[idx]) < len(funcs[i])) idx = i;
    swap(funcs[idx], funcs.back());
    FUNC f = POP(funcs);
    vc<pair<T, T>> dat;
    for (auto &g: funcs) {
      f.y0 += g.y0, f.a0 += g.a0;
      auto tmp = ST.get_all(g.root);
      concat(dat, tmp);
      ST.free_subtree(g.root);
    }
    sort(all(dat));
    // あとは単に dat を挿入していけばいい
    if (!f.root) {
      f.root = ST.new_node(dat);
      return f;
    }
    auto dfs = [&](auto &dfs, np root, int l, int r) -> void {
      if (l == r) return;
      root->prop();
      T x = root->x.fi;
      // [l,m),[m,r)
      int m = binary_search([&](int i) -> bool { return dat[i].fi >= x; }, r, l - 1, 0);
      if (l < m) {
        if (!root->l) {
          root->l = ST.new_node({dat.begin() + l, dat.begin() + m});
        } else {
          dfs(dfs, root->l, l, m);
        }
        root->l->p = root;
      }
      if (m < r) {
        if (!root->r) {
          root->r = ST.new_node({dat.begin() + m, dat.begin() + r});
        } else {
          dfs(dfs, root->r, m, r);
        }
        root->r->p = root;
      }
      root->update();
    };
    dfs(dfs, f.root, 0, len(dat));
    return f;
  }

  FUNC shift(FUNC &f, T add_x, T add_y) {
    ST.apply(f.root, add_x);
    f.x0 += add_x, f.x1 += add_x, f.y0 += add_y;
    return f;
  }

  // h[z]=min(x+y==z)f(x)+g(y)
  FUNC convolve(FUNC &f, FUNC &g) {
    if (f.x0 > f.x1 || g.x0 > g.x1) { return {nullptr, infty<T>, -infty<T>, 0, 0}; }
    if (len(f) < len(g)) swap(f, g);
    shift(f, g.x0, g.y0), shift(g, -g.x0, -g.y0);
    if (len(g) == 0) { return convolve_segment(f, 0, g.x1, g.a0, 0); }
    auto tmp = ST.get_all(g.root);
    ST.free_subtree(g.root);
    f = convolve_segment(f, 0, tmp[0].fi, g.a0, 0);
    T a = g.a0;
    FOR(i, len(tmp)) {
      T nx = (i + 1 < len(tmp) ? tmp[i + 1].fi : g.x1);
      a += tmp[i].se;
      f = convolve_segment(f, 0, nx - tmp[i].fi, a, 0);
      for (auto &[x, a]: ST.get_all(f.root)) {
        assert(f.x0 <= x && x <= f.x1);
        if (f.root) assert(!f.root->p);
      }
    }
    return f;
  }

  // [x0,x1], y=ax+b
  FUNC convolve_segment(FUNC &f, T x0, T x1, T a, T b) {
    assert(x0 <= x1);
    if (f.x0 > f.x1) { return {nullptr, infty<T>, -infty<T>, 0, 0}; }
    f = shift(f, x0, a * x0 + b);
    T d = x1 - x0;
    if (d == 0) return f;
    // (0,0) から (x1,ax1) への線分をどこかに挿入する
    // 特に x0, y0 はこのままでよい
    if (f.x0 == f.x1) { return {nullptr, f.x0, f.x0 + d, a, f.y0}; }
    // 先頭に挿入できる場合
    if (a <= f.a0) {
      ST.apply(f.root, d);
      f.root = ST.merge(ST.new_node({f.x0 + d, f.a0 - a}), f.root);
      f.x1 += d, f.a0 = a;
      return f;
    }
    // 末尾に挿入できる場合
    T a_last = f.a0 + ST.prod(f.root).fi;
    if (a_last <= a) {
      f.root = ST.merge(f.root, ST.new_node({f.x1, a - a_last}));
      f.x1 += d;
      return f;
    }
    // 間のどこかに挿入
    auto [l, r] = ST.split_max_right_prod(f.root, [&](auto prod) -> bool { return f.a0 + prod.fi < a; });
    T asum = ST.prod(l).fi;
    T a1 = a - (asum + f.a0);
    auto [xx, aa] = ST.get(r, 0);
    ST.apply(r, d);
    ST.set(r, 0, {xx + d, aa - a1});
    f.root = ST.merge3(l, ST.new_node({xx, a1}), r);
    f.x1 += d;
    return f;
  }

  FUNC add_const(FUNC &f, T a) {
    f.y0 += a;
    return f;
  }

  FUNC add_linear(FUNC &f, T a, T b) {
    f.y0 += a * f.x0 + b;
    f.a0 += a;
    return f;
  }

  // (a-x)+
  FUNC add_a_minus_x(FUNC &f, T a) {
    auto [x0, x1] = domain(f);
    if (x0 > x1) return f;
    if (a <= x0) return f;
    if (x1 <= a) return add_linear(f, -1, a);
    vc<pair<T, T>> point;
    point.eb(x0, a - x0), point.eb(a, 0), point.eb(x1, 0);
    FUNC g = from_points(point);
    return add(f, g);
  }

  // (x-a)+
  FUNC add_x_minus_a(FUNC &f, T a) {
    auto [x0, x1] = domain(f);
    if (x0 > x1) return f;
    if (a <= x0) return add_linear(f, 1, -a);
    if (x1 <= a) return f;
    vc<pair<T, T>> point;
    point.eb(x0, 0), point.eb(a, 0), point.eb(x1, x1 - a);
    FUNC g = from_points(point);
    return add(f, g);
  }

  // |x-a|
  FUNC add_abs(FUNC &f, T a) {
    f = add_a_minus_x(f, a);
    f = add_x_minus_a(f, a);
    return f;
  }

  // fx,x
  pair<T, T> get_min(FUNC &f) {
    if (f.x0 > f.x1) return {infty<T>, 0};
    if (f.a0 >= 0) { return {f.y0, f.x0}; }
    auto [l, r] = ST.split_max_right_prod(f.root, [&](auto prod) -> bool { return f.a0 + prod.fi < 0; });
    auto [asum, xasum] = ST.prod(l);
    T x = (r ? ST.get(r, 0).fi : f.x1);
    f.root = ST.merge(l, r);
    T y = f.y0 + f.a0 * (x - f.x0) + x * asum - xasum;
    return {y, x};
  }

  FUNC clear_right(FUNC &f) {
    if (f.a0 >= 0) {
      ST.free_subtree(f.root), f.root = nullptr, f.a0 = 0;
      return f;
    }
    auto [l, r] = ST.split_max_right_prod(f.root, [&](auto prod) -> bool { return f.a0 + prod.fi < 0; });
    f.root = l;
    if (!r) { return f; }
    T x = ST.get(r, 0).fi;
    ST.free_subtree(r);
    f.root = ST.merge(f.root, ST.new_node({x, -(f.a0 + ST.prod(l).fi)}));
    return f;
  }
  FUNC clear_left(FUNC &f) {
    if (f.a0 >= 0) { return f; }
    auto [l, r] = ST.split_max_right_prod(f.root, [&](auto prod) -> bool { return f.a0 + prod.fi < 0; });
    auto [asum, xasum] = ST.prod(l);
    if (!r) {
      // 定数にする
      T x = f.x1;
      T y = f.y0 + f.a0 * (x - f.x0) + x * asum - xasum;
      ST.free_subtree(l);
      f.root = nullptr;
      f.y0 = y, f.a0 = 0;
      return f;
    }
    T x = ST.get(f.root, 0).fi;
    T y = f.y0 + f.a0 * (x - f.x0) + x * asum - xasum;
    T a = f.a0 + asum + ST.get(r, 0).se;
    ST.free_subtree(l);
    f.root = r;
    ST.set(r, 0, {x, a});
    f.y0 = y;
    f.a0 = 0;
    return f;
  }
#ifdef FASTIO
  void debug(FUNC &f) {
    auto dat = ST.get_all(f.root);
    SHOW(f.x0, f.x1, f.a0, f.y0);
    SHOW(dat);
  }
#endif
};
} // namespace SLOPE_TRICK_SUPER
using SLOPE_TRICK_SUPER::Slope_Trick_Super;
#line 6 "main.cpp"

void solve() {
  LL(N, M);
  Graph<ll, 1> G(N + M);
  FOR(v, 1, N + M) {
    LL(p, c);
    --p;
    G.add(p, v, c);
  }
  G.build();

  Slope_Trick_Super<i128> ST(2 * (N + M));
  using F = decltype(ST)::FUNC;
  using np = decltype(ST)::np;

  auto dfs = [&](auto& dfs, int v) -> F {
    if (N <= v) { return ST.segment_func(0, infty<ll>, 1, 0); }
    vc<F> funcs;
    funcs.eb(ST.segment_func(0, infty<ll>, 0, 0));
    for (auto& e: G[v]) {
      auto g = dfs(dfs, e.to);
      auto h = ST.segment_func(0, infty<ll>, 0, 0);
      ST.add_abs(h, e.cost);
      g = ST.convolve(g, h);
      g = ST.restrict_domain(g, 0, infty<ll>);
      funcs.eb(g);
    }
    return ST.sum_all(funcs);
  };
  F f = dfs(dfs, 0);
  auto [fx, x] = ST.get_min(f);
  print(fx);
}

signed main() { solve(); }

详细

Subtask #1:

score: 7
Accepted

Test #1:

score: 7
Accepted
time: 0ms
memory: 3808kb

input:

1 1
1 594911537

output:

0

result:

ok single line: '0'

Test #2:

score: 7
Accepted
time: 0ms
memory: 3780kb

input:

1 9
1 825879648
1 544663269
1 523587648
1 265820061
1 715816315
1 46975056
1 181647299
1 679235405
1 657226464

output:

1860127768

result:

ok single line: '1860127768'

Test #3:

score: 7
Accepted
time: 1ms
memory: 3824kb

input:

1 91
1 385900550
1 26102977
1 667546315
1 172015997
1 279290345
1 307280944
1 617208234
1 356267123
1 736729319
1 625308102
1 760176838
1 58322561
1 791921572
1 668621731
1 363109524
1 158699815
1 131392063
1 584992804
1 691545613
1 974934420
1 997953539
1 876137720
1 564678478
1 746582187
1 8674083...

output:

22110262696

result:

ok single line: '22110262696'

Test #4:

score: 7
Accepted
time: 1ms
memory: 3824kb

input:

1 89
1 82978004
1 725546925
1 824854855
1 134535238
1 182588700
1 121005200
1 274161085
1 287432242
1 765301085
1 762002231
1 14627523
1 160221425
1 835613649
1 891489782
1 963032877
1 232634649
1 551893622
1 78012995
1 298144433
1 402762010
1 455684005
1 921659880
1 244327108
1 567279640
1 40490786...

output:

22880340918

result:

ok single line: '22880340918'

Test #5:

score: 7
Accepted
time: 1ms
memory: 3800kb

input:

1 84
1 887742276
1 580594619
1 950555150
1 771426100
1 465681669
1 469566475
1 651054119
1 538349774
1 623278516
1 795673458
1 647508726
1 643240398
1 182322569
1 832568327
1 160387299
1 114691531
1 329968342
1 620479904
1 652768604
1 98506590
1 198564634
1 538477498
1 754322865
1 94590693
1 9102737...

output:

17356166508

result:

ok single line: '17356166508'

Test #6:

score: 7
Accepted
time: 1ms
memory: 3808kb

input:

1 91
1 162454772
1 470082830
1 143610614
1 831070014
1 146983411
1 789045826
1 82582863
1 652480352
1 119027154
1 612678289
1 544551347
1 937361107
1 925273426
1 143655813
1 9085899
1 588299483
1 859313272
1 294149571
1 735356175
1 55633143
1 366991221
1 687012588
1 773929825
1 737277036
1 229399700...

output:

22730104544

result:

ok single line: '22730104544'

Test #7:

score: 7
Accepted
time: 1ms
memory: 3828kb

input:

1 98
1 492984845
1 603301504
1 827779013
1 543424271
1 353910948
1 498019066
1 188352400
1 421972590
1 541331626
1 790810535
1 384601943
1 691547921
1 617300120
1 647962561
1 650218429
1 425228920
1 274801429
1 550676594
1 641040777
1 380212377
1 634255275
1 713589024
1 320089450
1 691185086
1 94179...

output:

22934390057

result:

ok single line: '22934390057'

Test #8:

score: 7
Accepted
time: 1ms
memory: 3804kb

input:

1 96
1 676422399
1 383144101
1 908806402
1 195899706
1 858455939
1 680490408
1 612812422
1 205243456
1 937363104
1 467111199
1 445555414
1 213343027
1 415956505
1 963661454
1 937156337
1 556569608
1 344720216
1 835918329
1 893856969
1 184325381
1 53110184
1 524825668
1 356433261
1 686051960
1 263022...

output:

22860762666

result:

ok single line: '22860762666'

Test #9:

score: 7
Accepted
time: 1ms
memory: 3672kb

input:

1 87
1 411199713
1 407170309
1 411199713
1 690858790
1 411199713
1 411199713
1 411199713
1 407170309
1 973231939
1 108775390
1 108775390
1 973231939
1 973231939
1 973231939
1 176781105
1 690858790
1 407170309
1 176781105
1 411199713
1 176781105
1 493892183
1 411199713
1 493892183
1 411199713
1 69085...

output:

15512296902

result:

ok single line: '15512296902'

Test #10:

score: 7
Accepted
time: 1ms
memory: 3828kb

input:

1 95
1 556207110
1 813583629
1 263051757
1 263051757
1 556207110
1 263051757
1 813583629
1 263051757
1 813583629
1 263051757
1 813583629
1 813583629
1 556207110
1 263051757
1 263051757
1 556207110
1 556207110
1 813583629
1 813583629
1 263051757
1 813583629
1 556207110
1 556207110
1 813583629
1 81358...

output:

18060215274

result:

ok single line: '18060215274'

Subtask #2:

score: 19
Accepted

Test #11:

score: 19
Accepted
time: 1ms
memory: 3824kb

input:

6 7
1 65
1 115
2 33
1 298
1 42
3 33
4 6
1 180
5 2
6 126
5 2
2 177

output:

302

result:

ok single line: '302'

Test #12:

score: 19
Accepted
time: 1ms
memory: 3764kb

input:

13 23
1 48
2 167
1 248
2 48
3 61
1 194
1 126
4 33
4 47
5 138
1 262
1 299
6 24
6 7
4 18
6 7
7 49
8 53
4 11
9 3
10 2
4 41
11 39
6 5
12 2
2 172
5 123
10 4
8 63
6 5
13 1
4 47
13 1
13 1
13 1

output:

374

result:

ok single line: '374'

Test #13:

score: 19
Accepted
time: 1ms
memory: 4092kb

input:

21 38
1 24
1 32
2 177
3 203
4 47
3 59
5 5
1 1
6 48
1 181
7 92
7 68
8 1
4 8
9 106
10 1
11 22
12 108
13 6
13 5
4 61
1 11
14 6
12 52
15 35
16 55
2 10
17 1
7 100
8 56
10 3
2 106
17 1
18 1
11 104
8 2
13 116
19 5
10 3
9 195
6 4
9 100
20 128
9 243
13 110
3 82
4 96
8 46
13 31
11 77
7 22
21 124
9 287
7 169
3...

output:

1917

result:

ok single line: '1917'

Test #14:

score: 19
Accepted
time: 1ms
memory: 3676kb

input:

33 38
1 18
1 81
2 100
1 245
3 102
4 5
2 204
5 26
6 37
7 10
8 41
6 52
7 100
4 71
9 25
3 172
10 61
11 144
12 33
9 9
9 17
11 46
13 15
5 17
14 20
15 87
14 73
2 223
8 41
1 283
11 96
16 1
17 4
18 5
19 7
9 17
20 4
17 31
8 2
13 58
21 9
22 2
23 121
7 102
24 50
9 2
25 7
26 13
27 14
28 4
29 45
16 3
20 2
1 219
...

output:

804

result:

ok single line: '804'

Test #15:

score: 19
Accepted
time: 1ms
memory: 3868kb

input:

93 1
1 2
2 2
3 1
4 1
5 2
6 2
7 2
8 1
9 1
10 1
11 2
12 2
13 1
14 2
15 2
16 2
17 1
18 1
19 1
20 1
21 2
22 2
23 1
24 1
25 2
26 1
27 2
28 2
29 1
30 1
31 1
32 2
33 1
34 2
35 2
36 2
37 1
38 1
39 2
40 1
41 1
42 1
43 1
44 2
45 2
46 2
47 1
48 1
49 1
50 2
51 2
52 2
53 2
54 1
55 1
56 2
57 1
58 2
59 1
60 1
61 2...

output:

0

result:

ok single line: '0'

Test #16:

score: 19
Accepted
time: 1ms
memory: 3868kb

input:

55 62
1 118
2 144
1 264
1 113
1 5
3 27
4 12
5 68
2 21
6 44
7 7
8 15
9 80
2 65
8 3
5 32
10 151
11 137
12 1
2 153
13 5
8 15
7 5
14 24
15 70
1 73
16 13
17 122
5 135
10 131
13 8
4 31
18 5
3 17
19 52
14 6
11 243
20 1
21 10
9 42
14 24
22 1
23 5
3 37
14 33
24 1
25 1
3 26
26 30
9 59
22 2
27 43
24 5
4 33
28 ...

output:

1219

result:

ok single line: '1219'

Test #17:

score: 19
Accepted
time: 1ms
memory: 3916kb

input:

63 67
1 121
2 106
1 147
3 4
4 87
3 36
5 52
6 21
7 21
2 169
8 8
2 165
2 46
7 33
8 13
9 34
10 7
4 119
3 43
1 225
1 2
11 5
12 4
1 137
1 215
13 12
14 1
15 2
3 55
16 3
14 95
17 3
12 7
18 6
19 1
20 12
19 29
21 53
22 71
8 10
5 24
8 4
23 3
24 1
25 100
11 9
12 8
4 60
4 36
14 47
26 44
9 22
27 1
25 76
13 3
28 ...

output:

934

result:

ok single line: '934'

Test #18:

score: 19
Accepted
time: 1ms
memory: 4124kb

input:

66 88
1 246
2 6
3 2
4 21
2 34
2 40
5 10
6 2
5 11
7 3
1 231
8 1
8 11
4 25
4 42
1 253
9 5
4 30
10 1
11 3
12 20
12 9
13 3
7 12
14 2
15 10
16 1
17 44
6 17
1 6
18 5
4 29
19 7
1 252
20 6
21 1
22 30
23 12
20 11
24 7
25 1
8 9
14 2
26 1
20 7
27 1
28 2
29 2
30 2
31 292
21 3
21 1
32 3
15 14
33 3
28 1
34 8
25 1...

output:

594

result:

ok single line: '594'

Test #19:

score: 19
Accepted
time: 1ms
memory: 4096kb

input:

78 99
1 204
1 105
1 137
2 3
3 125
4 21
5 65
6 39
7 41
2 41
8 6
6 14
9 17
10 46
4 44
11 20
1 183
4 103
12 13
13 18
14 9
13 17
3 37
15 1
16 4
17 9
18 78
2 56
11 39
19 8
20 2
21 29
13 12
22 3
2 64
1 125
23 32
24 119
5 89
9 27
25 39
26 16
27 7
28 8
29 3
29 19
7 30
30 4
16 112
14 12
22 1
31 51
32 3
33 3
...

output:

1443

result:

ok single line: '1443'

Test #20:

score: 19
Accepted
time: 0ms
memory: 3840kb

input:

89 101
1 120
2 60
1 229
1 156
3 32
4 44
5 105
2 85
2 53
6 11
7 7
4 62
8 15
3 24
9 32
10 94
11 23
12 4
13 5
14 7
10 82
3 98
5 44
15 28
16 43
1 146
16 5
17 25
9 59
17 16
2 118
17 31
18 47
19 10
20 2
8 34
21 8
22 3
23 3
24 65
25 34
26 14
27 61
10 75
2 175
3 89
6 71
28 9
29 3
30 4
31 10
32 16
33 1
34 2
...

output:

1341

result:

ok single line: '1341'

Test #21:

score: 19
Accepted
time: 1ms
memory: 4148kb

input:

101 112
1 172
2 49
3 19
4 3
1 86
1 217
5 28
1 141
6 160
7 35
8 9
9 25
9 109
9 36
6 103
10 37
3 56
11 31
7 58
12 12
2 99
13 88
5 3
14 6
11 39
15 84
16 19
13 106
9 100
17 12
10 41
18 1
17 7
19 10
20 16
21 5
22 16
23 23
1 222
24 33
25 30
26 3
27 7
28 14
29 14
20 20
30 32
2 43
30 40
31 3
23 32
3 17
2 93...

output:

939

result:

ok single line: '939'

Test #22:

score: 19
Accepted
time: 1ms
memory: 3880kb

input:

109 126
1 111
2 115
3 33
1 55
4 26
5 110
6 9
1 188
5 125
7 21
7 102
2 42
2 49
8 1
9 20
10 36
11 19
12 11
4 6
5 154
5 97
7 123
13 5
7 106
14 130
3 51
3 43
6 4
15 2
4 31
11 13
16 2
17 33
18 90
19 6
5 128
7 131
14 87
20 23
21 21
22 25
21 79
10 45
3 25
23 2
24 132
14 77
25 25
22 145
14 18
26 1
27 21
4 3...

output:

1725

result:

ok single line: '1725'

Test #23:

score: 19
Accepted
time: 1ms
memory: 3852kb

input:

117 141
1 107
1 44
2 46
3 99
4 37
5 127
6 46
7 1
1 210
8 59
1 175
1 68
5 56
6 28
9 13
3 86
10 66
11 1
12 38
13 228
12 94
13 180
12 38
8 22
14 1
15 64
16 11
13 159
17 112
18 21
17 47
7 15
19 1
11 1
18 11
20 67
21 2
22 10
23 38
12 53
24 19
25 1
15 34
26 20
27 6
28 1
29 28
30 1
3 204
7 27
10 58
21 1
31...

output:

1931

result:

ok single line: '1931'

Test #24:

score: 19
Accepted
time: 1ms
memory: 3920kb

input:

134 137
1 232
2 11
1 91
1 60
3 47
4 156
5 192
5 103
6 2
1 200
7 16
5 2
8 15
9 26
10 2
11 41
12 23
4 27
9 64
13 131
14 21
2 2
15 92
4 140
16 1
17 50
5 151
18 4
11 17
19 99
20 50
1 155
19 139
21 53
1 188
17 3
22 5
23 31
24 11
25 42
12 22
26 2
23 46
27 1
15 61
10 4
28 69
29 7
26 2
30 57
31 63
32 14
33 ...

output:

1934

result:

ok single line: '1934'

Test #25:

score: 19
Accepted
time: 0ms
memory: 4076kb

input:

293 1
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
11 1
12 1
13 1
14 1
15 1
16 1
17 1
18 1
19 1
20 1
21 1
22 1
23 1
24 1
25 1
26 1
27 1
28 1
29 1
30 1
31 1
32 1
33 1
34 1
35 1
36 1
37 1
38 1
39 1
40 1
41 1
42 1
43 1
44 1
45 1
46 1
47 1
48 1
49 1
50 1
51 1
52 1
53 1
54 1
55 1
56 1
57 1
58 1
59 1
60 1
61 ...

output:

0

result:

ok single line: '0'

Test #26:

score: 19
Accepted
time: 1ms
memory: 3804kb

input:

139 161
1 108
2 10
2 10
3 51
1 274
4 101
5 3
1 26
6 13
6 2
7 21
4 129
1 253
8 7
2 116
9 98
10 5
11 9
12 28
4 9
13 34
14 12
15 64
16 21
17 86
18 4
2 21
5 25
19 3
16 9
20 3
2 178
12 4
21 104
7 29
22 8
15 90
23 2
15 46
17 169
24 19
25 50
26 12
13 22
20 22
27 1
28 152
9 196
5 95
29 25
30 9
31 57
32 11
3...

output:

2627

result:

ok single line: '2627'

Test #27:

score: 19
Accepted
time: 1ms
memory: 3804kb

input:

130 170
1 166
2 66
1 9
1 94
1 133
3 2
4 244
5 100
6 127
7 20
8 6
6 85
9 65
4 223
10 14
6 142
11 16
8 4
9 4
5 83
1 279
8 23
6 139
12 11
13 50
12 34
9 43
14 31
15 47
16 8
3 62
14 7
17 5
12 38
18 26
12 38
19 19
7 11
17 19
20 68
21 82
16 3
22 19
9 99
23 3
9 5
24 6
25 13
25 27
14 4
26 20
27 3
28 8
17 22
...

output:

1683

result:

ok single line: '1683'

Test #28:

score: 19
Accepted
time: 0ms
memory: 4140kb

input:

137 163
1 14
2 97
3 91
1 91
4 28
5 89
3 55
6 47
1 159
7 33
5 116
8 19
9 2
10 103
11 78
1 170
1 261
12 10
13 43
1 41
14 8
15 5
10 14
16 5
17 82
18 3
17 99
19 75
20 43
21 140
18 10
7 16
18 19
22 2
15 22
22 2
8 2
16 1
18 4
12 16
23 23
24 10
25 1
26 10
27 29
28 12
29 5
1 235
30 11
31 50
32 19
33 51
34 3...

output:

1711

result:

ok single line: '1711'

Test #29:

score: 19
Accepted
time: 1ms
memory: 3972kb

input:

140 160
1 116
2 62
1 228
3 54
4 14
3 112
3 33
5 14
6 23
1 44
7 2
2 124
2 7
2 9
8 76
9 9
10 4
11 240
11 7
2 69
12 1
1 123
13 34
14 119
15 36
16 3
17 16
5 7
18 7
19 2
20 94
21 5
22 1
8 30
18 22
23 37
24 11
19 7
3 22
25 4
26 79
27 3
8 55
22 1
5 54
28 7
29 13
1 51
30 4
11 22
31 6
32 64
7 6
12 4
33 51
34...

output:

2102

result:

ok single line: '2102'

Test #30:

score: 19
Accepted
time: 1ms
memory: 3760kb

input:

121 179
1 16
1 66
2 7
3 69
4 14
1 273
5 64
6 71
1 121
7 9
8 65
7 4
9 150
10 75
11 3
2 277
5 52
12 10
13 2
8 87
8 27
14 22
15 7
11 5
9 104
16 4
17 3
12 5
18 107
19 13
5 28
1 107
2 164
20 15
21 7
16 7
22 15
23 2
17 5
12 29
22 34
6 186
24 54
25 9
7 25
26 28
27 3
28 1
29 28
11 13
30 3
31 3
30 2
1 112
5 ...

output:

2300

result:

ok single line: '2300'

Subtask #3:

score: 29
Accepted

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Test #31:

score: 29
Accepted
time: 1ms
memory: 3952kb

input:

301 162
1 920419098
2 13883963
3 922640814
4 576832601
5 458005774
6 627831463
7 637684083
8 363092299
9 608472070
10 993759086
11 191942487
12 177579899
11 778884458
13 475677264
14 618356404
15 782795435
16 407631159
17 814281326
18 753827889
19 190238739
20 344692223
21 212165514
22 256037358
23 ...

output:

132811066618

result:

ok single line: '132811066618'

Test #32:

score: 29
Accepted
time: 0ms
memory: 4280kb

input:

717 419
1 376688158
2 38218218
3 741729439
4 690180775
5 143289985
6 639629931
7 279619166
8 81401288
9 360313680
10 194769290
11 24761187
12 251418858
13 152297565
14 472298949
15 441611798
16 961909797
17 565923038
18 624777680
19 854783150
20 459946195
21 786187234
22 504262463
23 604133263
24 15...

output:

346801924243

result:

ok single line: '346801924243'

Test #33:

score: 29
Accepted
time: 4ms
memory: 4304kb

input:

1039 570
1 763342664
2 630794
3 305459828
4 879255848
5 900380412
6 599712338
7 339885175
8 83275249
9 352596598
10 198141694
11 762445358
12 923992448
13 47801521
14 424346913
4 567981821
15 772530456
16 26119978
17 256886608
18 329480410
19 568425276
20 833205645
21 421390104
22 562202794
23 22678...

output:

507266242467

result:

ok single line: '507266242467'

Test #34:

score: 29
Accepted
time: 5ms
memory: 4224kb

input:

1426 825
1 21845067
2 830363113
3 417705301
4 289927464
5 437535695
6 751469381
7 546832677
8 845762062
9 308121229
10 519035372
11 930131981
12 76264594
13 334316135
14 410847902
15 84600782
16 64577703
17 769816534
18 923402308
19 471587430
20 857634912
21 18912000
22 613450475
23 316973077
24 709...

output:

697458913436

result:

ok single line: '697458913436'

Test #35:

score: 29
Accepted
time: 3ms
memory: 4516kb

input:

1870 1054
1 398066557
2 678052759
3 670475186
4 861116608
5 773321138
6 937239049
7 953430280
8 657951645
9 426251998
10 735428487
11 481848196
12 346087245
8 911283015
13 993057922
14 591673939
15 817915837
16 984331303
17 486190973
18 10049554
19 642301159
20 894607734
21 438105131
22 913310885
23...

output:

905295828241

result:

ok single line: '905295828241'

Test #36:

score: 29
Accepted
time: 10ms
memory: 4608kb

input:

2275 1322
1 418164933
2 775647573
3 736430086
4 987537454
5 209486744
6 17546
7 469060248
8 394145046
9 251495602
10 187675560
11 601481752
12 391420587
13 784661212
14 26939760
15 147656809
16 42133747
17 436410803
18 283704828
19 754349602
20 816052547
21 831053284
22 568655247
23 137154090
24 255...

output:

1095328057433

result:

ok single line: '1095328057433'

Test #37:

score: 29
Accepted
time: 11ms
memory: 4780kb

input:

2587 1483
1 499164205
2 221727844
3 930497471
4 520918543
5 208865162
6 121777946
7 375261020
8 979185681
9 368487058
10 176303026
11 429699232
12 199269283
13 556946849
14 464979024
15 539431406
16 788299689
17 778688657
18 471605859
19 282547751
20 555232934
21 886757378
22 522088193
23 610918707
...

output:

1297800304292

result:

ok single line: '1297800304292'

Test #38:

score: 29
Accepted
time: 11ms
memory: 5076kb

input:

2987 1757
1 82381521
2 474824518
3 920067780
4 425601747
5 635345786
6 51606401
7 734314641
8 414123511
9 583010473
10 359136643
11 836886762
12 215412429
13 196470367
14 701930966
15 385076961
16 633601874
17 268928317
18 384687131
19 761766761
20 806090212
21 774808549
22 961943909
23 154108049
24...

output:

1488454586050

result:

ok single line: '1488454586050'

Test #39:

score: 29
Accepted
time: 20ms
memory: 5308kb

input:

3123 1877
1 831706841
2 781051540
3 70423592
4 648655398
5 158458556
6 611149549
7 396966782
8 617480824
9 214989801
10 294061539
11 684337379
12 110708324
13 221076634
14 474967492
15 372121526
16 470471163
17 385672322
18 94757906
19 547022828
20 567943653
21 444077661
22 188282505
23 509056361
24...

output:

1541144647994

result:

ok single line: '1541144647994'

Test #40:

score: 29
Accepted
time: 12ms
memory: 5168kb

input:

3151 1849
1 324493214
2 131808411
3 293261515
4 355022995
5 29873362
6 599664564
7 835662356
8 933482072
9 541691957
10 873329218
11 410787654
12 303482132
13 284242771
14 484832970
15 858674624
16 880571806
17 574083015
18 467202365
19 329656609
20 2293338
21 772198634
22 361183818
5 298303587
23 4...

output:

1541693936031

result:

ok single line: '1541693936031'

Test #41:

score: 29
Accepted
time: 173ms
memory: 8864kb

input:

4999 1
1 999993036
2 999994800
3 999993285
4 999997650
5 999997940
6 999990140
7 999993201
8 999999276
9 999999071
10 999993743
11 999998318
12 999991514
13 999995090
14 999999301
15 999995132
16 999994465
17 999992463
18 999998028
19 999992382
20 999999010
21 999991716
22 999993591
23 999993053
24 ...

output:

0

result:

ok single line: '0'

Test #42:

score: 29
Accepted
time: 171ms
memory: 9160kb

input:

4998 2
1 999998194
2 999996844
3 999993710
4 999992665
5 999992557
6 999992584
7 999993134
8 999999644
9 999994554
10 999993257
11 999994252
12 999999337
13 999996816
14 999994751
15 999992337
16 999996042
17 999998574
18 999995011
19 999991262
20 999992554
21 999998979
22 999996349
23 999998149
24 ...

output:

4997974986577

result:

ok single line: '4997974986577'

Test #43:

score: 29
Accepted
time: 9ms
memory: 5120kb

input:

4900 100
1 999991561
2 999991023
1 999999431
3 999996825
4 999999376
1 999992285
5 999998086
6 999997485
7 999990084
1 999991950
8 999993135
9 999996894
10 999998611
11 999995713
1 999993078
12 999990674
13 999993528
14 999995213
15 999995084
16 999999507
1 999999999
17 999996140
18 999994248
19 999...

output:

2450987727801

result:

ok single line: '2450987727801'

Test #44:

score: 29
Accepted
time: 56ms
memory: 6808kb

input:

2499 2500
1 439460934
2 783025325
3 559459286
4 10655632
5 754555467
6 175379690
7 267810515
8 826564855
9 514616538
10 432833129
11 586659275
12 537666212
13 587511202
14 867749449
15 442072911
16 792455522
17 430082776
18 15270738
19 669713751
20 972536273
21 253824811
22 875736843
23 963067186
24...

output:

1873824603983

result:

ok single line: '1873824603983'

Test #45:

score: 29
Accepted
time: 52ms
memory: 6508kb

input:

2364 2635
1 800243976
2 560941019
3 611820188
4 110334476
5 653585207
6 73255039
7 55721869
8 492675013
9 672841239
10 558274347
11 695906535
12 19336760
13 475895478
14 320393876
15 784637116
16 973989669
17 665711154
18 786318273
19 779795065
20 54345337
21 423971980
22 828924854
23 486328381
24 3...

output:

1664606780993

result:

ok single line: '1664606780993'

Test #46:

score: 29
Accepted
time: 47ms
memory: 6344kb

input:

2396 2603
1 143164022
2 651107986
3 793461024
4 519935252
5 707437122
6 796625886
7 758986319
8 279582210
9 552992089
10 904220500
11 772924575
12 555594519
13 753901465
14 541269303
15 629634339
16 589254516
17 344548855
18 517628181
19 658109611
20 838928925
21 136407119
22 252769354
23 68158164
2...

output:

1661418840499

result:

ok single line: '1661418840499'

Test #47:

score: 29
Accepted
time: 5ms
memory: 5076kb

input:

53 4943
1 108701533
1 762016066
2 158864139
2 229999782
2 715410397
2 163820409
2 966678446
2 613380142
2 945872922
2 704482790
2 204038648
2 596548544
3 372743765
3 162866106
3 851451963
3 657877743
3 536191158
3 182343668
3 132574004
1 322562600
1 171055898
1 639777706
1 152989795
1 706582237
1 51...

output:

1247558162340

result:

ok single line: '1247558162340'

Test #48:

score: 29
Accepted
time: 5ms
memory: 5040kb

input:

48 4911
1 614868110
2 916120791
2 285591885
2 743652924
2 845514194
2 580273340
2 464847323
2 423348016
2 413536741
2 457069744
2 667830491
2 233015744
2 627828401
2 526677681
2 818440202
2 901083373
2 447934950
2 126282434
2 765771761
2 273471556
2 879518874
2 968316988
2 611124813
2 398175119
2 68...

output:

1234131077649

result:

ok single line: '1234131077649'

Test #49:

score: 29
Accepted
time: 5ms
memory: 5176kb

input:

5 4504
1 890856182
1 78084000
1 16801616
1 466720154
2 589876776
2 807786152
2 719506874
2 160993093
2 76194773
2 535428398
2 157769551
2 206652352
2 129941210
2 917191586
2 567805164
2 257949749
2 836704375
2 656249964
2 458611620
2 976148277
2 259862819
2 312525943
2 925359719
2 816243053
2 614506...

output:

1102143195362

result:

ok single line: '1102143195362'

Test #50:

score: 29
Accepted
time: 2ms
memory: 5392kb

input:

6 4994
1 456281990
1 365849799
1 791098247
1 586851273
1 318572198
2 433461526
2 813265522
2 34581226
2 340305299
2 177232589
2 289532934
2 164014007
2 504775473
2 537364884
2 967517769
2 67477974
2 180646637
2 512956835
2 49621198
2 210136320
2 246610136
2 997359957
2 391536302
2 801647537
2 398463...

output:

1233299707784

result:

ok single line: '1233299707784'

Test #51:

score: 29
Accepted
time: 0ms
memory: 4996kb

input:

2 4275
1 170424242
2 157296367
2 45154595
2 278995331
2 724266140
2 369993724
2 280188774
2 601418859
2 343732082
2 97347826
2 467160686
2 24041524
2 178804849
2 198259179
2 533348580
2 581895147
2 924498865
2 732721030
2 360402368
2 785752880
2 121075840
2 765506692
2 937671044
2 854745655
2 818916...

output:

1063932478026

result:

ok single line: '1063932478026'

Test #52:

score: 29
Accepted
time: 5ms
memory: 4968kb

input:

2 4163
1 790277286
2 763894393
2 322193685
2 724891720
2 477186471
2 130318815
2 448598387
2 934582586
2 755424724
2 408157030
2 383717615
2 361736424
2 885848767
2 415478506
2 433214016
2 249463387
2 86227659
2 381128704
2 180941538
2 230260809
2 313401061
2 874038533
2 781675685
2 339357136
2 1614...

output:

1034079385209

result:

ok single line: '1034079385209'

Test #53:

score: 29
Accepted
time: 4ms
memory: 5280kb

input:

110 4388
1 129329989
2 290770655
3 399085375
4 480394263
5 998084599
6 601669068
7 976030367
7 654350436
8 380191293
9 128462385
10 435930271
11 617820729
8 509826083
12 773630320
13 29155358
14 40756873
15 676289561
16 597719328
17 853617983
18 727413460
18 728104236
19 517523345
20 136755862
12 54...

output:

1127480276183

result:

ok single line: '1127480276183'

Test #54:

score: 29
Accepted
time: 6ms
memory: 5048kb

input:

133 4533
1 981373360
2 950241053
2 691321246
2 34700278
2 304373714
3 769268909
3 524168686
4 230905187
4 758609460
5 799069986
5 987124992
6 894207766
6 412045712
2 987074888
7 576433866
7 16062561
7 66876089
7 399361925
7 143989268
7 576793183
7 604689553
7 152226793
7 313746254
7 728803328
7 5207...

output:

1141003981040

result:

ok single line: '1141003981040'

Test #55:

score: 29
Accepted
time: 5ms
memory: 5880kb

input:

1 4999
1 295391035
1 196472221
1 340660725
1 715185782
1 936519692
1 307450161
1 364305579
1 609135879
1 14555582
1 691886765
1 96885367
1 666972226
1 696216099
1 886834387
1 705696297
1 241496338
1 760998611
1 221849757
1 169070630
1 392736300
1 443509648
1 42218117
1 555255985
1 844348180
1 301198...

output:

1243420232820

result:

ok single line: '1243420232820'

Subtask #4:

score: 0
Time Limit Exceeded

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Test #56:

score: 45
Accepted
time: 40ms
memory: 7788kb

input:

8275 4844
1 107176958
2 220278740
3 824198376
4 659292090
5 583192157
6 160643819
7 557329363
8 260681680
9 496275861
10 858165732
11 732553326
12 772560828
13 783345064
14 711374681
15 492845246
16 860862812
17 196965386
18 820776112
19 455804299
20 350615945
21 278193443
22 837935011
23 828959959
...

output:

4075544302733

result:

ok single line: '4075544302733'

Test #57:

score: 45
Accepted
time: 399ms
memory: 19184kb

input:

31089 18103
1 220776148
2 692381056
3 346957199
4 713647714
5 714759568
6 924734592
7 659276882
8 851637822
9 448371335
10 809574888
11 340376373
12 726397625
13 330389128
14 815108721
15 739332880
16 680592803
17 89540638
18 865581855
19 124042339
20 134606221
21 882819008
22 659695235
23 979295548...

output:

15494390766791

result:

ok single line: '15494390766791'

Test #58:

score: 45
Accepted
time: 875ms
memory: 30544kb

input:

53299 30967
1 855240498
2 154546935
3 234034227
4 308783540
5 567404088
6 936699401
7 175478476
8 275564115
9 593725435
10 709911797
11 183871063
12 165826746
13 216306032
14 856542036
15 584479452
16 720403214
17 830100662
18 615363483
19 983834172
20 524083998
21 210643476
22 406935995
23 37216820...

output:

26365637274114

result:

ok single line: '26365637274114'

Test #59:

score: 45
Accepted
time: 980ms
memory: 37484kb

input:

67684 39423
1 255518383
2 383661588
3 4810446
4 525628041
5 379438718
6 49421468
7 213128275
8 125716504
9 857695694
10 624656565
11 772321426
12 201754727
13 254786355
14 942071029
15 273736495
16 102807767
17 551341975
18 345946666
19 621677927
20 583679679
21 993753318
22 183768877
23 538570621
2...

output:

33302307710368

result:

ok single line: '33302307710368'

Test #60:

score: 0
Time Limit Exceeded

input:

90485 52695
1 805472417
2 142689110
3 189995996
4 810067756
5 661480709
6 278044642
7 97014836
8 386959386
9 977471172
10 22636387
11 573815741
12 694548433
13 125152039
14 225054605
15 299499113
16 479863003
17 640033253
18 282736250
19 245410175
20 105257314
21 11870447
22 801974433
23 213321971
2...

output:


result: