#line 2 "library/other/template.hpp" #include #line 2 "library/template/macros.hpp" #line 4 "library/template/macros.hpp" #ifndef __COUNTER__ #define __COUNTER__ __LINE__ #endif #define REP_SELECTER(a, b, c, d, e, ...) e #define REP1_0(b, c) REP1_1(b, c) #define REP1_1(b, c) \ for (ll REP_COUNTER_##c = 0; REP_COUNTER_##c < (ll)(b); ++REP_COUNTER_##c) #define REP1(b) REP1_0(b, __COUNTER__) #define REP2(i, b) for (ll i = 0; i < (ll)(b); ++i) #define REP3(i, a, b) for (ll i = (ll)(a); i < (ll)(b); ++i) #define REP4(i, a, b, c) for (ll i = (ll)(a); i < (ll)(b); i += (ll)(c)) #define rep(...) REP_SELECTER(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__) #define RREP2(i, a) for (ll i = (ll)(a)-1; i >= 0; --i) #define RREP3(i, a, b) for (ll i = (ll)(a)-1; i >= (ll)(b); --i) #define RREP4(i, a, b, c) for (ll i = (ll)(a)-1; i >= (ll)(b); i -= (ll)(c)) #define rrep(...) REP_SELECTER(__VA_ARGS__, RREP4, RREP3, RREP2)(__VA_ARGS__) #define REPS2(i, b) for (ll i = 1; i <= (ll)(b); ++i) #define REPS3(i, a, b) for (ll i = (ll)(a) + 1; i <= (ll)(b); ++i) #define REPS4(i, a, b, c) for (ll i = (ll)(a) + 1; i <= (ll)(b); i += (ll)(c)) #define reps(...) REP_SELECTER(__VA_ARGS__, REPS4, REPS3, REPS2)(__VA_ARGS__) #define RREPS2(i, a) for (ll i = (ll)(a); i > 0; --i) #define RREPS3(i, a, b) for (ll i = (ll)(a); i > (ll)(b); --i) #define RREPS4(i, a, b, c) for (ll i = (ll)(a); i > (ll)(b); i -= (ll)(c)) #define rreps(...) \ REP_SELECTER(__VA_ARGS__, RREPS4, RREPS3, RREPS2)(__VA_ARGS__) #define each_for(...) for (auto&& __VA_ARGS__) #define each_const(...) for (const auto& __VA_ARGS__) #define all(v) std::begin(v), std::end(v) #if __cplusplus >= 201402L #define rall(v) std::rbegin(v), std::rend(v) #else #define rall(v) v.rbegin(), v.rend() #endif #if __cpp_constexpr >= 201304L #define CONSTEXPR constexpr #else #define CONSTEXPR #endif #if __cpp_if_constexpr >= 201606L #define IF_CONSTEXPR constexpr #else #define IF_CONSTEXPR #endif #define IO_BUFFER_SIZE 2048 #line 2 "library/template/alias.hpp" #line 4 "library/template/alias.hpp" using ll = long long; using ull = unsigned long long; using ld = long double; using PLL = std::pair; template using prique = std::priority_queue, std::greater>; template class infinity { public: static constexpr T value = std::numeric_limits::max() / 2; static constexpr T mvalue = std::numeric_limits::min() / 2; static constexpr T max = std::numeric_limits::max(); static constexpr T min = std::numeric_limits::min(); }; #if __cplusplus <= 201402L template constexpr T infinity::value; template constexpr T infinity::mvalue; template constexpr T infinity::max; template constexpr T infinity::min; #endif #if __cpp_variable_templates >= 201304L template constexpr T INF = infinity::value; #endif constexpr ll inf = infinity::value; constexpr ld EPS = 1e-8; constexpr ld PI = 3.1415926535897932384626; #line 2 "library/template/type_traits.hpp" #line 5 "library/template/type_traits.hpp" template struct function_traits_impl { using result_type = T; template using argument_type = typename std::tuple_element>::type; using argument_tuple = std::tuple; static constexpr std::size_t arg_size() { return sizeof...(Args); } }; template struct function_traits_helper; template struct function_traits_helper { using type = function_traits_impl; }; template struct function_traits_helper { using type = function_traits_impl; }; template struct function_traits_helper { using type = function_traits_impl; }; template struct function_traits_helper { using type = function_traits_impl; }; #if __cpp_noexcept_function_type >= 201510L template struct function_traits_helper { using type = function_traits_impl; }; template struct function_traits_helper { using type = function_traits_impl; }; template struct function_traits_helper { using type = function_traits_impl; }; template struct function_traits_helper { using type = function_traits_impl; }; #endif template using function_traits = typename function_traits_helper::type; template using is_signed_int = std::disjunction, std::is_signed>, std::is_same>; template using is_unsigned_int = std::disjunction, std::is_unsigned>, std::is_same>; template using is_int = std::disjunction, is_unsigned_int>; template using make_signed_int = typename std::conditional< std::is_same::value || std::is_same::value, std::common_type<__int128_t>, std::make_signed>::type; template using make_unsigned_int = typename std::conditional< std::is_same::value || std::is_same::value, std::common_type<__uint128_t>, std::make_unsigned>::type; template struct is_range : std::false_type {}; template struct is_range< T, decltype(all(std::declval::type>()), (void)0)> : std::true_type {}; template::value> struct range_rank : std::integral_constant {}; template struct range_rank : std::integral_constant::value + 1> {}; template struct int_least { static_assert(size <= 128, "size must be less than or equal to 128"); using type = typename std::conditional< size <= 8, std::int_least8_t, typename std::conditional< size <= 16, std::int_least16_t, typename std::conditional< size <= 32, std::int_least32_t, typename std::conditional::type>::type>::type>:: type; }; template using int_least_t = typename int_least::type; template struct uint_least { static_assert(size <= 128, "size must be less than or equal to 128"); using type = typename std::conditional< size <= 8, std::uint_least8_t, typename std::conditional< size <= 16, std::uint_least16_t, typename std::conditional< size <= 32, std::uint_least32_t, typename std::conditional::type>::type>::type>:: type; }; template using uint_least_t = typename uint_least::type; template using double_size_int = int_least::digits * 2 + 1>; template using double_size_int_t = typename double_size_int::type; template using double_size_uint = uint_least::digits * 2>; template using double_size_uint_t = typename double_size_uint::type; template using double_size = typename std::conditional::value, double_size_int, double_size_uint>::type; template using double_size_t = typename double_size::type; #line 2 "library/template/in.hpp" #line 4 "library/template/in.hpp" #include #line 8 "library/template/in.hpp" template class Reader { private: int fd, idx, sz; bool state; std::array buffer; inline void read_buf() { sz = read(fd, buffer.begin(), buf_size); idx = 0; if (sz < 0) throw std::runtime_error("input failed"); } public: static constexpr int get_buf_size() { return buf_size; } Reader() noexcept : fd(0), idx(0), sz(0), state(true) {} Reader(int fd) noexcept : fd(fd), idx(0), sz(0), state(true) {} Reader(FILE* fp) noexcept : fd(fileno(fp)), idx(0), sz(0), state(true) {} class iterator { private: Reader* reader; public: using difference_type = void; using value_type = void; using pointer = void; using reference = void; using iterator_category = std::input_iterator_tag; iterator() : reader(nullptr) {} explicit iterator(Reader& reader) : reader(&reader) {} explicit iterator(Reader* reader) : reader(reader) {} iterator& operator++() { if (reader->idx == reader->sz) reader->read_buf(); ++reader->idx; return *this; } iterator operator++(int) { iterator res = *this; ++(*this); return res; } char operator*() const { if (reader->idx == reader->sz) reader->read_buf(); if (reader->idx < reader->sz) return reader->buffer[reader->idx]; reader->state = false; return '\0'; } bool rdstate() const { return reader->state; } }; iterator begin() noexcept { return iterator(this); } }; Reader<> reader(0); template class Scanner { public: using iterator_type = Iterator; private: template struct has_scan : std::false_type {}; template struct has_scan< T, decltype(std::declval().scan(std::declval()), (void)0)> : std::true_type {}; Iterator itr; public: Scanner() = default; Scanner(const Iterator& itr) : itr(itr) {} char scan_char() { char c = *itr; ++itr; return c; } Scanner ignore(int n = 1) { rep (n) ++itr; return *this; } inline void discard_space() { while (('\t' <= *itr && *itr <= '\r') || *itr == ' ') ++itr; } void scan(char& a) { discard_space(); a = *itr; ++itr; } void scan(bool& a) { discard_space(); a = *itr != '0'; ++itr; } void scan(std::string& a) { discard_space(); a.clear(); while ((*itr < '\t' || '\r' < *itr) && *itr != ' ' && *itr != '\0') { a += *itr; ++itr; } } template void scan(std::bitset& a) { discard_space(); rrep (i, len) { a[i] = *itr != '0'; ++itr; } } template::value && !has_scan::value>::type* = nullptr> void scan(T& a) { discard_space(); if (*itr == '-') { ++itr; a = 0; while ('0' <= *itr && *itr <= '9') { a = a * 10 - (*itr - '0'); ++itr; } } else { a = 0; while ('0' <= *itr && *itr <= '9') { a = a * 10 + (*itr - '0'); ++itr; } } } template::value && !has_scan::value>::type* = nullptr> void scan(T& a) { discard_space(); a = 0; while ('0' <= *itr && *itr <= '9') { a = a * 10 + *itr - '0'; ++itr; } } template::value && !has_scan::value>::type* = nullptr> void scan(T& a) { discard_space(); bool sgn = false; if (*itr == '-') { sgn = true; ++itr; } a = 0; while ('0' <= *itr && *itr <= '9') { a = a * 10 + *itr - '0'; ++itr; } if (*itr == '.') { ++itr; T n = 0, d = 1; for (int i = 0; '0' <= *itr && *itr <= '9' && i < (int)decimal_precision; ++i) { n = n * 10 + *itr - '0'; d *= 10; ++itr; } while ('0' <= *itr && *itr <= '9') ++itr; a += n / d; } if (sgn) a = -a; } private: template void scan(std::tuple& a) { if IF_CONSTEXPR (i < sizeof...(Args)) { scan(std::get(a)); scan(a); } } public: template void scan(std::tuple& a) { scan<0, Args...>(a); } template void scan(std::pair& a) { scan(a.first); scan(a.second); } template::value && !has_scan::value>::type* = nullptr> void scan(T& a) { each_for (i : a) scan(i); } template::value>::type* = nullptr> void scan(T& a) { a.scan(*this); } void operator()() {} template void operator()(Head& head, Args&... args) { scan(head); operator()(args...); } template Scanner& operator>>(T& a) { scan(a); return *this; } explicit operator bool() const { return itr.rdstate(); } }; Scanner::iterator> scan(reader.begin()); template Scanner& getline(Scanner& scan, std::string& a) { a.clear(); char c; while ((c = scan.scan_char()) != '\n') { a += c; } return scan; } #line 2 "library/template/out.hpp" #line 8 "library/template/out.hpp" template class Writer { private: int fd, idx; std::array buffer; inline void write_buf() { int num = write(fd, buffer.begin(), idx); idx = 0; if (num < 0) throw std::runtime_error("output failed"); } public: Writer() noexcept : fd(1), idx(0) {} Writer(int fd) noexcept : fd(fd), idx(0) {} Writer(FILE* fp) noexcept : fd(fileno(fp)), idx(0) {} ~Writer() { write_buf(); } class iterator { private: Writer* writer; public: using difference_type = void; using value_type = void; using pointer = void; using reference = void; using iterator_category = std::output_iterator_tag; iterator() noexcept : writer(nullptr) {} explicit iterator(Writer& writer) noexcept : writer(&writer) {} explicit iterator(Writer* writer) noexcept : writer(writer) {} iterator& operator++() { ++writer->idx; if (writer->idx == buf_size) writer->write_buf(); return *this; } iterator operator++(int) { iterator res = *this; ++(*this); return res; } char& operator*() const { return writer->buffer[writer->idx]; } void flush() const { writer->write_buf(); } }; iterator begin() noexcept { return iterator(this); } }; Writer<> writer(1), ewriter(2); template class Printer { public: using iterator_type = Iterator; private: template struct has_print : std::false_type {}; template struct has_print().print(std::declval()), (void)0)> : std::true_type {}; template struct has_print().debug(std::declval()), (void)0)> : std::true_type {}; Iterator itr; std::size_t decimal_precision; public: void print_char(char c) { *itr = c; ++itr; } void flush() { itr.flush(); } Printer() noexcept = default; explicit Printer(const Iterator& itr) noexcept : itr(itr), decimal_precision(16) {} void set_decimal_precision(std::size_t decimal_precision) { this->decimal_precision = decimal_precision; } void print(char c) { if IF_CONSTEXPR (debug) print_char('\''); print_char(c); if IF_CONSTEXPR (debug) print_char('\''); } void print(bool b) { print_char((char)(b + '0')); } void print(const char* a) { if IF_CONSTEXPR (debug) print_char('"'); for (; *a != '\0'; ++a) print_char(*a); if IF_CONSTEXPR (debug) print_char('"'); } template void print(const char (&a)[len]) { if IF_CONSTEXPR (debug) print_char('"'); for (auto i : a) print_char(i); if IF_CONSTEXPR (debug) print_char('"'); } void print(const std::string& a) { if IF_CONSTEXPR (debug) print_char('"'); for (auto i : a) print_char(i); if IF_CONSTEXPR (debug) print_char('"'); } template void print(const std::bitset& a) { rrep (i, len) print_char((char)(a[i] + '0')); } template::value && !has_print::value>::type* = nullptr> void print(T a) { if (!a) { print_char('0'); return; } if IF_CONSTEXPR (is_signed_int::value) { if (a < 0) { print_char('-'); using U = typename make_unsigned_int::type; print(static_cast(-static_cast(a))); return; } } std::string s; while (a) { s += (char)(a % 10 + '0'); a /= 10; } for (auto i = s.rbegin(); i != s.rend(); ++i) print_char(*i); } template::value && !has_print::value>::type* = nullptr> void print(T a) { if (a == std::numeric_limits::infinity()) { print("inf"); return; } if (a == -std::numeric_limits::infinity()) { print("-inf"); return; } if (std::isnan(a)) { print("nan"); return; } if (a < 0) { print_char('-'); a = -a; } T b = a; if (b < 1) { print_char('0'); } else { std::string s; while (b >= 1) { s += (char)('0' + (int)std::fmod(b, 10.0)); b /= 10; } for (auto i = s.rbegin(); i != s.rend(); ++i) print_char(*i); } print_char('.'); rep (decimal_precision) { a *= 10; print_char((char)('0' + (int)std::fmod(a, 10.0))); } } private: template void print(const std::tuple& a) { if IF_CONSTEXPR (i < sizeof...(Args)) { if IF_CONSTEXPR (debug) print_char(','); print_char(' '); print(std::get(a)); print(a); } } public: template void print(const std::tuple& a) { if IF_CONSTEXPR (debug) print_char('('); if IF_CONSTEXPR (sizeof...(Args) != 0) print(std::get<0>(a)); print<1, Args...>(a); if IF_CONSTEXPR (debug) print_char(')'); } template void print(const std::pair& a) { if IF_CONSTEXPR (debug) print_char('('); print(a.first); if IF_CONSTEXPR (debug) print_char(','); print_char(' '); print(a.second); if IF_CONSTEXPR (debug) print_char(')'); } template::value && !has_print::value>::type* = nullptr> void print(const T& a) { if IF_CONSTEXPR (debug) print_char('{'); for (auto i = std::begin(a); i != std::end(a); ++i) { if (i != std::begin(a)) { if IF_CONSTEXPR (debug) print_char(','); print_char(' '); } print(*i); } if IF_CONSTEXPR (debug) print_char('}'); } template::value && !debug>::type* = nullptr> void print(const T& a) { a.print(*this); } template::value && debug>::type* = nullptr> void print(const T& a) { a.debug(*this); } void operator()() {} template void operator()(const Head& head, const Args&... args) { print(head); operator()(args...); } template Printer& operator<<(const T& a) { print(a); return *this; } Printer& operator<<(Printer& (*pf)(Printer&)) { return pf(*this); } }; template Printer& endl(Printer& pr) { pr.print_char('\n'); pr.flush(); return pr; } template Printer& flush(Printer& pr) { pr.flush(); return pr; } struct SetPrec { int n; template void print(Pr& pr) const { pr.set_decimal_precision(n); } template void debug(Pr& pr) const { pr.set_decimal_precision(n); } }; SetPrec setprec(int n) { return SetPrec{n}; }; Printer::iterator> print(writer.begin()), eprint(ewriter.begin()); template auto prints(const T& v) -> decltype(print << v, (void)0) { print << v; print.print_char('\n'); } template auto prints(const Head& head, const Tail&... tail) -> decltype(print << head, (void)0) { print << head; print.print_char(' '); prints(tail...); } #ifdef SHIO_LOCAL Printer::iterator, true> debug(writer.begin()), edebug(ewriter.begin()); #else char debug_iterator_character; class DebugIterator { public: DebugIterator() noexcept = default; DebugIterator& operator++() { return *this; } DebugIterator& operator++(int) { return *this; } char& operator*() const { return debug_iterator_character; } void flush() const {} }; Printer debug, edebug; #endif template auto debugs(const T& v) -> decltype(debug << v, (void)0) { debug << v; debug.print_char('\n'); } template auto debugs(const Head& head, const Tail&... tail) -> decltype(debug << head, (void)0) { debug << head; debug.print_char(' '); debugs(tail...); } #line 2 "library/template/bitop.hpp" #line 6 "library/template/bitop.hpp" namespace bitop { #define KTH_BIT(b, k) (((b) >> (k)) & 1) #define POW2(k) (1ull << (k)) inline ull next_combination(int n, ull x) { if (n == 0) return 1; ull a = x & -x; ull b = x + a; return (x & ~b) / a >> 1 | b; } #define rep_comb(i, n, k) \ for (ull i = (1ull << (k)) - 1; i < (1ull << (n)); \ i = bitop::next_combination((n), i)) inline CONSTEXPR int msb(ull x) { int res = x ? 0 : -1; if (x & 0xFFFFFFFF00000000) x &= 0xFFFFFFFF00000000, res += 32; if (x & 0xFFFF0000FFFF0000) x &= 0xFFFF0000FFFF0000, res += 16; if (x & 0xFF00FF00FF00FF00) x &= 0xFF00FF00FF00FF00, res += 8; if (x & 0xF0F0F0F0F0F0F0F0) x &= 0xF0F0F0F0F0F0F0F0, res += 4; if (x & 0xCCCCCCCCCCCCCCCC) x &= 0xCCCCCCCCCCCCCCCC, res += 2; return res + ((x & 0xAAAAAAAAAAAAAAAA) ? 1 : 0); } inline CONSTEXPR int ceil_log2(ull x) { return x ? msb(x - 1) + 1 : 0; } inline CONSTEXPR ull reverse(ull x) { x = ((x & 0xAAAAAAAAAAAAAAAA) >> 1) | ((x & 0x5555555555555555) << 1); x = ((x & 0xCCCCCCCCCCCCCCCC) >> 2) | ((x & 0x3333333333333333) << 2); x = ((x & 0xF0F0F0F0F0F0F0F0) >> 4) | ((x & 0x0F0F0F0F0F0F0F0F) << 4); x = ((x & 0xFF00FF00FF00FF00) >> 8) | ((x & 0x00FF00FF00FF00FF) << 8); x = ((x & 0xFFFF0000FFFF0000) >> 16) | ((x & 0x0000FFFF0000FFFF) << 16); return (x >> 32) | (x << 32); } inline CONSTEXPR ull reverse(ull x, int n) { return reverse(x) >> (64 - n); } } // namespace bitop inline CONSTEXPR int popcnt(ull x) noexcept { #if __cplusplus >= 202002L return std::popcount(x); #endif x = (x & 0x5555555555555555) + ((x >> 1) & 0x5555555555555555); x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333); x = (x & 0x0f0f0f0f0f0f0f0f) + ((x >> 4) & 0x0f0f0f0f0f0f0f0f); x = (x & 0x00ff00ff00ff00ff) + ((x >> 8) & 0x00ff00ff00ff00ff); x = (x & 0x0000ffff0000ffff) + ((x >> 16) & 0x0000ffff0000ffff); return (x & 0x00000000ffffffff) + ((x >> 32) & 0x00000000ffffffff); } #line 2 "library/template/func.hpp" #line 6 "library/template/func.hpp" template> inline constexpr bool chmin(T& a, const U& b, Comp cmp = Comp()) noexcept(noexcept(cmp(b, a))) { return cmp(b, a) ? a = b, true : false; } template> inline constexpr bool chmax(T& a, const U& b, Comp cmp = Comp()) noexcept(noexcept(cmp(a, b))) { return cmp(a, b) ? a = b, true : false; } inline CONSTEXPR ll gcd(ll a, ll b) noexcept { if (a < 0) a = -a; if (b < 0) b = -b; while (b) { const ll c = a; a = b; b = c % b; } return a; } inline CONSTEXPR ll lcm(ll a, ll b) noexcept { return a / gcd(a, b) * b; } inline CONSTEXPR bool is_prime(ll N) noexcept { if (N <= 1) return false; for (ll i = 2; i * i <= N; ++i) { if (N % i == 0) return false; } return true; } inline std::vector prime_factor(ll N) { std::vector res; for (ll i = 2; i * i <= N; ++i) { while (N % i == 0) { res.push_back(i); N /= i; } } if (N != 1) res.push_back(N); return res; } inline CONSTEXPR ll my_pow(ll a, ll b) noexcept { ll res = 1; while (b) { if (b & 1) res *= a; b >>= 1; a *= a; } return res; } inline CONSTEXPR ll mod_pow(ll a, ll b, ll mod) { assert(mod > 0); if (mod == 1) return 0; a %= mod; ll res = 1; while (b) { if (b & 1) (res *= a) %= mod; b >>= 1; (a *= a) %= mod; } return res; } inline PLL extGCD(ll a, ll b) { const ll n = a, m = b; ll x = 1, y = 0, u = 0, v = 1; ll t; while (b) { t = a / b; std::swap(a -= t * b, b); std::swap(x -= t * u, u); std::swap(y -= t * v, v); } if (x < 0) { x += m; y -= n; } return {x, y}; } inline ll mod_inv(ll a, ll mod) { ll b = mod; ll x = 1, u = 0; ll t; while (b) { t = a / b; std::swap(a -= t * b, b); std::swap(x -= t * u, u); } if (x < 0) x += mod; assert(a == 1); return x; } #line 2 "library/template/util.hpp" #line 6 "library/template/util.hpp" template class RecLambda { private: F f; public: explicit constexpr RecLambda(F&& f_) : f(std::forward(f_)) {} template constexpr auto operator()(Args&&... args) -> decltype(f(*this, std::forward(args)...)) { return f(*this, std::forward(args)...); } }; template inline constexpr RecLambda rec_lambda(F&& f) { return RecLambda(std::forward(f)); } template struct multi_dim_vector { using type = std::vector::type>; }; template struct multi_dim_vector { using type = T; }; template constexpr std::vector make_vec(int n, Arg&& arg) { return std::vector(n, std::forward(arg)); } template constexpr typename multi_dim_vector::type make_vec(int n, Args&&... args) { return typename multi_dim_vector::type( n, make_vec(std::forward(args)...)); } template> class presser { private: std::vector dat; Comp cmp; bool sorted = false; public: presser() : presser(Comp()) {} presser(const Comp& cmp) : cmp(cmp) {} presser(const std::vector& vec, const Comp& cmp = Comp()) : dat(vec), cmp(cmp) {} presser(std::vector&& vec, const Comp& cmp = Comp()) : dat(std::move(vec)), cmp(cmp) {} presser(std::initializer_list il, const Comp& cmp = Comp()) : dat(all(il)), cmp(cmp) {} void reserve(int n) { assert(!sorted); dat.reserve(n); } void push_back(const T& v) { assert(!sorted); dat.push_back(v); } void push_back(T&& v) { assert(!sorted); dat.push_back(std::move(v)); } template void emplace_back(Args&&... args) { assert(!sorted); dat.emplace_back(std::forward(args)...); } void push(const std::vector& vec) { assert(!sorted); const int n = dat.size(); dat.resize(n + vec.size()); rep (i, vec.size()) dat[n + i] = vec[i]; } int build() { assert(!sorted); sorted = true; std::sort(all(dat), cmp); dat.erase(std::unique(all(dat), [&](const T& a, const T& b) -> bool { return !cmp(a, b) && !cmp(b, a); }), dat.end()); return dat.size(); } const T& operator[](int k) const& { assert(sorted); assert(0 <= k && k < (int)dat.size()); return dat[k]; } T operator[](int k) && { assert(sorted); assert(0 <= k && k < (int)dat.size()); return std::move(dat[k]); } int get(const T& val) const { assert(sorted); auto itr = std::lower_bound(all(dat), val, cmp); assert(itr != dat.end() && !cmp(val, *itr)); return itr - dat.begin(); } int lower_bound(const T& val) const { assert(sorted); auto itr = std::lower_bound(all(dat), val, cmp); return itr - dat.begin(); } int upper_bound(const T& val) const { assert(sorted); auto itr = std::upper_bound(all(dat), val, cmp); return itr - dat.begin(); } bool contains(const T& val) const { assert(sorted); return std::binary_search(all(dat), val, cmp); } std::vector pressed(const std::vector& vec) const { assert(sorted); std::vector res(vec.size()); rep (i, vec.size()) res[i] = get(vec[i]); return res; } void press(std::vector& vec) const { static_assert(std::is_convertible::value, "template argument must be convertible from int type"); assert(sorted); each_for (i : vec) i = get(i); } int size() const { assert(sorted); return dat.size(); } const std::vector& data() const& { return dat; } std::vector data() && { return std::move(dat); } }; #line 2 "combined.cpp" #line 6 "combined.cpp" #include #line 8 "combined.cpp" #ifdef _MSC_VER #include #endif namespace atcoder { namespace internal { int ceil_pow2(int n) { int x = 0; while ((1U << x) < (unsigned int)(n)) x++; return x; } constexpr int bsf_constexpr(unsigned int n) { int x = 0; while (!(n & (1 << x))) x++; return x; } int bsf(unsigned int n) { #ifdef _MSC_VER unsigned long index; _BitScanForward(&index, n); return index; #else return __builtin_ctz(n); #endif } } // namespace internal } // namespace atcoder #line 48 "combined.cpp" #ifdef _MSC_VER #include #endif #line 55 "combined.cpp" #ifdef _MSC_VER #include #endif namespace atcoder { namespace internal { constexpr long long safe_mod(long long x, long long m) { x %= m; if (x < 0) x += m; return x; } struct barrett { unsigned int _m; unsigned long long im; explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {} unsigned int umod() const { return _m; } unsigned int mul(unsigned int a, unsigned int b) const { unsigned long long z = a; z *= b; #ifdef _MSC_VER unsigned long long x; _umul128(z, im, &x); #else unsigned long long x = (unsigned long long)(((unsigned __int128)(z)*im) >> 64); #endif unsigned int v = (unsigned int)(z - x * _m); if (_m <= v) v += _m; return v; } }; constexpr long long pow_mod_constexpr(long long x, long long n, int m) { if (m == 1) return 0; unsigned int _m = (unsigned int)(m); unsigned long long r = 1; unsigned long long y = safe_mod(x, m); while (n) { if (n & 1) r = (r * y) % _m; y = (y * y) % _m; n >>= 1; } return r; } constexpr bool is_prime_constexpr(int n) { if (n <= 1) return false; if (n == 2 || n == 7 || n == 61) return true; if (n % 2 == 0) return false; long long d = n - 1; while (d % 2 == 0) d /= 2; constexpr long long bases[3] = {2, 7, 61}; for (long long a : bases) { long long t = d; long long y = pow_mod_constexpr(a, t, n); while (t != n - 1 && y != 1 && y != n - 1) { y = y * y % n; t <<= 1; } if (y != n - 1 && t % 2 == 0) { return false; } } return true; } template constexpr bool is_prime = is_prime_constexpr(n); constexpr std::pair inv_gcd(long long a, long long b) { a = safe_mod(a, b); if (a == 0) return {b, 0}; long long s = b, t = a; long long m0 = 0, m1 = 1; while (t) { long long u = s / t; s -= t * u; m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b auto tmp = s; s = t; t = tmp; tmp = m0; m0 = m1; m1 = tmp; } if (m0 < 0) m0 += b / s; return {s, m0}; } constexpr int primitive_root_constexpr(int m) { if (m == 2) return 1; if (m == 167772161) return 3; if (m == 469762049) return 3; if (m == 754974721) return 11; if (m == 998244353) return 3; int divs[20] = {}; divs[0] = 2; int cnt = 1; int x = (m - 1) / 2; while (x % 2 == 0) x /= 2; for (int i = 3; (long long)(i)*i <= x; i += 2) { if (x % i == 0) { divs[cnt++] = i; while (x % i == 0) { x /= i; } } } if (x > 1) { divs[cnt++] = x; } for (int g = 2;; g++) { bool ok = true; for (int i = 0; i < cnt; i++) { if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) { ok = false; break; } } if (ok) return g; } } template constexpr int primitive_root = primitive_root_constexpr(m); unsigned long long floor_sum_unsigned(unsigned long long n, unsigned long long m, unsigned long long a, unsigned long long b) { unsigned long long ans = 0; while (true) { if (a >= m) { ans += n * (n - 1) / 2 * (a / m); a %= m; } if (b >= m) { ans += n * (b / m); b %= m; } unsigned long long y_max = a * n + b; if (y_max < m) break; n = (unsigned long long)(y_max / m); b = (unsigned long long)(y_max % m); std::swap(m, a); } return ans; } } // namespace internal } // namespace atcoder #line 221 "combined.cpp" namespace atcoder { namespace internal { #ifndef _MSC_VER template using is_signed_int128 = typename std::conditional::value || std::is_same::value, std::true_type, std::false_type>::type; template using is_unsigned_int128 = typename std::conditional::value || std::is_same::value, std::true_type, std::false_type>::type; template using make_unsigned_int128 = typename std::conditional::value, __uint128_t, unsigned __int128>; template using is_integral = typename std::conditional::value || is_signed_int128::value || is_unsigned_int128::value, std::true_type, std::false_type>::type; template using is_signed_int = typename std::conditional<(is_integral::value && std::is_signed::value) || is_signed_int128::value, std::true_type, std::false_type>::type; template using is_unsigned_int = typename std::conditional<(is_integral::value && std::is_unsigned::value) || is_unsigned_int128::value, std::true_type, std::false_type>::type; template using to_unsigned = typename std::conditional< is_signed_int128::value, make_unsigned_int128, typename std::conditional::value, std::make_unsigned, std::common_type>::type>::type; #else template using is_integral = typename std::is_integral; template using is_signed_int = typename std::conditional::value && std::is_signed::value, std::true_type, std::false_type>::type; template using is_unsigned_int = typename std::conditional::value && std::is_unsigned::value, std::true_type, std::false_type>::type; template using to_unsigned = typename std::conditional::value, std::make_unsigned, std::common_type>::type; #endif template using is_signed_int_t = std::enable_if_t::value>; template using is_unsigned_int_t = std::enable_if_t::value>; template using to_unsigned_t = typename to_unsigned::type; } // namespace internal } // namespace atcoder namespace atcoder { namespace internal { struct modint_base {}; struct static_modint_base : modint_base {}; template using is_modint = std::is_base_of; template using is_modint_t = std::enable_if_t::value>; } // namespace internal template * = nullptr> struct static_modint : internal::static_modint_base { using mint = static_modint; public: static constexpr int mod() { return m; } static mint raw(int v) { mint x; x._v = v; return x; } static_modint() : _v(0) {} template * = nullptr> static_modint(T v) { long long x = (long long)(v % (long long)(umod())); if (x < 0) x += umod(); _v = (unsigned int)(x); } template * = nullptr> static_modint(T v) { _v = (unsigned int)(v % umod()); } unsigned int val() const { return _v; } mint& operator++() { _v++; if (_v == umod()) _v = 0; return *this; } mint& operator--() { if (_v == 0) _v = umod(); _v--; return *this; } mint operator++(int) { mint result = *this; ++*this; return result; } mint operator--(int) { mint result = *this; --*this; return result; } mint& operator+=(const mint& rhs) { _v += rhs._v; if (_v >= umod()) _v -= umod(); return *this; } mint& operator-=(const mint& rhs) { _v -= rhs._v; if (_v >= umod()) _v += umod(); return *this; } mint& operator*=(const mint& rhs) { unsigned long long z = _v; z *= rhs._v; _v = (unsigned int)(z % umod()); return *this; } mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); } mint operator+() const { return *this; } mint operator-() const { return mint() - *this; } mint pow(long long n) const { assert(0 <= n); mint x = *this, r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } mint inv() const { if (prime) { assert(_v); return pow(umod() - 2); } else { auto eg = internal::inv_gcd(_v, m); assert(eg.first == 1); return eg.second; } } friend mint operator+(const mint& lhs, const mint& rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint& lhs, const mint& rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint& lhs, const mint& rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint& lhs, const mint& rhs) { return mint(lhs) /= rhs; } friend bool operator==(const mint& lhs, const mint& rhs) { return lhs._v == rhs._v; } friend bool operator!=(const mint& lhs, const mint& rhs) { return lhs._v != rhs._v; } private: unsigned int _v; static constexpr unsigned int umod() { return m; } static constexpr bool prime = internal::is_prime; }; template struct dynamic_modint : internal::modint_base { using mint = dynamic_modint; public: static int mod() { return (int)(bt.umod()); } static void set_mod(int m) { assert(1 <= m); bt = internal::barrett(m); } static mint raw(int v) { mint x; x._v = v; return x; } dynamic_modint() : _v(0) {} template * = nullptr> dynamic_modint(T v) { long long x = (long long)(v % (long long)(mod())); if (x < 0) x += mod(); _v = (unsigned int)(x); } template * = nullptr> dynamic_modint(T v) { _v = (unsigned int)(v % mod()); } unsigned int val() const { return _v; } mint& operator++() { _v++; if (_v == umod()) _v = 0; return *this; } mint& operator--() { if (_v == 0) _v = umod(); _v--; return *this; } mint operator++(int) { mint result = *this; ++*this; return result; } mint operator--(int) { mint result = *this; --*this; return result; } mint& operator+=(const mint& rhs) { _v += rhs._v; if (_v >= umod()) _v -= umod(); return *this; } mint& operator-=(const mint& rhs) { _v += mod() - rhs._v; if (_v >= umod()) _v -= umod(); return *this; } mint& operator*=(const mint& rhs) { _v = bt.mul(_v, rhs._v); return *this; } mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); } mint operator+() const { return *this; } mint operator-() const { return mint() - *this; } mint pow(long long n) const { assert(0 <= n); mint x = *this, r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } mint inv() const { auto eg = internal::inv_gcd(_v, mod()); assert(eg.first == 1); return eg.second; } friend mint operator+(const mint& lhs, const mint& rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint& lhs, const mint& rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint& lhs, const mint& rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint& lhs, const mint& rhs) { return mint(lhs) /= rhs; } friend bool operator==(const mint& lhs, const mint& rhs) { return lhs._v == rhs._v; } friend bool operator!=(const mint& lhs, const mint& rhs) { return lhs._v != rhs._v; } private: unsigned int _v; static internal::barrett bt; static unsigned int umod() { return bt.umod(); } }; template internal::barrett dynamic_modint::bt(998244353); using modint998244353 = static_modint<998244353>; using modint1000000007 = static_modint<1000000007>; using modint = dynamic_modint<-1>; namespace internal { template using is_static_modint = std::is_base_of; template using is_static_modint_t = std::enable_if_t::value>; template struct is_dynamic_modint : public std::false_type {}; template struct is_dynamic_modint> : public std::true_type {}; template using is_dynamic_modint_t = std::enable_if_t::value>; } // namespace internal } // namespace atcoder namespace atcoder { namespace internal { template , internal::is_static_modint_t* = nullptr> struct fft_info { static constexpr int rank2 = bsf_constexpr(mint::mod() - 1); std::array root; // root[i]^(2^i) == 1 std::array iroot; // root[i] * iroot[i] == 1 std::array rate2; std::array irate2; std::array rate3; std::array irate3; fft_info() { root[rank2] = mint(g).pow((mint::mod() - 1) >> rank2); iroot[rank2] = root[rank2].inv(); for (int i = rank2 - 1; i >= 0; i--) { root[i] = root[i + 1] * root[i + 1]; iroot[i] = iroot[i + 1] * iroot[i + 1]; } { mint prod = 1, iprod = 1; for (int i = 0; i <= rank2 - 2; i++) { rate2[i] = root[i + 2] * prod; irate2[i] = iroot[i + 2] * iprod; prod *= iroot[i + 2]; iprod *= root[i + 2]; } } { mint prod = 1, iprod = 1; for (int i = 0; i <= rank2 - 3; i++) { rate3[i] = root[i + 3] * prod; irate3[i] = iroot[i + 3] * iprod; prod *= iroot[i + 3]; iprod *= root[i + 3]; } } } }; template * = nullptr> void butterfly(std::vector& a) { int n = int(a.size()); int h = internal::ceil_pow2(n); static const fft_info info; int len = 0; // a[i, i+(n>>len), i+2*(n>>len), ..] is transformed while (len < h) { if (h - len == 1) { int p = 1 << (h - len - 1); mint rot = 1; for (int s = 0; s < (1 << len); s++) { int offset = s << (h - len); for (int i = 0; i < p; i++) { auto l = a[i + offset]; auto r = a[i + offset + p] * rot; a[i + offset] = l + r; a[i + offset + p] = l - r; } if (s + 1 != (1 << len)) rot *= info.rate2[bsf(~(unsigned int)(s))]; } len++; } else { int p = 1 << (h - len - 2); mint rot = 1, imag = info.root[2]; for (int s = 0; s < (1 << len); s++) { mint rot2 = rot * rot; mint rot3 = rot2 * rot; int offset = s << (h - len); for (int i = 0; i < p; i++) { auto mod2 = 1ULL * mint::mod() * mint::mod(); auto a0 = 1ULL * a[i + offset].val(); auto a1 = 1ULL * a[i + offset + p].val() * rot.val(); auto a2 = 1ULL * a[i + offset + 2 * p].val() * rot2.val(); auto a3 = 1ULL * a[i + offset + 3 * p].val() * rot3.val(); auto a1na3imag = 1ULL * mint(a1 + mod2 - a3).val() * imag.val(); auto na2 = mod2 - a2; a[i + offset] = a0 + a2 + a1 + a3; a[i + offset + 1 * p] = a0 + a2 + (2 * mod2 - (a1 + a3)); a[i + offset + 2 * p] = a0 + na2 + a1na3imag; a[i + offset + 3 * p] = a0 + na2 + (mod2 - a1na3imag); } if (s + 1 != (1 << len)) rot *= info.rate3[bsf(~(unsigned int)(s))]; } len += 2; } } } template * = nullptr> void butterfly_inv(std::vector& a) { int n = int(a.size()); int h = internal::ceil_pow2(n); static const fft_info info; int len = h; // a[i, i+(n>>len), i+2*(n>>len), ..] is transformed while (len) { if (len == 1) { int p = 1 << (h - len); mint irot = 1; for (int s = 0; s < (1 << (len - 1)); s++) { int offset = s << (h - len + 1); for (int i = 0; i < p; i++) { auto l = a[i + offset]; auto r = a[i + offset + p]; a[i + offset] = l + r; a[i + offset + p] = (unsigned long long)(mint::mod() + l.val() - r.val()) * irot.val(); ; } if (s + 1 != (1 << (len - 1))) irot *= info.irate2[bsf(~(unsigned int)(s))]; } len--; } else { int p = 1 << (h - len); mint irot = 1, iimag = info.iroot[2]; for (int s = 0; s < (1 << (len - 2)); s++) { mint irot2 = irot * irot; mint irot3 = irot2 * irot; int offset = s << (h - len + 2); for (int i = 0; i < p; i++) { auto a0 = 1ULL * a[i + offset + 0 * p].val(); auto a1 = 1ULL * a[i + offset + 1 * p].val(); auto a2 = 1ULL * a[i + offset + 2 * p].val(); auto a3 = 1ULL * a[i + offset + 3 * p].val(); auto a2na3iimag = 1ULL * mint((mint::mod() + a2 - a3) * iimag.val()).val(); a[i + offset] = a0 + a1 + a2 + a3; a[i + offset + 1 * p] = (a0 + (mint::mod() - a1) + a2na3iimag) * irot.val(); a[i + offset + 2 * p] = (a0 + a1 + (mint::mod() - a2) + (mint::mod() - a3)) * irot2.val(); a[i + offset + 3 * p] = (a0 + (mint::mod() - a1) + (mint::mod() - a2na3iimag)) * irot3.val(); } if (s + 1 != (1 << (len - 2))) irot *= info.irate3[bsf(~(unsigned int)(s))]; } len -= 2; } } } template * = nullptr> std::vector convolution_naive(const std::vector& a, const std::vector& b) { int n = int(a.size()), m = int(b.size()); std::vector ans(n + m - 1); if (n < m) { for (int j = 0; j < m; j++) { for (int i = 0; i < n; i++) { ans[i + j] += a[i] * b[j]; } } } else { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ans[i + j] += a[i] * b[j]; } } } return ans; } template * = nullptr> std::vector convolution_fft(std::vector a, std::vector b) { int n = int(a.size()), m = int(b.size()); int z = 1 << internal::ceil_pow2(n + m - 1); a.resize(z); internal::butterfly(a); b.resize(z); internal::butterfly(b); for (int i = 0; i < z; i++) { a[i] *= b[i]; } internal::butterfly_inv(a); a.resize(n + m - 1); mint iz = mint(z).inv(); for (int i = 0; i < n + m - 1; i++) a[i] *= iz; return a; } } // namespace internal template * = nullptr> std::vector convolution(std::vector&& a, std::vector&& b) { int n = int(a.size()), m = int(b.size()); if (!n || !m) return {}; if (std::min(n, m) <= 60) return convolution_naive(a, b); return internal::convolution_fft(a, b); } template * = nullptr> std::vector convolution(const std::vector& a, const std::vector& b) { int n = int(a.size()), m = int(b.size()); if (!n || !m) return {}; if (std::min(n, m) <= 60) return convolution_naive(a, b); return internal::convolution_fft(a, b); } template ::value>* = nullptr> std::vector convolution(const std::vector& a, const std::vector& b) { int n = int(a.size()), m = int(b.size()); if (!n || !m) return {}; using mint = static_modint; std::vector a2(n), b2(m); for (int i = 0; i < n; i++) { a2[i] = mint(a[i]); } for (int i = 0; i < m; i++) { b2[i] = mint(b[i]); } auto c2 = convolution(move(a2), move(b2)); std::vector c(n + m - 1); for (int i = 0; i < n + m - 1; i++) { c[i] = c2[i].val(); } return c; } std::vector convolution_ll(const std::vector& a, const std::vector& b) { int n = int(a.size()), m = int(b.size()); if (!n || !m) return {}; static constexpr unsigned long long MOD1 = 754974721; // 2^24 static constexpr unsigned long long MOD2 = 167772161; // 2^25 static constexpr unsigned long long MOD3 = 469762049; // 2^26 static constexpr unsigned long long M2M3 = MOD2 * MOD3; static constexpr unsigned long long M1M3 = MOD1 * MOD3; static constexpr unsigned long long M1M2 = MOD1 * MOD2; static constexpr unsigned long long M1M2M3 = MOD1 * MOD2 * MOD3; static constexpr unsigned long long i1 = internal::inv_gcd(MOD2 * MOD3, MOD1).second; static constexpr unsigned long long i2 = internal::inv_gcd(MOD1 * MOD3, MOD2).second; static constexpr unsigned long long i3 = internal::inv_gcd(MOD1 * MOD2, MOD3).second; auto c1 = convolution(a, b); auto c2 = convolution(a, b); auto c3 = convolution(a, b); std::vector c(n + m - 1); for (int i = 0; i < n + m - 1; i++) { unsigned long long x = 0; x += (c1[i] * i1) % MOD1 * M2M3; x += (c2[i] * i2) % MOD2 * M1M3; x += (c3[i] * i3) % MOD3 * M1M2; long long diff = c1[i] - internal::safe_mod((long long)(x), (long long)(MOD1)); if (diff < 0) diff += MOD1; static constexpr unsigned long long offset[5] = { 0, 0, M1M2M3, 2 * M1M2M3, 3 * M1M2M3}; x -= offset[diff % 5]; c[i] = x; } return c; } } // namespace atcoder using namespace std; int main() { ll N, M, X; scan >> N >> M >> X; vector C(N); scan >> C; vector> A(M); scan >> A; rep (i, N) --C[i]; rep (i, M) { --A[i][0]; --A[i][1]; } vector ans(N); rep (i, 5) { vector X(N); rep (j, N) { if (C[j] == i) X[j] = 1; } vector Y(N); rep (j, M) { if (A[j][1] == i) Y[N - 1 - A[j][0]] += A[j][2]; } auto Z = atcoder::convolution_ll(X, Y); rep (i, N) ans[i] += Z[N - 1 + i]; } ll mx = 0; rep (i, N) chmax(mx, ans[i] + i * X); prints(mx); }