QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#852758#9732. Gathering Mushroomsucup-team087#AC ✓212ms71560kbC++2338.6kb2025-01-11 13:50:192025-01-11 13:50:20

Judging History

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

  • [2025-01-11 13:50:20]
  • 评测
  • 测评结果:AC
  • 用时:212ms
  • 内存:71560kb
  • [2025-01-11 13:50:19]
  • 提交

answer

#line 1 "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 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_sgn(int x) { return (__builtin_parity(unsigned(x)) & 1 ? -1 : 1); }
int popcnt_sgn(u32 x) { return (__builtin_parity(x) & 1 ? -1 : 1); }
int popcnt_sgn(ll x) { return (__builtin_parityll(x) & 1 ? -1 : 1); }
int popcnt_sgn(u64 x) { return (__builtin_parityll(x) & 1 ? -1 : 1); }
// (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 kth_bit(int k) {
  return T(1) << k;
}
template <typename T>
bool has_kth_bit(T x, int k) {
  return x >> k & 1;
}

template <typename UINT>
struct all_bit {
  struct iter {
    UINT s;
    iter(UINT s) : s(s) {}
    int operator*() const { return lowbit(s); }
    iter &operator++() {
      s &= s - 1;
      return *this;
    }
    bool operator!=(const iter) const { return s != 0; }
  };
  UINT s;
  all_bit(UINT s) : s(s) {}
  iter begin() const { return iter(s); }
  iter end() const { return iter(0); }
};

template <typename UINT>
struct all_subset {
  static_assert(is_unsigned<UINT>::value);
  struct iter {
    UINT s, t;
    bool ed;
    iter(UINT s) : s(s), t(s), ed(0) {}
    int operator*() const { return s ^ t; }
    iter &operator++() {
      (t == 0 ? ed = 1 : t = (t - 1) & s);
      return *this;
    }
    bool operator!=(const iter) const { return !ed; }
  };
  UINT s;
  all_subset(UINT s) : s(s) {}
  iter begin() const { return iter(s); }
  iter end() const { return iter(0); }
};

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 "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); }
void YA(bool t = 1) { print(t ? "YA" : "TIDAK"); }
void TIDAK(bool t = 1) { YES(!t); }
#line 3 "main.cpp"

#line 2 "library/graph/tree.hpp"

#line 2 "library/ds/hashmap.hpp"

// u64 -> Val
template <typename Val>
struct HashMap {
  // n は入れたいものの個数で ok
  HashMap(u32 n = 0) { build(n); }
  void build(u32 n) {
    u32 k = 8;
    while (k < n * 2) k *= 2;
    cap = k / 2, mask = k - 1;
    key.resize(k), val.resize(k), used.assign(k, 0);
  }

  // size を保ったまま. size=0 にするときは build すること.
  void clear() {
    used.assign(len(used), 0);
    cap = (mask + 1) / 2;
  }
  int size() { return len(used) / 2 - cap; }

  int index(const u64& k) {
    int i = 0;
    for (i = hash(k); used[i] && key[i] != k; i = (i + 1) & mask) {}
    return i;
  }

  Val& operator[](const u64& k) {
    if (cap == 0) extend();
    int i = index(k);
    if (!used[i]) { used[i] = 1, key[i] = k, val[i] = Val{}, --cap; }
    return val[i];
  }

  Val get(const u64& k, Val default_value) {
    int i = index(k);
    return (used[i] ? val[i] : default_value);
  }

  bool count(const u64& k) {
    int i = index(k);
    return used[i] && key[i] == k;
  }

  // f(key, val)
  template <typename F>
  void enumerate_all(F f) {
    FOR(i, len(used)) if (used[i]) f(key[i], val[i]);
  }

private:
  u32 cap, mask;
  vc<u64> key;
  vc<Val> val;
  vc<bool> used;

  u64 hash(u64 x) {
    static const u64 FIXED_RANDOM = std::chrono::steady_clock::now().time_since_epoch().count();
    x += FIXED_RANDOM;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return (x ^ (x >> 31)) & mask;
  }

