QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#822925#9772. Permutation Routingucup-team087#AC ✓161ms15052kbC++2334.6kb2024-12-20 17:31:242025-01-10 11:38:30

Judging History

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

  • [2025-01-10 11:38:30]
  • 自动重测本题所有获得100分的提交记录
  • 测评结果:AC
  • 用时:161ms
  • 内存:15052kb
  • [2025-01-10 11:38:24]
  • hack成功,自动添加数据
  • (/hack/1438)
  • [2024-12-20 17:31:26]
  • 评测
  • 测评结果:100
  • 用时:163ms
  • 内存:14924kb
  • [2024-12-20 17:31:24]
  • 提交

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 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_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 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/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 2 "library/graph/find_centroid.hpp"

// (v,w) or (v,-1)
template <typename GT>
pair<int, int> find_centroids(GT& G) {
  int N = G.N;
  vc<int> par(N, -1);
  vc<int> V(N);
  vc<int> sz(N);
  int l = 0, r = 0;
  V[r++] = 0;
  while (l < r) {
    int v = V[l++];
    for (auto&& e: G[v])
      if (e.to != par[v]) {
        par[e.to] = v;
        V[r++] = e.to;
      }
  }
  FOR_R(i, N) {
    int v = V[i];
    sz[v] += 1;
    int p = par[v];
    if (p != -1) sz[p] += sz[v];
  }

  int M = N / 2;
  auto check = [&](int v) -> bool {
    if (N - sz[v] > M) return false;
    for (auto&& e: G[v]) {
      if (e.to != par[v] && sz[e.to] > M) return false;
    }
    return true;
  };
  pair<int, int> ANS = {-1, -1};
  FOR(v, N) if (check(v)) {
    if (ANS.fi != -1) {
      ANS.se = v;
    } else {
      ANS.fi = v;
    }
  }
  return ANS;
}
#line 2 "library/graph/tree.hpp"

#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/random/base.hpp"

u64 RNG_64() {
  static u64 x_ = u64(chrono::duration_cast<chrono::nanoseconds>(chrono::high_resolution_clock::now().time_since_epoch()).count()) * 10150724397891781847ULL;
  x_ ^= x_ << 7;
  return x_ ^= x_ >> 9;
}

u64 RNG(u64 lim) { return RNG_64() % lim; }

ll RNG(ll l, ll r) { return l + RNG_64() % (r - l); }
#line 2 "library/random/shuffle.hpp"

template <typename T>
void shuffle(vc<T>& A) {
  FOR(i, len(A)) {
    int j = RNG(0, i + 1);
    if (i != j) swap(A[i], A[j]);
  }
}
#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 5 "library/random/random_graph.hpp"

void random_relabel(int N, vc<pair<int, int>>& G) {
  shuffle(G);
  vc<int> A(N);
  FOR(i, N) A[i] = i;
  shuffle(A);
  for (auto& [a, b]: G) a = A[a], b = A[b];
}

template <int DIRECTED>
vc<pair<int, int>> random_graph(int n, bool simple) {
  vc<pair<int, int>> G, cand;
  FOR(a, n) FOR(b, n) {
    if (simple && a == b) continue;
    if (!DIRECTED && a > b) continue;
    cand.eb(a, b);
  }
  int m = RNG(0, len(cand) + 1);
  set<int> ss;
  FOR(m) {
    while (1) {
      int i = RNG(0, len(cand));
      if (simple && ss.count(i)) continue;
      ss.insert(i);
      auto [a, b] = cand[i];
      G.eb(a, b);
      break;
    }
  }
  random_relabel(n, G);
  return G;
}

vc<pair<int, int>> random_tree(int n) {
  vc<pair<int, int>> G;
  FOR(i, 1, n) { G.eb(RNG(0, i), i); }
  random_relabel(n, G);
  return G;
}

// EDGE = true: 各辺が唯一のサイクル(関節点でサイクルまたは辺)
// EDGE = false: 各頂点が唯一のサイクル(橋でサイクルまたは辺)
vc<pair<int, int>> random_cactus(int N, bool EDGE) {
  if (!EDGE) {
    // n 頂点を 1 または 3 以上に分割
    vvc<int> A;
    int n = RNG(1, N + 1);
    vc<int> S(n, 1);
    int rest = N - n;
    while (rest > 0) {
      int k = RNG(0, n);
      if (S[k] == 1) {
        if (rest == 1) {
          S.eb(1), rest = 0;
        } else {
          S[k] += 2, rest -= 2;
        }
      } else {
        S[k]++, rest--;
      }
    }
    n = len(S);
    int p = 0;
    FOR(i, n) {
      vc<int> C;
      FOR(v, p, p + S[i]) C.eb(v);
      A.eb(C);
      p += S[i];
    }
    int m = len(A);
    auto H = random_tree(m);
    vc<pair<int, int>> G;
    FOR(i, m) {
      vc<int>& V = A[i];
      if (len(V) == 1) continue;
      FOR(k, len(V)) { G.eb(V[k], V[(1 + k) % len(V)]); }
    }
    for (auto& [c1, c2]: H) {
      int a = A[c1][RNG(0, len(A[c1]))];
      int b = A[c2][RNG(0, len(A[c2]))];
      G.eb(a, b);
    }
    random_relabel(N, G);
    return G;
  }
  assert(EDGE);
  if (N == 1) return {};
  int n = RNG(1, N);
  vc<int> S(n, 2);
  int rest = N - 1 - n;
  while (rest > 0) {
    int k = RNG(0, n);
    S[k]++, --rest;
  }
  vvc<int> A;
  int p = 0;
  FOR(i, n) {
    vc<int> C;
    FOR(v, p, p + S[i]) C.eb(v);
    A.eb(C);
    p += S[i];
  }
  assert(p == N + n - 1);
  UnionFind uf(p);
  auto H = random_tree(n);
  for (auto& [c1, c2]: H) {
    int a = A[c1][RNG(0, len(A[c1]))];
    int b = A[c2][RNG(0, len(A[c2]))];
    uf.merge(a, b);
  }
  vc<int> new_idx(p);
  int x = 0;
  FOR(i, p) if (uf[i] == i) new_idx[i] = x++;
  assert(x == N);
  FOR(i, p) new_idx[i] = new_idx[uf[i]];
  vc<pair<int, int>> G;
  FOR(i, n) {
    vc<int>& V = A[i];
    for (auto& v: V) v = new_idx[v];
    if (len(V) == 2) {
      G.eb(V[0], V[1]);
    } else {
      FOR(k, len(V)) { G.eb(V[k], V[(1 + k) % len(V)]); }
    }
  }
  random_relabel(N, G);
  return G;
}
#line 8 "main.cpp"