  void extend() {
    vc<pair<u64, Val>> dat;
    dat.reserve(len(used) / 2 - cap);
    FOR(i, len(used)) {
      if (used[i]) dat.eb(key[i], val[i]);
    }
    build(2 * len(dat));
    for (auto& [a, b]: dat) (*this)[a] = b;
  }
};
#line 3 "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;
  }

  HashMap<int> MP_FOR_EID;

  int get_eid(u64 a, u64 b) {
    if (len(MP_FOR_EID) == 0) {
      MP_FOR_EID.build(N - 1);
      for (auto& e: edges) {
        u64 a = e.frm, b = e.to;
        u64 k = to_eid_key(a, b);
        MP_FOR_EID[k] = e.id;
      }
    }
    return MP_FOR_EID.get(to_eid_key(a, b), -1);
  }

  u64 to_eid_key(u64 a, u64 b) {
    if (!directed && a > b) swap(a, b);
    return N * a + b;
  }

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 "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};
  }

  // uv path 上で check(v) を満たす最後の v
  // なければ (つまり check(v) が ng )-1
  template <class F>
  int max_path(F check, int u, int v) {
    if (!check(u)) return -1;
    auto pd = get_path_decomposition(u, v, false);
    for (auto [a, b]: pd) {
      if (!check(V[a])) return u;
      if (check(V[b])) {
        u = V[b];
        continue;
      }
      int c = binary_search([&](int c) -> bool { return check(V[c]); }, a, b, 0);
      return V[c];
    }
    return u;
  }
};
#line 2 "library/ds/unionfind/unionfind.hpp"

struct UnionFind {
  int n, n_comp;
  vc<int> dat; // par or (-size)
  UnionFind(int n = 0) { build(n); }

  void build(int m) {
    n = m, n_comp = m;
    dat.assign(n, -1);
  }

  void reset() { build(n); }

  int operator[](int x) {
    while (dat[x] >= 0) {
      int pp = dat[dat[x]];
      if (pp < 0) { return dat[x]; }
      x = dat[x] = pp;
    }
    return x;
  }

  ll size(int x) {
    x = (*this)[x];
    return -dat[x];
  }

  bool merge(int x, int y) {
    x = (*this)[x], y = (*this)[y];
    if (x == y) return false;
    if (-dat[x] < -dat[y]) swap(x, y);
    dat[x] += dat[y], dat[y] = x, n_comp--;
    return true;
  }

  vc<int> get_all() {
    vc<int> A(n);
    FOR(i, n) A[i] = (*this)[i];
    return A;
  }
};
#line 3 "library/graph/functional.hpp"

// N が根となる木を新たに作る
template <typename T = int>
struct FunctionalGraph {
  int N, M;
  vc<int> TO;
  vc<T> wt;
  vc<int> root;
  Graph<T, 1> G;

  FunctionalGraph() {}
  FunctionalGraph(int N) : N(N), M(0), TO(N, -1), wt(N), root(N, -1) {}

  void add(int a, int b, T c = 1) {
    assert(0 <= a && a < N);
    assert(TO[a] == -1);
    ++M;
    TO[a] = b;
    wt[a] = c;
  }

  pair<Graph<T, 1>, Tree<Graph<T, 1>>> build() {
    assert(N == M);
    UnionFind uf(N);
    FOR(v, N) if (!uf.merge(v, TO[v])) { root[v] = v; }
    FOR(v, N) if (root[v] == v) root[uf[v]] = v;
    FOR(v, N) root[v] = root[uf[v]];

    G.build(N + 1);
    FOR(v, N) {
      if (root[v] == v)
        G.add(N, v, wt[v]);
      else
        G.add(TO[v], v, wt[v]);
    }
    G.build();
    Tree<Graph<T, 1>> tree(G, N);
    return {G, tree};
  }

  // a -> b にかかる回数. 不可能なら infty<int>. O(1).
  template <typename TREE>
  int dist(TREE& tree, int a, int b) {
    if (tree.in_subtree(a, b)) return tree.depth[a] - tree.depth[b];
    int r = root[a];
    int btm = TO[r];
    // a -> r -> btm -> b
    if (tree.in_subtree(btm, b)) {
      int x = tree.depth[a] - tree.depth[r];
      x += 1;
      x += tree.depth[btm] - tree.depth[b];
      return x;
    }
    return infty<int>;
  }

  // functional graph に向かって進む
  template <typename TREE>
  int jump(TREE& tree, int v, ll step) {
    int d = tree.depth[v];
    if (step <= d - 1) return tree.jump(v, N, step);
    v = root[v];
    step -= d - 1;
    int bottom = TO[v];
    int c = tree.depth[bottom];
    step %= c;
    if (step == 0) return v;
    return tree.jump(bottom, N, step - 1);
  }

  // functional graph に step 回進む
  template <typename TREE>
  vc<int> jump_all(TREE& tree, ll step) {
    vc<int> res(N, -1);
    // v の k 個先を res[w] に入れる
    vvc<pair<int, int>> query(N);
    FOR(v, N) {
      int d = tree.depth[v];
      int r = root[v];
      if (d - 1 > step) { query[v].eb(v, step); }
      if (d - 1 <= step) {
        ll k = step - (d - 1);
        int bottom = TO[r];
        int c = tree.depth[bottom];
        k %= c;
        if (k == 0) {
          res[v] = r;
          continue;
        }
        query[bottom].eb(v, k - 1);
      }
    }

    vc<int> path;
    auto dfs = [&](auto& dfs, int v) -> void {
      path.eb(v);
      for (auto&& [w, k]: query[v]) { res[w] = path[len(path) - 1 - k]; }
      for (auto&& e: G[v]) dfs(dfs, e.to);
      path.pop_back();
    };
    for (auto&& e: G[N]) { dfs(dfs, e.to); }
    return res;
  }

  template <typename TREE>
  bool in_cycle(TREE& tree, int v) {
    int r = root[v];
    int bottom = TO[r];
    return tree.in_subtree(bottom, v);
  }

  vc<int> collect_cycle(int r) {
    assert(r == root[r]);
    vc<int> cyc = {TO[r]};
    while (cyc.back() != r) cyc.eb(TO[cyc.back()]);
    return cyc;
  }

  // F^k(i)==F^k(j) となる最小の k OR -1
  template <typename TREE>
  int meet_time(TREE& tree, int i, int j) {
    if (i == j) return 0;
    if (root[i] != root[j]) return -1;
    int r = root[i];
    int b = TO[r];
    int n = tree.depth[b] - tree.depth[r] + 1; // cyc len
    if ((tree.depth[i] - tree.depth[j]) % n != 0) return -1;

    if (tree.depth[i] == tree.depth[j]) {
      int lca = tree.lca(i, j);
      return tree.depth[i] - tree.depth[lca];
    }
    int ti = tree.depth[i] - tree.depth[tree.lca(b, i)];
    int tj = tree.depth[j] - tree.depth[tree.lca(b, j)];
    return max(ti, tj);
  }
};
#line 2 "library/ds/segtree/segtree.hpp"

template <class Monoid>
struct SegTree {
  using MX = Monoid;
  using X = typename MX::value_type;
  using value_type = X;
  vc<X> dat;
  int n, log, size;

  SegTree() {}
  SegTree(int n) { build(n); }
  template <typename F>
  SegTree(int n, F f) {
    build(n, f);
  }
  SegTree(const vc<X>& v) { build(v); }

  void build(int m) {
    build(m, [](int i) -> X { return MX::unit(); });
  }
  void build(const vc<X>& v) {
    build(len(v), [&](int i) -> X { return v[i]; });
  }
  template <typename F>
  void build(int m, F f) {
    n = m, log = 1;
    while ((1 << log) < n) ++log;
    size = 1 << log;
    dat.assign(size << 1, MX::unit());
    FOR(i, n) dat[size + i] = f(i);
    FOR_R(i, 1, size) update(i);
  }

  X get(int i) { return dat[size + i]; }
  vc<X> get_all() { return {dat.begin() + size, dat.begin() + size + n}; }

  void update(int i) { dat[i] = Monoid::op(dat[2 * i], dat[2 * i + 1]); }
  void set(int i, const X& x) {
    assert(i < n);
    dat[i += size] = x;
    while (i >>= 1) update(i);
  }

  void multiply(int i, const X& x) {
    assert(i < n);
    i += size;
    dat[i] = Monoid::op(dat[i], x);
    while (i >>= 1) update(i);
  }

  X prod(int L, int R) {
    assert(0 <= L && L <= R && R <= n);
    X vl = Monoid::unit(), vr = Monoid::unit();
    L += size, R += size;
    while (L < R) {
      if (L & 1) vl = Monoid::op(vl, dat[L++]);
      if (R & 1) vr = Monoid::op(dat[--R], vr);
      L >>= 1, R >>= 1;
    }
    return Monoid::op(vl, vr);
  }

  X prod_all() { return dat[1]; }

  template <class F>
  int max_right(F check, int L) {
    assert(0 <= L && L <= n && check(Monoid::unit()));
    if (L == n) return n;
    L += size;
    X sm = Monoid::unit();
    do {
      while (L % 2 == 0) L >>= 1;
      if (!check(Monoid::op(sm, dat[L]))) {
        while (L < size) {
          L = 2 * L;
          if (check(Monoid::op(sm, dat[L]))) { sm = Monoid::op(sm, dat[L++]); }
        }
        return L - size;
      }
      sm = Monoid::op(sm, dat[L++]);
    } while ((L & -L) != L);
    return n;
  }