vc<tuple<int, int, int>> sub(int N, vc<int> A, Graph<int, 0> G) {
  if (N == 1) {
    assert(A[0] == 0);
    return {};
  }
  int c = find_centroids(G).fi;
  Tree<decltype(G)> tree(G, c);

  vc<int> color(N);
  FOR(v, N) {
    if (v == c) {
      color[v] = c;
    } else {
      color[v] = tree.jump(c, v, 1);
    }
  }

  vc<tuple<int, int, int>> ANS;
  vc<int> par = tree.parent;

  auto is_ok = [&](int v) -> bool { return color[v] == color[A[v]]; };

  int t = -1;
  while (1) {
    ++t;
    bool ed = 1;
    FOR(v, N) if (!is_ok(v)) ed = 0;
    if (ed) break;

    auto dfs = [&](auto& dfs, int v, bool used) -> void {
      auto ch = tree.collect_child(v);
      if (used) {
        for (auto& w: ch) { dfs(dfs, w, false); }
        return;
      }

      // w とスワップします
      int w = [&]() -> int {
        if (v == c) {
          if (!is_ok(v)) {
            int w = color[A[v]];
            // w にはこびこみたい
            if (!is_ok(w)) return w;
            return -1;
          }
          // 中心は正しくなっているので、おかしい部分木からもらってくる
          for (auto& w: ch) {
            if (is_ok(w)) continue;
            return w;
          }
          return -1;
        }
        assert(v != c);
        if (is_ok(v)) {
          // 正しい色を持っているので、部分木に落としたい
          for (auto& w: ch) {
            if (is_ok(w)) continue;
            return w;
          }
          return -1;
        }
        // v は違う色をもっているので子とのスワップはなし
        return -1;
      }();
      if (w == -1) {
        for (auto& w: ch) { dfs(dfs, w, false); }
      } else {
        ANS.eb(t, v, w);
        swap(A[v], A[w]);
        for (auto& ww: ch) { dfs(dfs, ww, w == ww); }
      }
    };
    dfs(dfs, c, false);
  }

  // 各色の辺をいじった最終時刻
  vc<int> last(N, -1);

  for (auto& [t, a, b]: ANS) {
    chmax(last[color[a]], t);
    chmax(last[color[b]], t);
  }

  vvc<int> vs(N);
  FOR(v, N) vs[color[v]].eb(v);

  vc<int> new_idx(N, -1);

  FOR(k, N) {
    vc<int> V = vs[k];
    if (V.empty()) continue;
    int n = len(V);
    FOR(i, n) new_idx[V[i]] = i;
    Graph<int, 0> H = G.rearrange(V);
    vc<int> B(n);
    FOR(i, n) { B[i] = new_idx[A[V[i]]]; }
    FOR(i, n) new_idx[V[i]] = -1;
    auto sub_ANS = sub(n, B, H);
    for (auto& [t, a, b]: sub_ANS) { ANS.eb(last[k] + 1 + t, V[a], V[b]); }
  }
  return ANS;
}

void solve() {
  INT(N);
  VEC(int, A, N);
  Graph<int, 0> G(N);
  G.read_tree();
  for (auto& x: A) --x;
  auto ANS = sub(N, A, G);

  map<pi, int> MP;
  for (auto& e: G.edges) {
    int a = e.frm, b = e.to;
    if (a > b) swap(a, b);
    pi p = {a, b};
    MP[p] = e.id;
  }
  auto get_eid = [&](int a, int b) -> int {
    if (a > b) swap(a, b);
    pi p = {a, b};
    return MP[p];
  };

  vvc<int> dat(3 * N);
  for (auto& [t, a, b]: ANS) {
    assert(t < 3 * N);
    dat[t].eb(get_eid(a, b));
  }

  vvc<int> tmp;
  for (auto& A: dat) {
    if (A.empty()) continue;
    tmp.eb(A);
  }
  swap(dat, tmp);

  // check ANS
  for (auto& E: dat) {
    vc<int> V;
    for (auto& eid: E) {
      int a = G.edges[eid].frm;
      int b = G.edges[eid].to;
      swap(A[a], A[b]);
      V.eb(a), V.eb(b);
    }
    sort(all(V));
    FOR(k, len(V) - 1) assert(V[k] != V[k + 1]);
  }
  FOR(v, N) assert(A[v] == v);

  print(len(dat));
  for (vc<int> E: dat) {
    for (auto& x: E) ++x;
    print(len(E), E);
  }
}