  template <class F>
  int min_left(F check, int R) {
    assert(0 <= R && R <= n && check(Monoid::unit()));
    if (R == 0) return 0;
    R += size;
    X sm = Monoid::unit();
    do {
      --R;
      while (R > 1 && (R % 2)) R >>= 1;
      if (!check(Monoid::op(dat[R], sm))) {
        while (R < size) {
          R = 2 * R + 1;
          if (check(Monoid::op(dat[R], sm))) { sm = Monoid::op(dat[R--], sm); }
        }
        return R + 1 - size;
      }
      sm = Monoid::op(dat[R], sm);
    } while ((R & -R) != R);
    return 0;
  }

  // prod_{l<=i<r} A[i xor x]
  X xor_prod(int l, int r, int xor_val) {
    static_assert(Monoid::commute);
    X x = Monoid::unit();
    for (int k = 0; k < log + 1; ++k) {
      if (l >= r) break;
      if (l & 1) { x = Monoid::op(x, dat[(size >> k) + ((l++) ^ xor_val)]); }
      if (r & 1) { x = Monoid::op(x, dat[(size >> k) + ((--r) ^ xor_val)]); }
      l /= 2, r /= 2, xor_val /= 2;
    }
    return x;
  }
};
#line 2 "library/alg/monoid/min_idx.hpp"

template <typename T, bool tie_is_left = true>
struct Monoid_Min_Idx {
  using value_type = pair<T, int>;
  using X = value_type;
  static constexpr bool is_small(const X& x, const X& y) {
    if (x.fi < y.fi) return true;
    if (x.fi > y.fi) return false;
    return (tie_is_left ? (x.se < y.se) : (x.se >= y.se));
  }
  static X op(X x, X y) { return (is_small(x, y) ? x : y); }
  static constexpr X unit() { return {infty<T>, -1}; }
  static constexpr bool commute = true;
};
#line 7 "main.cpp"

void solve() {
  LL(N, K);
  VEC(ll, C, N);
  for (auto& x: C) --x;
  FunctionalGraph<int> FG(N);
  FOR(v, N) {
    INT(w);
    --w;
    SHOW(v, w);
    FG.add(v, w);
  }
  auto [G, tree] = FG.build();

  SegTree<Monoid_Min_Idx<ll>> seg(N);

  vi ANS(N);
  vvc<int> pdat(N); // 色 -> path 上の深さ
  vvc<int> cdat(N); // 色 -> 余分にすすむ回数
  ll cyc_len = 0;
  auto segf = [&](int c) -> pair<ll, int> {
    ll n = len(pdat[c]);
    if (n >= K) {
      ll d = pdat[c][n - K];
      return {1 - d, c};
    }
    ll m = len(cdat[c]);
    if (m == 0) return {infty<ll>, c};
    ll k = K - n;
    auto [q, r] = divmod(k, m);
    if (r == 0) {
      ll ans = cyc_len * (q - 1);
      ans += cdat[c][m - 1];
      return {ans, c};
    }
    SHOW(q, r, cdat[c], cdat[c][r - 1]);
    ll ans = cdat[c][r - 1];
    ans += cyc_len * q;
    return {ans, c};
  };

  FOR(r, N) {
    if (FG.root[r] != r) continue;
    auto cyc = FG.collect_cycle(r);
    cyc_len = len(cyc);
    sort(all(cyc), [&](auto& L, auto& R) -> bool { return tree.depth[L] > tree.depth[R]; });
    FOR(i, len(cyc)) {
      int c = C[cyc[i]];
      cdat[c].eb(1 + i);
      seg.set(c, segf(c));
    }

    auto dfs = [&](auto& dfs, int v) -> void {
      // add
      int c = C[v];
      pdat[c].eb(tree.depth[v]);
      seg.set(c, segf(c));

      // calc ans
      ANS[v] = seg.prod_all().se;

      for (auto& e: G[v]) { dfs(dfs, e.to); }

      pdat[c].pop_back();
      seg.set(c, segf(c));
    };
    dfs(dfs, r);

    FOR(i, len(cyc)) {
      int c = C[cyc[i]];
      cdat[c].clear();
      cdat[c].shrink_to_fit();
      seg.set(c, segf(c));
    }
  }

  ll ans = 0;
  FOR(v, N) { ans += (1 + v) * (1 + ANS[v]); }
  print(ans);
}

signed main() {
  INT(T);
  FOR(T) solve();
  return 0;
}

这程序好像有点Bug,我给组数据试试?

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3696kb

input:

3
5 3
2 2 1 3 3
2 5 1 2 4
5 4
2 2 1 3 3
2 5 1 2 4
3 10
1 2 3
1 3 2

output:

41
45
14

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 40ms
memory: 3956kb

input:

6000
19 48
18 19 18 19 11 9 15 19 12 18 11 18 9 18 9 18 19 11 15
12 14 18 8 1 3 19 5 13 14 15 2 14 5 19 2 19 12 9
15 23
3 1 1 3 6 1 4 1 1 6 6 4 12 4 6
14 1 8 8 6 6 12 14 6 8 5 7 14 2 5
9 140979583
4 5 8 9 2 7 6 8 2
8 9 4 6 9 2 4 7 8
4 976357580
2 3 1 3
2 1 1 4
6 508962809
4 3 4 3 4 4
4 5 4 5 5 6
13 ...

output:

3420
260
254
26
84
759
126
30
1092
1
2493
2422
168
360
298
324
2424
2520
220
228
1107
9
3486
1
796
81
340
272
600
3196
32
495
40
128
140
665
1635
702
68
96
90
288
29
588
16
234
445
2928
140
40
477
1197
19
1994
1082
32
522
672
20
390
32
2204
1938
42
21
885
4
1539
196
420
11
1709
801
720
1
556
40
17
2...

result:

ok 6000 lines

Test #3:

score: 0
Accepted
time: 93ms
memory: 47296kb

input:

1
200000 40000
46988 88148 28442 9596 17281 27561 58024 1062 138912 175273 194682 27958 11240 187099 28869 177531 154933 83035 11300 178646 6952 44234 168671 169483 187602 178519 99885 98196 64731 100802 16974 85402 50616 126862 159025 116795 83016 127770 3067 56860 19833 64583 11977 100045 198272 1...

output:

2654974404037027

result:

ok single line: '2654974404037027'

Test #4:

score: 0
Accepted
time: 99ms
memory: 47452kb

input:

1
200000 10000000
172532 195148 85227 155659 14990 35802 156340 83797 197030 21580 88873 28715 24950 12770 82745 113383 51071 21570 147715 85175 15394 148817 70297 38951 36754 55240 181122 186649 17533 4230 68223 4978 154960 144826 26781 185779 18292 195221 34441 37597 148301 81791 67645 197829 3693...

output:

1991871314556006

result:

ok single line: '1991871314556006'

Test #5:

score: 0
Accepted
time: 105ms
memory: 47788kb

input:

1
200000 1000000000
176004 145470 198726 37315 113426 105334 46063 63520 103219 121771 57158 185334 126226 141718 133227 158448 119801 62952 10829 175859 190290 80534 82892 42340 164975 79666 102327 14872 75277 57094 37650 183007 86587 47862 133654 48675 26581 199005 14582 75902 5988 102670 169993 1...

output:

2910045863489510

result:

ok single line: '2910045863489510'

Test #6:

score: 0
Accepted
time: 69ms
memory: 47252kb

input:

1
200000 40000
118673 82089 118673 118673 129221 118673 125539 106895 82089 129221 125539 118673 82089 129221 118673 125539 106895 106895 129221 118673 129221 82089 82089 125539 129221 129221 118673 106895 82089 129221 106895 82089 106895 82089 106895 106895 129221 118673 106895 82089 106895 82089 8...

output:

2404687852449892

result:

ok single line: '2404687852449892'

Test #7:

score: 0
Accepted
time: 80ms
memory: 46976kb

input:

1
200000 10000000
110770 103054 103054 67926 87448 110770 103054 67926 87448 87448 87448 87448 176259 67926 176259 110770 103054 110770 176259 67926 67926 67926 87448 87448 87448 67926 110770 67926 67926 67926 176259 87448 87448 176259 87448 103054 110770 176259 103054 87448 87448 176259 103054 1107...

output:

1871051161222455

result:

ok single line: '1871051161222455'

Test #8:

score: 0
Accepted
time: 70ms
memory: 46588kb

input:

1
200000 1000000000
92387 35422 92387 192988 35422 35422 92387 192988 35422 192988 192988 124898 35422 124898 124898 92387 35422 192988 124898 35422 132532 92387 132532 192988 35422 92387 124898 132532 35422 92387 35422 35422 124898 132532 92387 92387 192988 192988 124898 92387 92387 35422 124898 19...