signed main() {
  FOR(1000) {}
  INT(T);
  FOR(T) solve();
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3684kb

input:

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

output:

3
2 3 4
1 1
2 2 4

result:

ok ok, up to 3 steps were used

Test #2:

score: 0
Accepted
time: 45ms
memory: 3992kb

input:

10000
5
2 3 1 5 4
1 5
3 2
1 2
1 4
5
1 2 3 4 5
2 3
3 4
2 1
4 5
5
4 2 5 1 3
3 5
2 3
4 1
3 1
5
1 3 4 2 5
5 3
2 1
1 3
2 4
5
1 2 3 4 5
2 1
3 5
2 3
5 4
5
1 2 3 4 5
4 5
3 4
4 2
4 1
5
5 2 1 4 3
2 1
5 1
3 1
1 4
5
4 1 2 5 3
3 1
5 1
1 2
1 4
5
5 3 4 2 1
3 1
3 5
4 3
3 2
5
3 4 1 2 5
3 2
3 5
1 5
3 4
5
3 4 1 2 5
2 ...

output:

5
1 2
1 3
1 1
1 4
1 1
0
1
2 1 3
3
1 3
1 2
2 3 4
0
0
2
1 2
1 3
4
1 4
1 2
1 1
1 3
5
1 3
1 4
1 1
1 2
1 1
5
1 3
1 2
2 1 3
1 4
1 1
3
1 2
2 4 1
1 2
3
1 1
1 4
1 2
0
7
1 3
1 2
2 3 4
2 2 1
1 3
2 2 1
1 4
4
1 1
2 2 3
1 1
2 2 3
1
2 2 1
0
0
0
4
1 2
2 4 1
1 2
2 4 1
4
1 1
1 4
1 2
1 3
0
3
1 3
1 1
2 2 3
0
4
1 1
1 4
...

result:

ok ok, up to 7 steps were used

Test #3:

score: 0
Accepted
time: 127ms
memory: 4080kb

input:

10000
10
2 7 5 6 4 8 3 1 10 9
8 10
6 1
5 6
4 7
10 2
6 8
7 6
5 3
9 8
10
4 10 6 1 8 3 9 5 7 2
3 10
9 10
5 10
4 6
10 8
10 4
7 10
4 2
4 1
10
9 7 5 10 3 8 2 6 1 4
10 3
9 3
6 3
7 3
4 3
2 3
5 2
3 8
3 1
10
10 9 5 6 3 4 8 7 2 1
2 6
7 8
5 2
4 9
9 3
3 8
8 5
6 10
4 1
10
2 1 6 7 10 3 4 9 8 5
10 3
3 5
1 3
3 2
3 9...

output:

7
2 6 5
2 2 1
1 6
2 7 1
4 3 4 9 5
3 7 8 1
2 4 9
10
1 4
1 6
2 1 8
1 6
2 2 4
2 7 9
1 2
1 3
1 5
1 3
12
1 6
2 4 7
1 6
1 1
1 5
1 1
1 2
1 9
1 2
1 3
1 8
1 3
17
1 2
1 7
1 6
2 7 5
3 6 3 4
3 7 1 9
3 6 3 8
3 7 1 5
2 6 4
2 7 5
2 6 3
1 7
2 6 1
3 4 3 8
3 9 5 1
3 4 3 8
2 9 5
13
1 6
1 9
1 5
1 9
1 1
1 2
1 1
1 3
1 4
...

result:

ok ok, up to 20 steps were used

Test #4:

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

input:

2500
20
19 16 18 14 10 3 6 8 9 20 11 1 13 5 4 17 12 15 7 2
14 9
11 1
7 16
9 5
3 1
16 19
15 4
6 9
7 2
12 15
8 5
20 19
16 9
18 1
1 17
1 15
13 15
9 10
1 19
20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
5 14
6 10
12 14
9 1
13 1
18 15
7 10
7 12
17 4
4 5
16 9
11 10
18 8
13 19
6 20
2 4
3 4
15 11
8 ...

output:

16
3 6 7 18
2 19 13
3 6 16 8
1 12
1 6
2 19 13
1 6
2 19 13
3 5 1 3
2 14 4
2 16 18
3 15 10 13
2 16 8
2 10 13
1 9
1 3
0
0
36
2 13 11
3 10 14 4
3 13 9 6
4 10 14 12 19
5 13 9 18 11 1
4 10 12 5 4
5 13 18 8 11 6
6 10 14 5 15 4 19
5 13 9 8 3 6
4 10 14 12 15
4 13 9 18 11
4 10 12 5 4
4 13 18 8 11
4 10 14 5 16...

result:

ok ok, up to 47 steps were used

Test #5:

score: 0
Accepted
time: 65ms
memory: 4088kb

input:

400
50
18 35 3 1 22 2 29 14 27 44 32 4 34 43 33 23 21 48 13 47 46 50 38 12 49 9 11 31 10 8 37 41 42 36 17 15 45 16 24 40 25 30 19 28 7 39 5 20 6 26
45 32
39 28
27 43
29 39
34 40
17 12
8 38
3 23
5 41
40 8
36 43
41 25
19 1
18 43
1 46
28 4
39 11
42 20
44 40
35 43
50 43
15 9
31 20
35 37
22 42
29 30
21 2...

output:

59
4 47 10 9 37
3 35 19 31
3 43 5 17
3 47 32 21
4 30 19 22 3
3 43 8 48
3 39 21 18
3 47 13 3
4 43 28 15 48
3 35 44 21
4 47 9 17 3
2 43 32
2 35 21
3 43 17 20
3 47 21 24
4 1 28 20 34
3 43 44 24
3 35 46 21
3 43 41 20
3 1 21 29
2 47 20
1 43
2 35 21
3 47 4 11
3 30 28 27
2 47 44
3 35 28 12
3 47 44 4
3 30 2...

result:

ok ok, up to 129 steps were used

Test #6:

score: 0
Accepted
time: 67ms
memory: 4092kb

input:

400
50
16 19 35 42 20 8 28 6 25 15 49 31 24 34 10 1 50 33 2 5 26 46 27 13 9 21 23 7 30 29 12 44 18 14 3 40 45 48 43 36 47 4 39 32 37 22 41 38 11 17
13 24
1 43
6 10
12 47
21 47
17 13
2 20
10 8
35 33
5 13
1 20
23 40
13 14
47 9
49 2
42 41
10 42
8 3
27 34
42 29
46 48
46 14
23 42
1 36
26 31
33 14
2 50
7 ...

output:

56
7 41 33 30 10 36 2 48
6 46 17 25 49 11 5
5 22 16 45 7 28
4 26 21 2 27
4 22 33 11 48
5 13 21 17 7 5
4 46 33 23 10
4 13 17 12 45
4 46 23 6 2
3 13 45 11
3 46 42 2
3 13 45 31
3 22 29 2
3 46 21 24
4 13 33 45 44
5 22 17 35 2 19
3 26 21 24
4 22 33 44 48
4 46 21 8 4
4 22 33 18 45
4 46 21 8 2
4 22 33 45 2...

result:

ok ok, up to 128 steps were used

Test #7:

score: 0
Accepted
time: 45ms
memory: 4144kb

input:

100
100
49 90 5 62 95 42 92 16 74 58 66 85 21 98 15 6 73 13 40 78 44 2 93 11 54 76 80 34 89 96 31 56 67 77 79 39 26 14 23 81 3 69 37 97 83 25 12 22 61 48 17 84 35 47 36 53 7 91 60 100 82 57 4 99 45 28 70 72 59 86 33 64 75 41 94 51 29 46 20 8 24 52 1 43 19 27 38 10 55 68 32 71 65 9 30 88 18 87 63 50
...

output:

112
13 8 10 74 95 35 30 52 33 72 73 60 7 82
7 37 44 67 77 79 51 23
4 8 10 30 50
4 68 44 22 79
5 37 10 4 42 90
4 18 22 30 91
4 68 86 79 46
4 8 39 90 78
3 18 44 91
4 68 10 86 46
4 8 17 90 76
3 68 44 91
4 8 64 90 46
2 37 91
3 18 30 3
3 68 86 2
4 37 87 92 90
3 68 30 91
3 8 2 90
3 84 44 14
3 8 34 62
3 18...

result:

ok ok, up to 259 steps were used

Test #8:

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

input:

100
100
86 35 82 12 83 29 60 50 90 33 87 4 47 51 70 96 48 31 89 84 92 37 55 94 78 80 57 46 6 99 18 74 10 38 2 63 22 34 67 77 72 44 68 42 79 28 13 17 62 8 14 91 97 98 23 66 27 69 100 7 75 49 36 73 88 56 39 43 58 15 85 41 64 32 61 95 40 25 45 26 93 3 5 20 71 1 11 65 19 9 52 21 81 24 76 16 53 54 30 59
...

output:

139
9 83 43 45 6 10 48 3 51 89
4 61 52 47 25
5 83 81 67 51 76
6 22 59 79 47 25 90
5 83 48 80 51 76
6 18 52 86 47 25 5
6 95 78 81 31 51 76
4 18 36 72 25
5 83 78 21 40 91
5 18 36 17 1 47
5 22 78 21 32 51
6 83 36 17 63 48 25
5 18 21 32 52 47
5 83 78 17 16 51
4 22 36 47 33
5 83 21 48 51 98
6 18 20 52 47...

result:

ok ok, up to 276 steps were used

Test #9:

score: 0
Accepted
time: 26ms
memory: 4544kb

input:

1
1000
852 223 408 154 361 680 77 982 706 853 792 191 955 586 379 419 503 697 500 694 599 90 782 234 186 750 322 213 91 315 882 420 605 177 968 269 175 386 69 825 488 696 58 316 877 885 375 872 589 109 75 263 442 760 273 663 547 43 426 372 844 579 425 777 865 878 409 466 39 286 538 726 771 401 51 75...

output:

1190
104 400 140 869 351 45 917 713 163 257 268 361 453 38 994 312 687 870 44 760 772 474 329 54 111 900 75 366 130 650 710 155 343 904 256 69 579 585 186 901 170 691 428 486 221 338 906 239 136 556 964 201 882 222 422 395 438 498 623 414 523 880 950 833 533 374 581 292 115 682 652 135 417 597 601 8...

result:

ok ok, up to 1190 steps were used

Test #10:

score: 0
Accepted
time: 31ms
memory: 4532kb

input:

1
1000
690 995 868 467 793 949 539 17 557 637 246 77 677 681 714 642 40 43 69 594 433 612 283 865 152 907 30 486 979 253 139 227 810 478 932 439 202 508 388 901 705 821 822 209 920 725 133 469 165 205 16 403 598 858 198 81 954 600 7 387 335 178 658 859 115 905 166 55 127 745 738 144 511 383 648 452 ...

output:

1314
156 304 474 194 392 671 391 666 326 548 208 577 166 12 546 859 628 139 150 238 221 141 436 865 927 730 884 444 207 104 413 903 22 507 891 647 957 467 944 656 815 298 746 515 373 313 597 441 337 454 352 234 420 19 725 549 57 136 994 672 144 826 409 610 947 923 943 556 528 499 547 262 780 374 462...

result:

ok ok, up to 1314 steps were used

Test #11:

score: 0
Accepted
time: 43ms
memory: 6024kb

input:

1
1000
981 206 539 504 568 390 696 919 303 771 71 524 891 51 96 748 158 236 88 633 652 740 616 762 348 636 53 479 509 409 557 920 738 326 224 592 898 867 522 530 953 939 793 657 819 1 110 500 200 170 559 435 627 589 560 551 359 323 787 30 239 783 650 442 969 68 459 830 966 380 630 482 550 352 14 929...

output:

1296
201 348 724 1 446 766 726 887 257 203 249 441 478 707 590 354 2 140 698 535 700 445 76 481 65 163 982 188 82 867 914 742 280 523 122 909 303 145 9 407 576 331 901 776 949 378 292 625 480 833 816 697 196 246 538 567 602 74 799 667 150 794 817 78 488 319 433 305 750 735 219 126 510 314 596 815 77...

result:

ok ok, up to 1296 steps were used

Test #12:

score: 0
Accepted
time: 73ms
memory: 9264kb

input:

1
1000
327 708 701 120 714 171 723 70 98 93 402 303 137 145 936 525 741 99 328 656 48 553 753 783 945 254 547 405 823 842 844 822 690 206 436 6 919 560 267 886 613 147 810 908 771 130 407 893 578 439 929 426 59 879 738 770 156 651 323 119 697 357 884 599 990 347 172 590 545 438 223 677 663 363 115 2...

output:

1304
248 242 680 794 56 772 48 400 27 143 515 919 22 489 890 653 896 51 99 505 678 769 677 198 879 243 264 300 213 711 992 622 743 847 517 625 923 876 365 77 464 821 10 809 697 853 522 646 168 941 547 948 34 179 869 728 715 526 669 281 690 178 682 491 846 570 249 147 157 267 639 291 162 282 486 623 ...

result:

ok ok, up to 1304 steps were used

Test #13:

score: 0
Accepted
time: 75ms
memory: 9184kb

input:

1
1000
332 858 822 192 770 336 467 401 841 606 399 550 239 834 383 300 497 970 19 542 688 478 994 273 553 541 676 654 667 348 904 906 986 21 997 900 437 684 568 44 634 216 120 333 123 231 42 631 488 442 435 824 12 93 774 877 203 490 15 565 151 413 567 782 777 484 861 206 188 707 334 374 99 136 178 5...

output:

1267
254 85 527 307 446 21 520 724 876 419 602 599 57 135 128 230 958 497 490 32 462 871 959 327 655 140 416 166 255 751 77 383 892 613 472 247 489 901 930 830 635 936 907 110 645 512 773 431 155 75 509 27 865 690 888 362 644 664 810 769 1 522 573 377 69 652 157 737 621 98 942 92 116 52 883 842 683 ...

result:

ok ok, up to 1267 steps were used

Test #14:

score: 0
Accepted
time: 15ms
memory: 4156kb

input:

1
1000
115 372 488 59 513 596 934 677 862 54 696 855 901 825 265 417 899 693 490 203 536 735 402 875 289 510 793 522 199 695 25 726 710 422 137 572 274 881 374 755 163 295 343 219 888 205 597 454 806 39 782 138 889 344 858 160 7 72 185 815 820 830 968 795 691 217 271 772 646 546 840 998 520 670 777 ...

output:

1108
8 515 242 980 560 977 352 578 717
2 207 948
2 329 249
3 848 358 799
2 489 661
2 814 982
2 43 505
1 251
2 965 930
2 41 795
3 655 526 940
3 56 388 981
3 709 996 956
2 276 889
2 44 887
3 61 913 571
2 903 978
2 591 133
3 606 377 878
2 771 736
3 273 14 325
3 705 998 924
2 606 946
2 965 736
3 771 448...

result:

ok ok, up to 1108 steps were used

Test #15:

score: 0
Accepted
time: 12ms
memory: 4112kb

input:

1
1000
717 305 986 429 917 628 985 204 341 749 852 307 820 711 484 562 866 473 800 40 241 127 71 565 310 808 480 925 987 250 755 125 848 535 479 295 383 188 375 640 646 466 842 813 752 203 704 831 919 315 994 817 676 386 157 830 374 355 795 60 995 758 814 745 778 681 964 1 279 512 636 862 665 298 68...

output:

1091
1 154
2 906 954
2 198 982
2 154 877
2 906 5
2 807 25
1 850
2 154 993
2 307 6
1 987
2 991 952
1 152
1 976
2 906 481
1 535
2 416 958
1 850
2 14 22
2 110 750
2 216 997
1 110
2 597 3
2 154 965
2 303 20
1 835
2 850 989
2 285 71
1 154
2 906 23
2 4 26
1 154
2 164 35
1 185
1 154
2 535 49
2 618 2
1 900
...

result:

ok ok, up to 1091 steps were used

Test #16:

score: 0
Accepted
time: 10ms
memory: 4328kb

input:

1
1000
310 363 446 303 748 979 8 485 694 948 702 771 36 191 666 891 559 413 845 895 827 67 498 658 971 868 473 674 913 421 65 377 692 536 802 467 237 292 22 967 732 660 275 490 690 672 128 577 833 107 810 353 392 958 824 15 158 243 901 991 717 344 904 565 350 881 641 512 898 159 633 999 422 501 528 ...

output:

1102
1 707
1 417
1 599
1 778
1 126
1 92
1 576
1 172
1 763
1 124
1 785
1 409
1 649
1 481
1 984
1 381
1 60
1 424
1 198
1 285
1 828
1 825
1 311
1 855
1 890
1 133
1 675
1 819
1 444
1 544
1 306
1 814
1 298
1 698
1 955
1 667
1 852
1 431
1 590
1 404
1 630
2 176 993
1 129
1 663
1 630
2 676 34
1 152
1 120
1 ...

result:

ok ok, up to 1102 steps were used

Test #17:

score: 0
Accepted
time: 10ms
memory: 4248kb

input:

1
1000
77 278 871 920 284 552 661 728 2 158 428 62 898 909 18 496 195 754 63 957 946 772 703 82 394 806 701 197 941 758 665 573 982 308 736 389 423 616 416 530 874 618 494 436 322 939 447 631 391 136 208 985 681 424 529 393 595 19 841 30 461 605 440 132 587 825 848 869 107 512 364 169 872 679 849 75...

output:

1005
1 880
1 181
1 732
1 155
1 201
1 493
1 39
1 663
1 242
1 425
1 696
1 728
1 394
1 642
1 436
1 5
1 123
1 483
1 589
1 395
1 667
1 579
1 513
1 423
1 411
1 994
1 817
1 958
1 275
1 424
1 28
1 941
1 264
1 568
1 621
1 901
1 646
1 252
1 991
1 778
1 509
1 12
1 928
1 286
1 600
1 407
1 859
1 295
1 753
1 391
...

result:

ok ok, up to 1005 steps were used

Test #18:

score: 0
Accepted
time: 28ms
memory: 4432kb

input:

1
999
977 98 254 594 144 861 64 706 322 185 959 376 323 840 42 799 653 762 698 150 162 58 530 189 649 112 430 341 515 303 837 657 344 232 290 867 137 352 716 755 488 15 919 250 49 597 127 420 45 693 314 384 170 745 536 312 178 22 141 79 266 84 80 7 545 210 616 748 843 225 487 588 418 229 414 689 575...

output:

1449
100 739 916 373 303 557 686 628 544 656 572 77 648 359 865 722 154 426 746 738 198 667 273 479 883 268 481 357 786 491 193 79 249 693 197 325 311 511 162 170 795 265 779 747 676 846 789 344 617 972 20 893 396 385 535 183 603 814 174 560 116 675 400 65 31 127 423 176 809 740 360 137 358 150 788 ...

result:

ok ok, up to 1449 steps were used

Test #19:

score: 0
Accepted
time: 34ms
memory: 4904kb

input:

1
999
266 532 27 286 491 460 989 270 745 828 710 910 428 608 178 855 803 851 719 938 627 684 584 661 547 827 3 641 771 223 567 241 275 476 933 417 287 705 545 272 904 572 747 92 589 168 384 613 928 237 936 728 870 349 724 183 617 624 626 240 508 354 538 578 133 190 477 787 308 120 323 590 798 664 11...

output:

1318
157 613 214 714 637 85 313 980 617 816 744 101 589 397 676 12 399 395 578 778 986 689 531 593 165 854 385 765 957 52 696 631 357 86 176 124 527 638 329 731 306 506 972 911 310 876 104 384 549 460 847 131 40 455 182 828 968 396 781 207 181 261 897 623 829 644 919 754 48 697 443 517 796 61 376 87...

result:

ok ok, up to 1318 steps were used

Test #20:

score: 0
Accepted
time: 53ms
memory: 5776kb

input:

1
999
32 791 286 948 176 560 246 200 367 339 179 369 474 328 772 886 982 954 604 262 944 602 491 965 585 408 970 638 686 896 769 1 873 703 444 658 348 96 891 514 118 78 52 904 469 851 956 468 996 299 64 43 311 539 738 927 788 224 959 79 141 940 967 51 376 244 343 350 379 949 421 844 382 775 465 414 ...

output:

1976
5 833 452 312 954 670
5 746 513 7 756 141
5 380 358 312 374 700
5 746 694 7 954 626
6 380 394 312 374 756 739
7 746 694 377 7 954 700 971
8 380 394 170 312 374 756 626 924
9 746 694 377 451 7 954 700 739 487
10 380 394 170 30 312 374 756 626 971 253
10 746 377 451 89 7 954 700 739 924 286
11 38...

result:

ok ok, up to 1976 steps were used

Test #21:

score: 0
Accepted
time: 129ms
memory: 14556kb

input:

1
999
162 333 835 598 275 157 150 732 33 26 263 815 589 327 515 530 581 239 565 251 126 705 855 155 382 10 406 185 599 802 445 661 9 600 74 574 163 952 474 462 340 125 250 137 992 660 727 407 486 226 612 287 658 945 82 399 416 939 171 838 120 644 194 114 752 831 351 413 96 316 317 946 109 35 318 692...

output:

2712
1 555
1 316
2 555 620
3 316 482 185
3 555 683 192
4 316 482 1 703
5 555 683 885 620 955
5 316 1 229 185 565
6 555 885 605 620 192 121
7 316 482 229 597 185 703 136
7 555 683 605 797 192 955 168
8 316 482 1 597 993 703 565 123
9 555 683 885 797 688 620 955 121 925
9 316 1 229 993 296 185 565 136...

result:

ok ok, up to 2712 steps were used

Test #22:

score: 0
Accepted
time: 161ms
memory: 15052kb

input:

1
999
709 23 572 330 7 668 5 576 217 663 840 634 47 731 182 724 413 574 38 225 459 739 2 609 234 281 537 76 718 403 800 529 979 913 75 661 134 19 83 445 90 810 248 583 348 478 13 695 392 623 894 550 631 523 783 373 111 431 425 652 487 253 719 98 854 711 130 588 686 159 903 753 836 222 35 28 749 817 ...

output:

2968
1 809
1 212
2 809 186
3 212 798 72
3 809 99 752
4 212 798 362 969
5 809 99 55 186 650
5 212 362 732 72 947
6 809 55 344 186 752 804
7 212 798 732 836 72 969 591
7 809 99 344 754 752 650 272
8 212 798 362 836 525 969 947 314
9 809 99 55 754 851 186 650 804 826
9 212 362 732 525 169 72 947 591 95...

result:

ok ok, up to 2968 steps were used

Test #23:

score: 0
Accepted
time: 16ms
memory: 4440kb

input:

1
999
719 626 485 972 276 492 226 237 77 102 843 188 386 650 123 976 24 861 233 391 973 29 543 17 172 468 927 588 22 141 119 566 98 700 769 282 271 432 956 995 692 837 308 848 180 216 442 482 451 793 913 942 362 463 729 64 983 965 986 259 345 890 534 56 152 450 266 647 300 326 759 900 254 929 786 35...

output:

1162
7 635 537 479 909 788 85 839
3 338 11 987
2 14 884
3 726 537 992
3 192 30 237
3 273 975 842
2 157 923
1 273
2 13 20
2 726 110
3 14 147 237
3 851 537 506
3 195 63 978
2 691 654
3 14 916 209
3 405 537 997
3 190 115 98
3 693 954 969
2 338 156
2 744 884
3 14 164 941
2 64 537
3 727 152 855
2 14 994
...

result:

ok ok, up to 1162 steps were used

Test #24:

score: 0
Accepted
time: 12ms
memory: 4136kb

input:

1
999
759 723 437 606 248 905 512 502 912 612 469 81 971 736 446 481 888 903 672 823 229 900 959 148 532 220 225 849 241 943 88 263 810 228 915 45 934 167 89 879 782 859 315 426 36 252 451 142 604 894 910 549 677 561 57 476 55 427 682 652 120 352 779 292 289 815 996 590 923 435 796 366 855 181 507 9...

output:

1106
2 540 995
1 944
1 426
2 190 896
2 500 965
2 190 987
2 44 17
1 190
2 172 36
2 190 980
2 785 77
1 190
2 381 86
2 682 934
2 586 897
2 678 904
2 899 991
2 426 959
2 944 2
2 9 456
1 500
2 944 7
2 19 3
2 468 906
2 341 996
2 994 742
2 500 466
2 632 24
2 190 5
2 500 155
2 632 28
2 426 6
2 500 63
2 249 ...

result:

ok ok, up to 1106 steps were used

Test #25:

score: 0
Accepted
time: 14ms
memory: 3992kb

input:

1
999
920 435 55 749 629 95 418 187 971 806 239 760 641 396 219 47 777 413 500 229 271 182 571 754 162 624 935 790 755 646 739 447 341 949 485 236 193 363 315 633 463 656 852 129 126 696 16 710 961 542 59 156 483 841 3 246 992 972 51 947 651 969 405 516 85 821 71 487 848 98 67 857 709 281 895 825 14...

output:

1406
2 369 964
1 989
1 194
1 989
2 1 990
1 186
1 1
1 989
1 599
1 989
2 2 15
1 628
1 2
1 989
1 718
1 989
2 3 16
1 717
1 3
1 989
1 735
1 989
2 4 28
1 410
1 4
1 989
1 501
1 989
2 5 36
1 732
1 5
1 989
1 714
1 989
2 6 65
1 919
1 6
1 989
1 255
1 989
2 7 85
1 989
2 519 89
1 989
2 631 91
1 989
2 252 108
1 9...

result:

ok ok, up to 1406 steps were used

Test #26:

score: 0
Accepted
time: 14ms
memory: 4176kb

input:

1
999
311 242 610 99 258 592 164 715 179 45 161 139 499 398 738 170 404 234 445 395 63 33 968 301 323 568 938 447 721 855 584 645 22 601 300 710 279 923 700 265 380 489 853 613 10 297 166 574 324 278 152 407 263 187 117 798 340 470 202 221 411 622 21 876 820 659 261 885 405 905 831 289 126 237 825 2...

output:

1495
1 467
1 998
1 622
1 998
1 1
1 634
1 1
1 2
1 129
1 2
1 3
1 465
1 3
1 4
1 868
1 4
1 5
1 865
1 5
1 6
1 349
1 6
1 7
1 505
1 7
1 8
1 158
1 8
1 9
1 274
1 9
1 10
1 141
1 10
1 11
1 510
1 11
1 12
1 960
1 12
1 13
1 764
1 13
1 14
1 788
1 14
1 15
1 961
1 15
1 16
1 371
1 16
1 17
1 836
1 17
1 18
1 721
1 18
1...

result:

ok ok, up to 1495 steps were used

Test #27:

score: 0
Accepted
time: 23ms
memory: 4488kb

input:

1
998
948 460 335 899 872 723 754 646 789 772 119 382 651 383 942 755 774 997 446 970 867 949 471 67 935 391 875 632 727 992 366 915 369 88 207 889 918 511 526 138 792 384 967 388 162 661 179 375 304 897 559 368 688 907 267 531 787 573 817 943 530 278 677 814 327 850 24 606 339 37 435 578 75 169 73 ...

output:

1142
146 264 336 663 380 894 914 652 948 462 807 540 717 494 753 45 514 121 995 844 883 466 492 939 747 472 181 713 699 236 174 707 69 926 311 314 783 198 478 71 344 298 435 888 642 343 235 406 285 619 728 370 799 55 81 453 488 816 1 296 49 817 103 935 852 331 702 133 874 983 368 30 806 812 471 654 ...

result:

ok ok, up to 1142 steps were used

Test #28:

score: 0
Accepted
time: 36ms
memory: 4524kb

input:

1
998
494 391 49 950 498 165 692 300 510 673 931 837 954 352 465 197 719 959 328 896 303 198 461 729 298 887 437 549 445 696 457 955 378 552 765 73 180 383 103 980 321 852 194 345 105 513 471 538 3 527 878 708 732 148 913 833 806 894 111 897 816 790 828 947 858 604 388 142 517 455 367 632 36 240 396...

output:

1342
123 755 417 711 577 937 127 153 773 315 948 58 893 64 469 783 188 163 485 729 289 182 310 175 858 557 103 518 655 171 880 550 671 235 551 206 611 811 583 61 356 597 91 788 827 330 265 217 683 628 486 699 731 801 659 599 18 793 355 698 305 605 544 876 60 584 627 921 285 742 857 401 739 383 291 4...

result:

ok ok, up to 1342 steps were used

Test #29:

score: 0
Accepted
time: 51ms
memory: 5684kb

input:

1
998
765 850 400 698 96 680 434 963 388 616 398 505 511 25 611 887 550 641 153 736 743 946 908 659 14 595 984 939 332 937 373 792 787 251 150 617 767 809 990 477 959 953 380 358 789 462 987 348 227 929 931 168 510 452 823 387 627 614 62 466 529 59 449 410 711 758 649 158 541 403 531 475 968 795 412...

output:

1613
4 876 196 974 357
3 267 851 281
3 575 8 535
4 267 196 913 10
5 86 817 851 84 698
5 267 853 8 924 705
6 575 817 386 913 324 919
7 267 853 474 196 84 258 243
8 575 817 386 989 851 924 496 245
9 267 853 474 378 196 8 324 573 72
9 575 817 386 989 461 851 913 258 598
10 267 853 474 378 640 196 8 84 ...

result:

ok ok, up to 1613 steps were used

Test #30:

score: 0
Accepted
time: 136ms
memory: 13052kb

input:

1
998
627 431 727 497 487 126 684 838 644 15 516 818 127 44 10 657 823 189 63 115 939 963 873 607 346 706 608 79 210 461 392 243 454 227 647 565 774 851 478 625 141 910 177 14 239 317 498 543 457 294 893 531 132 234 492 804 92 585 908 116 678 674 19 583 856 403 595 688 554 797 223 596 789 295 438 24...

output:

2695
9 399 197 678 993 282 591 682 512 973
8 740 334 742 542 736 690 312 139
8 450 100 432 471 790 94 721 521
9 740 117 916 78 50 791 55 482 725
10 450 460 413 47 617 252 590 107 671 446
10 740 829 677 969 145 72 630 541 25 30
11 450 460 303 230 839 665 243 997 559 988 131
12 740 829 263 602 497 626...

result:

ok ok, up to 2695 steps were used

Test #31:

score: 0
Accepted
time: 157ms
memory: 14660kb

input:

1
998
733 959 347 569 526 485 632 134 887 320 369 825 92 463 536 915 808 805 589 982 537 826 812 336 647 686 638 884 36 841 243 429 687 855 483 29 293 140 892 280 94 750 682 816 394 791 676 186 913 339 766 945 655 960 718 480 866 974 85 121 849 188 206 933 604 517 820 553 476 313 370 420 964 193 528...

output:

2955
7 640 511 141 450 332 531 964
8 898 922 378 822 451 566 803 525
8 640 469 901 586 605 290 416 251
9 898 922 25 485 676 494 618 82 146
10 640 469 503 984 7 302 203 198 258 161
10 898 25 930 375 153 919 514 195 564 32
11 640 503 925 51 37 603 203 332 313 284 399
12 898 922 930 629 596 54 100 514 ...

result:

ok ok, up to 2955 steps were used

Test #32:

score: 0
Accepted
time: 15ms
memory: 4388kb

input:

1
998
238 368 91 13 760 72 419 694 981 177 883 447 4 329 902 810 141 624 473 919 916 507 778 923 701 863 657 812 200 832 256 605 472 974 301 328 515 304 481 62 176 297 891 345 661 769 889 595 488 422 849 276 954 644 897 708 960 333 236 240 326 40 485 564 782 896 79 197 187 869 136 6 830 828 60 788 4...

output:

1095
6 596 580 378 823 433 841
4 264 768 718 938
3 7 835 988
2 413 802
2 501 433
3 596 724 199
3 632 768 64
3 559 101 661
2 596 994
2 953 768
3 198 108 427
3 372 838 114
3 198 893 531
3 756 838 455
3 986 63 618
3 127 725 206
3 596 915 837
2 194 768
3 372 122 349
3 389 531 967
2 372 929
2 127 5
2 632...

result:

ok ok, up to 1095 steps were used

Test #33:

score: 0
Accepted
time: 12ms
memory: 4056kb

input:

1
998
657 220 904 795 418 211 522 176 489 271 647 237 700 935 594 194 535 136 835 286 259 838 858 551 906 623 502 154 508 141 742 797 863 682 500 920 299 899 850 642 68 495 689 282 656 735 745 796 181 593 894 929 783 723 713 414 260 314 354 668 563 193 727 768 845 996 625 41 376 296 983 235 677 840 ...

output:

1053
2 489 982
2 212 807
2 442 965
2 368 993
2 770 779
2 442 989
2 905 6
1 442
2 672 13
2 866 997
2 561 471
2 262 28
2 368 995
2 262 165
2 656 20
1 262
2 672 32
2 38 1
2 143 990
2 716 955
1 143
2 561 57
2 143 123
2 788 60
1 143
2 138 110
2 442 991
2 38 40
2 672 5
2 38 2
2 770 11
2 294 49
2 944 765
2...

result:

ok ok, up to 1053 steps were used

Test #34:

score: 0
Accepted
time: 14ms
memory: 4112kb

input:

1
998
571 670 211 487 704 982 672 742 89 547 852 953 925 808 256 92 828 807 288 236 815 570 430 408 434 407 426 447 763 310 694 720 706 870 36 35 61 622 479 791 46 208 944 933 389 41 417 514 504 712 106 543 343 143 992 134 793 891 613 630 37 234 616 93 589 887 212 544 345 477 945 113 866 278 853 814...

output:

1399
2 873 954
1 185
1 369
1 185
2 1 4
1 480
1 1
1 185
1 123
1 185
2 2 13
1 200
1 2
1 185
1 69
1 185
2 3 14
1 132
1 3
1 185
1 203
1 185
2 5 19
1 105
1 5
1 185
1 534
1 185
2 6 21
1 679
1 6
1 185
1 545
1 185
2 7 22
1 185
2 995 38
1 185
2 161 44
1 185
2 137 54
1 185
2 635 56
1 185
2 492 60
1 185
2 522 ...

result:

ok ok, up to 1399 steps were used

Test #35:

score: 0
Accepted
time: 14ms
memory: 4092kb

input:

1
998
648 130 748 548 766 651 270 645 672 196 25 141 588 538 622 530 658 580 763 359 794 476 341 579 11 614 893 255 703 612 569 290 232 446 393 323 535 371 821 870 281 157 518 148 325 619 986 138 434 807 923 626 714 250 239 209 620 309 945 829 347 360 118 173 367 693 571 957 599 568 725 973 488 936 ...

output:

1488
1 67
1 997
1 389
1 997
1 1
1 763
1 1
1 2
1 643
1 2
1 3
1 237
1 3
1 4
1 449
1 4
1 5
1 614
1 5
1 6
1 809
1 6
1 7
1 117
1 7
1 8
1 118
1 8
1 9
1 314
1 9
1 10
1 292
1 10
1 11
1 432
1 11
1 12
1 259
1 12
1 13
1 485
1 13
1 14
1 887
1 14
1 15
1 499
1 15
1 16
1 724
1 16
1 17
1 426
1 17
1 18
1 113
1 18
1 ...

result:

ok ok, up to 1488 steps were used

Test #36:

score: 0
Accepted
time: 0ms
memory: 3768kb

input:

1
1
1

output:

0

result:

ok ok, up to 0 steps were used

Test #37:

score: 0
Accepted
time: 56ms
memory: 4100kb

input:

400
48
43 47 8 20 36 13 35 3 23 34 15 48 6 31 11 44 27 33 37 4 46 40 9 28 29 41 17 24 25 32 14 30 18 10 7 5 19 45 42 22 26 39 1 16 38 21 2 12
10 29
29 17
13 3
42 26
5 26
37 41
6 40
16 19
12 26
38 23
7 39
11 33
10 39
43 39
20 10
35 18
45 14
5 21
34 6
38 39
8 45
26 25
9 12
33 15
37 9
45 3
48 1
47 21
3...

output:

52
9 36 13 33 47 27 9 41 3 24
5 1 20 7 5 23
5 18 13 10 9 39
5 43 20 5 23 42
4 31 34 9 39
5 43 36 23 8 46
4 18 34 1 39
4 43 36 13 5
5 18 34 1 11 9
5 43 36 13 5 23
6 31 34 1 45 9 40
5 29 36 13 23 26
4 43 1 16 17
3 18 34 35
4 43 36 33 5
4 29 34 1 9
3 31 36 35
3 43 1 26
3 18 34 15
3 43 36 5
4 31 34 1 4
...

result:

ok ok, up to 130 steps were used

Test #38:

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

input:

3
1
1
986
986 985 984 983 982 981 980 979 978 977 976 975 974 973 972 971 970 969 968 967 966 965 964 963 962 961 960 959 958 957 956 955 954 953 952 951 950 949 948 947 946 945 944 943 942 941 940 939 938 937 936 935 934 933 932 931 930 929 928 927 926 925 924 923 922 921 920 919 918 917 916 915 91...

output:

0
1796
5 1 379 469 491 497
8 2 610 380 522 470 502 492 498
8 1 611 379 523 469 503 491 499
12 2 610 612 380 522 524 470 502 504 492 498 500
15 1 611 613 3 379 523 525 381 469 503 505 471 491 499 493
14 2 612 614 4 380 524 526 382 470 504 506 472 492 494
17 1 613 615 3 5 379 525 527 381 383 469 505 5...

result:

ok ok, up to 1796 steps were used

Test #39:

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

input:

3
1
1
986
986 985 984 983 982 981 980 979 978 977 976 975 974 973 972 971 970 969 968 967 966 965 964 963 962 961 960 959 958 957 956 955 954 953 952 951 950 949 948 947 946 945 944 943 942 941 940 939 938 937 936 935 934 933 932 931 930 929 928 927 926 925 924 923 922 921 920 919 918 917 916 915 91...

output:

0
1796
5 1 379 469 491 497
8 2 610 380 522 470 502 492 498
8 1 611 379 523 469 503 491 499
12 2 610 612 380 522 524 470 502 504 492 498 500
15 1 611 613 3 379 523 525 381 469 503 505 471 491 499 493
14 2 612 614 4 380 524 526 382 470 504 506 472 492 494
17 1 613 615 3 5 379 525 527 381 383 469 505 5...

result:

ok ok, up to 1796 steps were used

Test #40:

score: 0
Accepted
time: 32ms
memory: 4632kb

input:

3
1
1
986
973 974 972 975 970 971 976 967 968 966 969 977 962 963 961 964 959 960 965 978 954 955 953 956 951 952 957 948 949 947 950 958 979 941 942 940 943 938 939 944 935 936 934 937 945 930 931 929 932 927 928 933 946 980 920 921 919 922 917 918 923 914 915 913 916 924 909 910 908 911 906 907 91...

output:

0
1439
4 610 522 502 498
8 1 611 379 523 469 503 491 499
12 2 610 612 380 522 524 470 502 504 492 498 500
15 1 611 613 3 379 523 525 381 469 503 505 471 491 499 493
18 2 610 612 614 4 380 522 524 526 382 470 502 504 506 472 492 498 494
21 1 611 613 615 3 5 379 523 525 527 381 383 469 503 505 507 471...

result:

ok ok, up to 1439 steps were used

Test #41:

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

input:

3
1
1
986
986 985 983 984 981 980 982 978 977 975 976 979 973 972 970 971 968 967 969 974 965 964 962 963 960 959 961 957 956 954 955 958 966 952 951 949 950 947 946 948 944 943 941 942 945 939 938 936 937 934 933 935 940 953 931 930 928 929 926 925 927 923 922 920 921 924 918 917 915 916 913 912 91...

output:

0
1821
2 1 388
2 610 387
2 611 386
2 612 385
2 613 384
2 614 383
2 615 382
2 616 381
2 617 380
2 618 379
2 378 619
3 1 620 474
4 2 621 473 475
4 1 3 472 474
5 2 610 4 471 473
5 1 611 5 470 472
6 2 610 612 6 469 471
7 1 611 613 3 7 468 470
7 2 612 614 4 8 379 469
7 1 613 615 3 5 9 380
9 2 610 614 616...

result:

ok ok, up to 1821 steps were used

Test #42:

score: 0
Accepted
time: 4ms
memory: 4216kb

input:

3
1
1
986
2 1 7 3 6 5 4 20 8 11 10 9 19 13 12 18 14 17 16 15 54 21 24 23 22 32 26 25 31 27 30 29 28 53 34 33 39 35 38 37 36 52 40 43 42 41 51 45 44 50 46 49 48 47 143 55 58 57 56 66 60 59 65 61 64 63 62 87 68 67 73 69 72 71 70 86 74 77 76 75 85 79 78 84 80 83 82 81 142 89 88 94 90 93 92 91 107 95 98...

output:

0
107
2 609 376
3 606 371 377
3 598 358 376
3 577 324 371
3 522 235 358
2 2 324
2 1 235
1 2
5 378 145 144 143 232
7 1 465 520 140 146 231 233
10 753 842 464 466 515 521 127 141 223 232
12 748 754 839 456 465 502 520 93 140 142 202 231
11 735 753 831 435 464 515 4 127 141 223 234
10 701 748 810 380 4...

result:

ok ok, up to 107 steps were used

Test #43:

score: 0
Accepted
time: 39ms
memory: 4312kb

input:

3
1
1
986
986 985 984 983 982 981 980 979 978 977 976 975 974 973 972 971 970 969 968 967 966 965 964 963 962 961 960 959 958 957 956 955 954 953 952 951 950 949 948 947 946 945 944 943 942 941 940 939 938 937 936 935 934 933 932 931 930 929 928 927 926 925 924 923 922 921 920 919 918 917 916 915 91...

output:

0
1790
1 387
2 386 388
2 385 387
2 384 386
2 383 385
2 382 384
2 381 383
2 380 382
2 379 381
2 378 380
2 1 379
1 2
2 1 3
3 378 610 4
3 1 611 5
4 2 610 612 6
5 1 611 613 3 7
6 2 610 612 614 4 8
7 1 611 613 615 3 5 9
8 2 610 612 614 616 4 6 10
9 1 611 613 615 617 3 5 7 11
10 2 610 612 614 616 618 4 6 ...

result:

ok ok, up to 1790 steps were used

Test #44:

score: 0
Accepted
time: 31ms
memory: 4628kb

input:

3
1
1
986
986 985 984 983 982 981 980 979 978 977 976 975 974 973 972 971 970 969 968 967 966 965 964 963 962 961 960 959 958 957 956 955 954 953 952 951 950 949 948 947 946 945 944 943 942 941 940 939 938 937 936 935 934 933 932 931 930 929 928 927 926 925 924 923 922 921 920 919 918 917 916 915 91...

output:

0
1424
3 610 609 608
3 1 611 606
5 2 610 612 598 607
6 1 611 613 577 606 3
7 2 610 612 614 522 598 4
7 1 611 613 615 577 3 5
7 2 610 612 614 616 4 6
8 1 611 613 615 617 3 5 7
9 2 610 612 614 616 618 4 6 8
10 1 611 613 615 617 619 3 5 7 9
11 2 610 612 614 616 618 620 4 6 8 10
12 1 611 613 615 617 619...

result:

ok ok, up to 1424 steps were used

Test #45:

score: 0
Accepted
time: 10ms
memory: 4100kb

input:

1
1000
996 43 577 950 687 195 887 640 567 684 142 595 290 471 322 5 885 318 272 246 98 607 931 578 718 867 45 676 655 616 650 222 367 615 31 385 203 834 488 437 710 603 804 392 713 399 311 70 847 617 513 411 575 695 347 431 463 216 77 306 295 144 566 475 270 933 897 892 706 954 526 440 240 919 974 7...

output:

667
334 5 97 863 32 341 712 26 246 895 53 345 516 941 153 618 463 766 121 467 135 541 737 227 216 955 694 819 65 13 278 141 442 588 985 499 791 200 441 509 462 1 136 765 816 92 779 682 515 389 296 698 714 750 337 69 806 747 275 981 402 255 931 224 728 822 143 348 631 794 843 446 440 448 110 833 906 ...

result:

ok ok, up to 667 steps were used

Test #46:

score: 0
Accepted
time: 10ms
memory: 4004kb

input:

1
1000
107 95 790 410 138 13 486 51 36 3 226 569 961 785 273 822 628 927 699 802 255 856 441 499 246 751 534 187 257 935 505 562 575 217 567 767 351 315 178 476 532 144 182 111 511 112 479 830 70 834 551 266 69 636 501 541 128 504 125 708 278 969 97 872 52 945 538 949 784 821 341 168 232 988 154 437...

output:

668
1 999
1 35
1 146
2 473 874
1 167
2 527 628
1 439
2 723 41
1 531
2 954 532
1 325
2 909 901
1 195
2 194 847
1 578
2 822 485
1 973
2 518 995
1 792
2 3 932
1 36
2 965 223
1 313
2 951 328
1 225
2 766 414
1 760
2 922 60
1 119
2 171 462
1 443
2 921 63
1 425
2 71 544
1 34
2 143 761
1 274
2 466 824
1 691...

result:

ok ok, up to 668 steps were used

Test #47:

score: 0
Accepted
time: 10ms
memory: 4088kb

input:

1
1000
657 131 1 655 428 7 948 767 429 296 26 667 74 3 804 399 782 796 260 295 719 724 378 56 708 570 442 500 896 403 861 937 833 616 379 812 95 457 122 293 210 917 162 342 22 189 963 949 14 766 933 847 199 117 911 612 424 274 791 6 671 715 805 564 718 542 928 416 11 314 315 891 494 906 120 829 173 ...

output:

668
1 3
1 699
1 12
2 935 385
1 881
2 930 460
1 430
2 520 17
1 761
2 606 746
1 992
2 869 406
1 897
2 289 90
1 631
2 982 140
1 954
2 395 599
1 18
2 888 846
1 158
2 802 336
1 52
2 487 428
1 615
2 195 149
1 892
2 176 472
1 969
2 207 947
1 534
2 598 525
1 961
2 622 649
1 605
2 659 865
1 526
2 751 595
1 1...

result:

ok ok, up to 668 steps were used

Test #48:

score: 0
Accepted
time: 10ms
memory: 4004kb

input:

1
1000
291 378 38 938 62 810 402 752 333 22 916 737 852 709 865 667 686 954 716 419 876 191 853 829 841 904 296 471 387 151 823 167 7 455 509 719 325 4 893 235 570 204 206 196 911 16 366 638 303 903 885 414 948 102 715 50 334 593 444 774 635 819 362 990 642 730 541 65 901 493 271 377 857 170 586 252...

output:

667
334 2 862 303 286 924 402 270 725 44 209 910 361 780 833 264 32 702 830 817 629 534 175 104 400 195 682 314 521 654 642 757 417 951 649 326 113 430 867 957 82 225 105 357 715 485 61 232 630 716 754 539 440 260 340 25 358 372 639 333 518 227 123 962 87 905 551 766 895 972 21 287 676 88 394 644 96...

result:

ok ok, up to 667 steps were used

Extra Test:

score: 0
Extra Test Passed