output:

2226090142242870

result:

ok single line: '2226090142242870'

Test #9:

score: 0
Accepted
time: 102ms
memory: 50864kb

input:

1
200000 40000
116752 38573 63445 128225 135901 46112 158662 189088 148798 191324 45035 154544 80127 91833 149943 48432 92060 43958 137795 77575 36781 45299 178350 100696 60034 61055 85213 69563 78385 21262 146118 102661 187678 130733 9787 16393 117846 185565 60069 3846 141624 104285 39616 57159 868...

output:

1143189663913379

result:

ok single line: '1143189663913379'

Test #10:

score: 0
Accepted
time: 116ms
memory: 50980kb

input:

1
200000 10000000
140527 170359 145004 37893 122429 127925 3461 95526 78195 26127 92887 141377 182681 136498 39270 58777 71455 38154 157727 24834 179001 155490 60975 147064 75506 36710 46141 189903 21088 196674 125247 103266 25995 4763 41038 180967 4283 21509 10323 142860 120149 127930 146726 109420...

output:

677866460957128

result:

ok single line: '677866460957128'

Test #11:

score: 0
Accepted
time: 114ms
memory: 50832kb

input:

1
200000 1000000000
127458 93467 147620 82303 77297 18842 171477 180789 73198 66451 114690 7452 145760 97912 182102 47863 91886 284 142563 117252 129365 135867 136635 64262 196952 183491 187881 182341 82756 135693 126974 12627 194511 30468 29983 30464 28044 129494 64477 122463 141768 103686 105366 2...

output:

1985383835721543

result:

ok single line: '1985383835721543'

Test #12:

score: 0
Accepted
time: 93ms
memory: 50664kb

input:

1
200000 40000
131243 50411 183082 141585 81827 174160 175932 74477 31310 20599 29372 136846 36578 133487 134440 147839 115280 45462 77063 5065 29725 137589 80437 170073 105648 191314 31432 157802 148931 184650 177611 69456 152253 158494 111351 10329 108646 119661 65317 177563 88191 60803 135866 555...

output:

1940771592135540

result:

ok single line: '1940771592135540'

Test #13:

score: 0
Accepted
time: 80ms
memory: 52396kb

input:

1
200000 10000000
152041 174470 165831 197804 141842 124891 172029 99878 29839 80130 188275 172670 60080 40554 177721 183620 68381 153260 43684 146559 5617 163940 5828 28239 152505 98252 150314 82119 197777 166408 147215 105141 138523 31398 31463 145584 147517 170652 189441 191295 120537 44537 57915...

output:

1651721014252886

result:

ok single line: '1651721014252886'

Test #14:

score: 0
Accepted
time: 72ms
memory: 59484kb

input:

1
200000 1000000000
113494 96820 113494 96820 113494 186155 113494 72596 113494 96820 96820 72596 72596 72596 113494 186155 72596 72596 72596 186155 140986 96820 113494 72596 186155 186155 140986 96820 186155 140986 113494 186155 186155 186155 96820 140986 72596 140986 140986 96820 72596 96820 96820...

output:

1451927259600000

result:

ok single line: '1451927259600000'

Test #15:

score: 0
Accepted
time: 174ms
memory: 57540kb

input:

1
200000 40000
4081 16238 11969 107578 198622 110168 135045 77728 131657 124891 24584 12007 91833 83365 54207 177065 31062 5567 193813 13669 194769 184436 37796 38153 154996 103928 4152 167404 164454 179449 1292 90714 631 188401 121030 170338 82614 92709 101075 197243 118713 156023 89703 132201 1498...

output:

551622758100000

result:

ok single line: '551622758100000'

Test #16:

score: 0
Accepted
time: 178ms
memory: 55428kb

input:

1
200000 10000000
118036 3793 5512 165907 172930 121912 186134 148346 83443 148336 65861 79032 153603 834 89892 55452 62702 146776 16844 122756 146483 134711 163729 88863 155117 48251 152885 125608 16053 13493 24658 118369 116062 126154 145675 46280 108793 91814 162570 11989 193985 96015 104182 6961...

output:

631963159800000

result:

ok single line: '631963159800000'

Test #17:

score: 0
Accepted
time: 174ms
memory: 59628kb

input:

1
200000 1000000000
33836 100196 59375 75273 128299 104538 94912 26541 50585 134718 98137 40094 123637 167966 149636 26105 22668 121583 92264 94615 56134 10763 140098 100436 16529 60575 111368 27690 73533 193629 180594 81747 109487 21125 29088 194323 135905 95040 18540 81652 176196 50575 186436 1418...

output:

1518987594900000

result:

ok single line: '1518987594900000'

Test #18:

score: 0
Accepted
time: 107ms
memory: 58332kb

input:

1
200000 40000
81104 81104 124637 80107 79894 80107 79894 124637 79894 81104 80107 108634 124637 80107 80107 124637 124637 81104 80107 80107 80107 80107 108634 79894 124637 79894 124637 124637 80107 79894 108634 124637 108634 79894 108634 124637 81104 80107 79894 80107 81104 124637 80107 80107 81104...

output:

2492752463700000

result:

ok single line: '2492752463700000'

Test #19:

score: 0
Accepted
time: 94ms
memory: 62416kb

input:

1
200000 10000000
113380 113380 6166 113380 73481 196022 142288 113380 142288 113380 196022 113380 73481 73481 142288 6166 6166 142288 142288 6166 6166 6166 6166 73481 73481 113380 196022 73481 196022 73481 73481 196022 113380 113380 6166 6166 196022 6166 6166 73481 113380 6166 113380 113380 73481 6...

output:

3920459602200000

result:

ok single line: '3920459602200000'

Test #20:

score: 0
Accepted
time: 89ms
memory: 64732kb

input:

1
200000 1000000000
55030 139062 93934 139062 139062 188123 93934 93934 55030 93934 93934 93934 188123 139062 55030 79446 79446 93934 93934 79446 93934 79446 188123 188123 55030 79446 55030 139062 93934 79446 188123 79446 93934 188123 139062 188123 188123 93934 55030 93934 55030 188123 93934 79446 5...

output:

1100605503000000

result:

ok single line: '1100605503000000'

Test #21:

score: 0
Accepted
time: 135ms
memory: 67244kb

input:

1
200000 40000
58977 6108 192639 180048 171812 123648 62338 77818 104884 77820 6656 90023 106001 100891 74036 166651 44552 176104 164299 22650 142254 62112 94221 64472 87477 117679 67351 88407 19219 20120 131885 182802 182606 76083 37465 44793 124801 101708 21282 72200 190182 51075 25878 8311 13717 ...

output:

2574846010902855

result:

ok single line: '2574846010902855'

Test #22:

score: 0
Accepted
time: 212ms
memory: 64956kb

input:

1
200000 10000000
32788 36106 63672 13633 133399 118569 177557 164892 70843 121881 44540 191592 148535 126625 128051 88393 11348 191253 59176 1824 105567 87004 192147 103542 76045 169545 147713 110812 53394 193545 30238 132934 71048 87462 48364 120934 93056 95812 50355 38222 11588 87032 123090 10803...

output:

2729241306046102

result:

ok single line: '2729241306046102'

Test #23:

score: 0
Accepted
time: 158ms
memory: 64176kb

input:

1
200000 1000000000
131865 21550 68612 79003 122691 149098 36210 58257 146491 195940 148723 135722 78009 36153 173518 139189 147549 29416 116017 31887 130076 74555 168769 146610 170223 18391 151390 123127 106284 103512 44593 31031 126874 171878 21855 119249 153019 42452 116328 199508 151523 139591 1...

output:

1666108330500000

result:

ok single line: '1666108330500000'

Test #24:

score: 0
Accepted
time: 102ms
memory: 51712kb

input:

1
200000 40000
136112 186291 100280 154449 159476 81489 98318 122844 76856 173595 31743 87091 54046 10622 107038 56139 99877 20376 189082 88641 13927 189464 183573 49637 103012 125344 52481 40353 3113 6858 76911 66861 180277 196605 151580 155980 135714 39503 93410 20548 126999 124971 82085 59247 111...

output:

2441525425533106

result:

ok single line: '2441525425533106'

Test #25:

score: 0
Accepted
time: 141ms
memory: 57328kb

input:

1
200000 10000000
162806 129138 143698 143684 52876 16339 82915 48852 115812 118744 45914 176901 77564 68400 135411 163307 10299 105681 3950 159948 77979 167276 3657 49709 185308 97441 79567 107905 179443 138462 1243 31895 156534 38122 171958 51476 17616 90813 28861 54137 63981 33759 189645 82105 19...

output:

2144464394695140

result:

ok single line: '2144464394695140'

Test #26:

score: 0
Accepted
time: 101ms
memory: 52600kb

input:

1
200000 1000000000
78833 145328 38486 129536 60040 9191 30204 80118 112190 47996 93256 178969 179815 100815 132643 100687 150108 137864 37775 26735 138430 169640 72378 29061 17640 29159 190247 113685 153946 121291 196453 35286 199171 88414 14895 84356 188662 83162 13022 112994 121011 131189 27284 1...

output:

3315416577000000

result:

ok single line: '3315416577000000'

Test #27:

score: 0
Accepted
time: 60ms
memory: 51440kb

input:

1
200000 40000
171393 130093 24874 162947 91299 171393 91299 24874 91299 91299 171393 24874 171393 130093 130093 130093 130093 130093 91299 91299 24874 162947 171393 162947 91299 91299 171393 91299 171393 24874 162947 91299 24874 130093 171393 162947 162947 171393 162947 24874 91299 162947 24874 912...

output:

2601873009300000

result:

ok single line: '2601873009300000'

Test #28:

score: 0
Accepted
time: 60ms
memory: 54032kb

input:

1
200000 10000000
192275 54817 112201 192275 54817 112201 173719 173719 96044 54817 96044 192275 54817 173719 192275 96044 173719 112201 54817 112201 112201 173719 54817 173719 192275 192275 192275 192275 96044 112201 173719 173719 112201 173719 96044 173719 173719 54817 192275 192275 112201 96044 1...

output:

3474397371900000

result:

ok single line: '3474397371900000'

Test #29:

score: 0
Accepted
time: 59ms
memory: 56840kb

input:

1
200000 1000000000
170945 122563 21753 21753 122563 21753 21753 170945 21753 170945 4558 122563 76598 21753 4558 122563 4558 21753 4558 122563 170945 21753 122563 4558 4558 170945 76598 170945 21753 76598 76598 170945 21753 170945 170945 76598 122563 4558 4558 76598 21753 21753 170945 76598 170945 ...

output:

91160455800000

result:

ok single line: '91160455800000'

Test #30:

score: 0
Accepted
time: 123ms
memory: 55632kb

input:

1
200000 40000
57272 148259 103913 35535 50615 42658 98719 181844 114732 939 195461 181376 140503 163961 70721 88757 84109 32701 174170 115475 34835 110046 125377 93603 71528 195519 112461 176353 51147 9256 162324 87296 23871 58118 63695 162693 31707 187748 178014 63229 103282 156388 81694 199276 15...

output:

3133634911846493

result:

ok single line: '3133634911846493'

Test #31:

score: 0
Accepted
time: 172ms
memory: 62968kb

input:

1
200000 10000000
112500 90246 62921 195911 136745 199776 44489 40804 99619 17241 119733 56381 139049 166317 40861 78167 49111 156547 114170 109702 101822 1745 174847 17521 53424 18555 11372 93868 115988 64517 42497 76581 2809 66033 78528 37839 154393 188229 79261 192324 31000 20862 147336 153027 12...

output:

3651098255400000

result:

ok single line: '3651098255400000'

Test #32:

score: 0
Accepted
time: 134ms
memory: 57936kb

input:

1
200000 1000000000
132700 119807 185544 154491 117888 74807 94376 195743 82179 66540 129245 122016 35981 99582 175418 194522 149003 154339 9846 20288 25216 101070 144 3633 143873 43429 17472 168735 139908 3295 171802 117288 142673 135841 37402 140757 88637 68374 76847 150625 188354 11779 157491 101...

output:

1245466695260976

result:

ok single line: '1245466695260976'

Test #33:

score: 0
Accepted
time: 49ms
memory: 71560kb

input:

1
200000 1000000000
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ...

output:

36271232538

result:

ok single line: '36271232538'

Test #34:

score: 0
Accepted
time: 60ms
memory: 25420kb

input:

101
804 569255960
501 134 501 605 605 501 134 605 605 501 605 501 134 605 605 134 407 501 407 501 605 407 134 134 605 605 134 501 501 134 407 501 501 407 134 501 605 134 134 134 501 134 407 605 605 501 501 407 134 501 501 407 501 407 134 134 605 605 605 407 407 605 134 605 605 605 134 407 605 501 13...

output:

185188538
146878847
245029219
352163694
185664666
94118714
415029105
144072128
190478600
164704080
51196302
300909977
225338145
126775054
155573423
200853919
166334503
216122690
183227794
68795056
189590304
37198542
261504691
115368648
93078486
111333313
287317913
126177627
261544569
168326353
22496...

result:

ok 101 lines

Extra Test:

score: 0
Extra Test Passed