QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#207039#7558. Abstractucup-team180#AC ✓70ms16344kbC++1731.4kb2023-10-08 04:18:522023-10-08 04:18:52

Judging History

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

  • [2023-10-08 04:18:52]
  • 评测
  • 测评结果:AC
  • 用时:70ms
  • 内存:16344kb
  • [2023-10-08 04:18:52]
  • 提交

answer

#pragma region Macros
#ifdef noimi
#include "my_template.hpp"
#else
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <immintrin.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <immintrin.h>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <tuple>
#include <type_traits>
#include <utility>
#include <variant>

#ifdef noimi
#define oj_local(a, b) b
#else
#define oj_local(a, b) a
#endif

#define LOCAL if(oj_local(0, 1))
#define OJ if(oj_local(1, 0))

using namespace std;
using ll = long long;
using ull = unsigned long long int;
using i128 = __int128_t;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ld = long double;
template <typename T> using vc = vector<T>;
template <typename T> using vvc = vector<vc<T>>;
template <typename T> using vvvc = vector<vvc<T>>;
using vi = vc<int>;
using vl = vc<ll>;
using vpi = vc<pii>;
using vpl = vc<pll>;
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
template <typename T> int si(const T &x) { return x.size(); }
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); }
vi iota(int n) {
    vi a(n);
    return iota(a.begin(), a.end(), 0), a;
}
template <typename T> vi iota(const vector<T> &a, bool greater = false) {
    vi res(a.size());
    iota(res.begin(), res.end(), 0);
    sort(res.begin(), res.end(), [&](int i, int j) {
        if(greater) return a[i] > a[j];
        return a[i] < a[j];
    });
    return res;
}

// macros
#define overload5(a, b, c, d, e, name, ...) name
#define overload4(a, b, c, d, name, ...) name
#define endl '\n'
#define REP0(n) for(ll jidlsjf = 0; jidlsjf < n; ++jidlsjf)
#define REP1(i, n) for(ll i = 0; i < (n); ++i)
#define REP2(i, a, b) for(ll i = (a); i < (b); ++i)
#define REP3(i, a, b, c) for(ll i = (a); i < (b); i += (c))
#define rep(...) overload4(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)
#define per0(n) for(int jidlsjf = 0; jidlsjf < (n); ++jidlsjf)
#define per1(i, n) for(ll i = (n)-1; i >= 0; --i)
#define per2(i, a, b) for(ll i = (a)-1; i >= b; --i)
#define per3(i, a, b, c) for(ll i = (a)-1; i >= (b); i -= (c))
#define per(...) overload4(__VA_ARGS__, per3, per2, per1, per0)(__VA_ARGS__)
#define fore0(a) rep(a.size())
#define fore1(i, a) for(auto &&i : a)
#define fore2(a, b, v) for(auto &&[a, b] : v)
#define fore3(a, b, c, v) for(auto &&[a, b, c] : v)
#define fore4(a, b, c, d, v) for(auto &&[a, b, c, d] : v)
#define fore(...) overload5(__VA_ARGS__, fore4, fore3, fore2, fore1, fore0)(__VA_ARGS__)
#define setbits(j, n) for(ll iiiii = (n), j = lowbit(iiiii); iiiii; iiiii ^= 1 << j, j = lowbit(iiiii))
#define perm(v) for(bool permrepflag = true; (permrepflag ? exchange(permrepflag, false) : next_permutation(all(v)));)
#define fi first
#define se second
#define pb push_back
#define ppb pop_back
#define ppf pop_front
#define eb emplace_back
#define drop(s) cout << #s << endl, exit(0)
#define si(c) (int)(c).size()
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define lbg(c, x) distance((c).begin(), lower_bound(all(c), (x), greater{}))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define ubg(c, x) distance((c).begin(), upper_bound(all(c), (x), greater{}))
#define rng(v, l, r) v.begin() + (l), v.begin() + (r)
#define all(c) begin(c), end(c)
#define rall(c) rbegin(c), rend(c)
#define SORT(v) sort(all(v))
#define REV(v) reverse(all(v))
#define UNIQUE(x) SORT(x), x.erase(unique(all(x)), x.end())
template <typename T = ll, typename S> T SUM(const S &v) { return accumulate(all(v), T(0)); }
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define overload2(_1, _2, name, ...) name
#define vec(type, name, ...) vector<type> name(__VA_ARGS__)
#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__))))
constexpr pii dx4[4] = {pii{1, 0}, pii{0, 1}, pii{-1, 0}, pii{0, -1}};
constexpr pii dx8[8] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};

namespace yesno_impl {
const string YESNO[2] = {"NO", "YES"};
const string YesNo[2] = {"No", "Yes"};
const string yesno[2] = {"no", "yes"};
const string firstsecond[2] = {"second", "first"};
const string FirstSecond[2] = {"Second", "First"};
const string possiblestr[2] = {"impossible", "possible"};
const string Possiblestr[2] = {"Impossible", "Possible"};
void YES(bool t = 1) { cout << YESNO[t] << endl; }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { cout << YesNo[t] << endl; }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { cout << yesno[t] << endl; }
void no(bool t = 1) { yes(!t); }
void first(bool t = 1) { cout << firstsecond[t] << endl; }
void First(bool t = 1) { cout << FirstSecond[t] << endl; }
void possible(bool t = 1) { cout << possiblestr[t] << endl; }
void Possible(bool t = 1) { cout << Possiblestr[t] << endl; }
}; // namespace yesno_impl
using namespace yesno_impl;

#define INT(...)                                                                                                                                               \
    int __VA_ARGS__;                                                                                                                                           \
    IN(__VA_ARGS__)
#define INTd(...)                                                                                                                                              \
    int __VA_ARGS__;                                                                                                                                           \
    IN2(__VA_ARGS__)
#define LL(...)                                                                                                                                                \
    ll __VA_ARGS__;                                                                                                                                            \
    IN(__VA_ARGS__)
#define LLd(...)                                                                                                                                               \
    ll __VA_ARGS__;                                                                                                                                            \
    IN2(__VA_ARGS__)
#define STR(...)                                                                                                                                               \
    string __VA_ARGS__;                                                                                                                                        \
    IN(__VA_ARGS__)
#define CHR(...)                                                                                                                                               \
    char __VA_ARGS__;                                                                                                                                          \
    IN(__VA_ARGS__)
#define DBL(...)                                                                                                                                               \
    double __VA_ARGS__;                                                                                                                                        \
    IN(__VA_ARGS__)
#define VEC(type, name, size)                                                                                                                                  \
    vector<type> name(size);                                                                                                                                   \
    IN(name)
#define VECd(type, name, size)                                                                                                                                 \
    vector<type> name(size);                                                                                                                                   \
    IN2(name)
#define VEC2(type, name1, name2, size)                                                                                                                         \
    vector<type> name1(size), name2(size);                                                                                                                     \
    for(int i = 0; i < size; i++) IN(name1[i], name2[i])
#define VEC2d(type, name1, name2, size)                                                                                                                        \
    vector<type> name1(size), name2(size);                                                                                                                     \
    for(int i = 0; i < size; i++) IN2(name1[i], name2[i])
#define VEC3(type, name1, name2, name3, size)                                                                                                                  \
    vector<type> name1(size), name2(size), name3(size);                                                                                                        \
    for(int i = 0; i < size; i++) IN(name1[i], name2[i], name3[i])
#define VEC3d(type, name1, name2, name3, size)                                                                                                                 \
    vector<type> name1(size), name2(size), name3(size);                                                                                                        \
    for(int i = 0; i < size; i++) IN2(name1[i], name2[i], name3[i])
#define VEC4(type, name1, name2, name3, name4, size)                                                                                                           \
    vector<type> name1(size), name2(size), name3(size), name4(size);                                                                                           \
    for(int i = 0; i < size; i++) IN(name1[i], name2[i], name3[i], name4[i]);
#define VEC4d(type, name1, name2, name3, name4, size)                                                                                                          \
    vector<type> name1(size), name2(size), name3(size), name4(size);                                                                                           \
    for(int i = 0; i < size; i++) IN2(name1[i], name2[i], name3[i], name4[i]);
#define VV(type, name, h, w)                                                                                                                                   \
    vector<vector<type>> name(h, vector<type>(w));                                                                                                             \
    IN(name)
#define VVd(type, name, h, w)                                                                                                                                  \
    vector<vector<type>> name(h, vector<type>(w));                                                                                                             \
    IN2(name)
int scan() { return getchar(); }
void scan(int &a) { cin >> a; }
void scan(long long &a) { cin >> a; }
void scan(char &a) { cin >> a; }
void scan(double &a) { cin >> a; }
void scan(string &a) { cin >> a; }
template <class T, class S> void scan(pair<T, S> &p) { scan(p.first), scan(p.second); }
template <class T> void scan(vector<T> &);
template <class T> void scan(vector<T> &a) {
    for(auto &i : a) scan(i);
}
template <class T> void scan(T &a) { cin >> a; }
void IN() {}
void IN2() {}
template <class Head, class... Tail> void IN(Head &head, Tail &...tail) {
    scan(head);
    IN(tail...);
}
template <class Head, class... Tail> void IN2(Head &head, Tail &...tail) {
    scan(head);
    --head;
    IN2(tail...);
}

template <int p = -1> void pat() {}
template <int p = -1, class Head, class... Tail> void pat(Head &h, Tail &...tail) {
    h += p;
    pat<p>(tail...);
}

template <typename T, typename S> T ceil(T x, S y) {
    assert(y);
    return (y < 0 ? ceil(-x, -y) : (x > 0 ? (x + y - 1) / y : x / y));
}

template <typename T, typename S> T floor(T x, S y) {
    assert(y);
    return (y < 0 ? floor(-x, -y) : (x > 0 ? x / y : x / y - (x % y == 0 ? 0 : 1)));
}
template <typename T, typename S, typename U> U bigmul(const T &x, const S &y, const U &lim) { // clamp(x * y, -lim, lim)
    if(x < 0 and y < 0) return bigmul(-x, -y, lim);
    if(x < 0) return -bigmul(-x, y, lim);
    if(y < 0) return -bigmul(x, -y, lim);
    return y == 0 or x <= lim / y ? x * y : lim;
}
template <class T> T POW(T x, int n) {
    T res = 1;
    for(; n; n >>= 1, x *= x)
        if(n & 1) res *= x;
    return res;
}
template <class T, class S> T POW(T x, S n, const ll &mod) {
    T res = 1;
    x %= mod;
    for(; n; n >>= 1, x = x * x % mod)
        if(n & 1) res = res * x % mod;
    return res;
}
vector<pll> factor(ll x) {
    vector<pll> ans;
    for(ll i = 2; i * i <= x; i++)
        if(x % i == 0) {
            ans.push_back({i, 1});
            while((x /= i) % i == 0) ans.back().second++;
        }
    if(x != 1) ans.push_back({x, 1});
    return ans;
}
template <class T> vector<T> divisor(T x) {
    vector<T> ans;
    for(T i = 1; i * i <= x; i++)
        if(x % i == 0) {
            ans.pb(i);
            if(i * i != x) ans.pb(x / i);
        }
    return ans;
}
template <typename T> void zip(vector<T> &x) {
    vector<T> y = x;
    UNIQUE(y);
    for(int i = 0; i < x.size(); ++i) { x[i] = lb(y, x[i]); }
}
template <class S> void fold_in(vector<S> &v) {}
template <typename Head, typename... Tail, class S> void fold_in(vector<S> &v, Head &&a, Tail &&...tail) {
    for(auto e : a) v.emplace_back(e);
    fold_in(v, tail...);
}
template <class S> void renumber(vector<S> &v) {}
template <typename Head, typename... Tail, class S> void renumber(vector<S> &v, Head &&a, Tail &&...tail) {
    for(auto &&e : a) e = lb(v, e);
    renumber(v, tail...);
}
template <class S, class... Args> vector<S> zip(vector<S> &head, Args &&...args) {
    vector<S> v;
    fold_in(v, head, args...);
    sort(all(v)), v.erase(unique(all(v)), v.end());
    renumber(v, head, args...);
    return v;
}

template <typename S> void rearrange(const vector<S> &id) {}
template <typename S, typename T> void rearrange_exec(const vector<S> &id, vector<T> &v) {
    vector<T> w(v.size());
    rep(i, si(id)) w[i] = v[id[i]];
    v.swap(w);
}
// 並び替える順番, 並び替える vector 達
template <typename S, typename Head, typename... Tail> void rearrange(const vector<S> &id, Head &a, Tail &...tail) {
    rearrange_exec(id, a);
    rearrange(id, tail...);
}

template <typename T> vector<T> RUI(const vector<T> &v) {
    vector<T> res(v.size() + 1);
    for(int i = 0; i < v.size(); i++) res[i + 1] = res[i] + v[i];
    return res;
}
template <typename T> void zeta_supersetsum(vector<T> &f) {
    int n = f.size();
    for(int i = 1; i < n; i <<= 1) rep(b, n) if(!(i & b)) f[b] += f[b | i];
}

template <typename T> void zeta_subsetsum(vector<T> &f) {
    int n = f.size();
    for(int i = 1; i < n; i <<= 1) rep(b, n) if(!(i & b)) f[b | i] += f[b];
}
template <typename T> void mobius_subset(vector<T> &f) {
    int n = f.size();
    for(int i = 1; i < n; i <<= 1) rep(b, n) if(!(i & b)) f[b] -= f[b | i];
}
template <typename T> void mobius_superset(vector<T> &f) {
    int n = f.size();
    for(int i = 1; i < n; i <<= 1) rep(b, n) if(!(i & b)) f[b | i] -= f[b];
}
// 反時計周りに 90 度回転
template <typename T> void rot(vector<vector<T>> &v) {
    if(empty(v)) return;
    int n = v.size(), m = v[0].size();
    vector<vector<T>> res(m, vector<T>(n));
    rep(i, n) rep(j, m) res[m - 1 - j][i] = v[i][j];
    v.swap(res);
}

vector<int> counter(const vector<int> &v, int max_num = -1) {
    if(max_num == -1) max_num = MAX(v);
    vector<int> res(max_num + 1);
    fore(e, v) res[e]++;
    return res;
}

// x in [l, r)
template <class T, class S> bool inc(const T &x, const S &l, const S &r) { return l <= x and x < r; }
template <class T, class S> bool inc(const T &x, const pair<S, S> &p) { return p.first <= x and x < p.second; }

// 便利関数
constexpr ll ten(int n) { return n == 0 ? 1 : ten(n - 1) * 10; }
constexpr ll tri(ll n) { return n * (n + 1) / 2; }
// l + ... + r
constexpr ll tri(ll l, ll r) { return (l + r) * (r - l + 1) / 2; }
ll max(int x, ll y) { return max((ll)x, y); }
ll max(ll x, int y) { return max(x, (ll)y); }
int min(int x, ll y) { return min((ll)x, y); }
int min(ll x, int y) { return min(x, (ll)y); }
// bit 演算系
#define bit(i) (1LL << i)       // (1 << i)
#define test(b, i) (b >> i & 1) // b の i bit 目が立っているか
ll pow2(int i) { return 1LL << i; }
int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
int topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
int lowbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }
int lowbit(ll a) { return a == 0 ? 64 : __builtin_ctzll(a); }
// int allbit(int n) { return (1 << n) - 1; }
constexpr ll mask(int n) { return (1LL << n) - 1; }
// int popcount(signed t) { return __builtin_popcount(t); }
// int popcount(ll t) { return __builtin_popcountll(t); }
int popcount(uint64_t t) { return __builtin_popcountll(t); }
static inline uint64_t popcount64(uint64_t x) {
    uint64_t m1 = 0x5555555555555555ll;
    uint64_t m2 = 0x3333333333333333ll;
    uint64_t m4 = 0x0F0F0F0F0F0F0F0Fll;
    uint64_t h01 = 0x0101010101010101ll;

    x -= (x >> 1) & m1;
    x = (x & m2) + ((x >> 2) & m2);
    x = (x + (x >> 4)) & m4;

    return (x * h01) >> 56;
}
bool ispow2(int i) { return i && (i & -i) == i; }

ll rnd(ll l, ll r) { //[l, r)
#ifdef noimi
    static mt19937_64 gen;
#else
    static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
#endif
    return uniform_int_distribution<ll>(l, r - 1)(gen);
}
ll rnd(ll n) { return rnd(0, n); }

template <class t> void random_shuffle(vc<t> &a) { rep(i, si(a)) swap(a[i], a[rnd(0, i + 1)]); }

int in() {
    int x;
    cin >> x;
    return x;
}
ll lin() {
    unsigned long long x;
    cin >> x;
    return x;
}

template <class T, class S> pair<T, S> operator-(const pair<T, S> &x) { return pair<T, S>(-x.first, -x.second); }
template <class T, class S> pair<T, S> operator-(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi - y.fi, x.se - y.se); }
template <class T, class S> pair<T, S> operator+(const pair<T, S> &x, const pair<T, S> &y) { return pair<T, S>(x.fi + y.fi, x.se + y.se); }
template <class T> pair<T, T> operator&(const pair<T, T> &l, const pair<T, T> &r) { return pair<T, T>(max(l.fi, r.fi), min(l.se, r.se)); }
template <class T, class S> pair<T, S> operator+=(pair<T, S> &l, const pair<T, S> &r) { return l = l + r; }
template <class T, class S> pair<T, S> operator-=(pair<T, S> &l, const pair<T, S> &r) { return l = l - r; }
template <class T> bool intersect(const pair<T, T> &l, const pair<T, T> &r) { return (l.se < r.se ? r.fi < l.se : l.fi < r.se); }

template <class T> vector<T> &operator++(vector<T> &v) {
    fore(e, v) e++;
    return v;
}
template <class T> vector<T> operator++(vector<T> &v, int) {
    auto res = v;
    fore(e, v) e++;
    return res;
}
template <class T> vector<T> &operator--(vector<T> &v) {
    fore(e, v) e--;
    return v;
}
template <class T> vector<T> operator--(vector<T> &v, int) {
    auto res = v;
    fore(e, v) e--;
    return res;
}
template <class T> void connect(vector<T> &l, const vector<T> &r) { fore(e, r) l.eb(e); }
template <class T> vector<T> operator+(const vector<T> &l, const vector<T> &r) {
    vector<T> res(max(si(l), si(r)));
    rep(i, si(l)) res[i] += l[i];
    rep(i, si(r)) res[i] += r[i];
    return res;
}
template <class T> vector<T> operator-(const vector<T> &l, const vector<T> &r) {
    vector<T> res(max(si(l), si(r)));
    rep(i, si(l)) res[i] += l[i];
    rep(i, si(r)) res[i] -= r[i];
    return res;
}
template <class T> vector<T> &operator+=(const vector<T> &l, const vector<T> &r) {
    if(si(l) < si(r)) l.resize(si(r));
    rep(i, si(r)) l[i] += r[i];
    return l;
}
template <class T> vector<T> &operator-=(const vector<T> &l, const vector<T> &r) {
    if(si(l) < si(r)) l.resize(si(r));
    rep(i, si(r)) l[i] -= r[i];
    return l;
}
template <class T> vector<T> &operator+=(vector<T> &v, const T &x) {
    fore(e, v) e += x;
    return v;
}
template <class T> vector<T> &operator-=(vector<T> &v, const T &x) {
    fore(e, v) e -= x;
    return v;
}

template <typename T> struct edge {
    int from, to;
    T cost;
    int id;
    edge(int to, T cost) : from(-1), to(to), cost(cost) {}
    edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}
    edge(int from, int to, T cost, int id) : from(from), to(to), cost(cost), id(id) {}
    constexpr bool operator<(const edge<T> &rhs) const noexcept { return cost < rhs.cost; }
    edge &operator=(const int &x) {
        to = x;
        return *this;
    }
    operator int() const { return to; }
    friend ostream operator<<(ostream &os, const edge &e) { return os << e.to; }
};
template <typename T> using Edges = vector<edge<T>>;

template <typename T = int> Edges<T> read_edges(int m, bool weighted = false) {
    Edges<T> res;
    res.reserve(m);
    for(int i = 0; i < m; i++) {
        int u, v, c = 0;
        scan(u), scan(v), u--, v--;
        if(weighted) scan(c);
        res.eb(u, v, c, i);
    }
    return res;
}

using Tree = vector<vector<int>>;
using Graph = vector<vector<int>>;
template <class T> using Wgraph = vector<vector<edge<T>>>;
Graph getG(int n, int m = -1, bool directed = false, int margin = 1) {
    Tree res(n);
    if(m == -1) m = n - 1;
    while(m--) {
        int a, b;
        cin >> a >> b;
        a -= margin, b -= margin;
        res[a].emplace_back(b);
        if(!directed) res[b].emplace_back(a);
    }
    return res;
}
Graph getTreeFromPar(int n, int margin = 1) {
    Graph res(n);
    for(int i = 1; i < n; i++) {
        int a;
        cin >> a;
        res[a - margin].emplace_back(i);
    }
    return res;
}
template <class T> Wgraph<T> getWg(int n, int m = -1, bool directed = false, int margin = 1) {
    Wgraph<T> res(n);
    if(m == -1) m = n - 1;
    while(m--) {
        int a, b;
        T c;
        scan(a), scan(b), scan(c);
        a -= margin, b -= margin;
        res[a].emplace_back(b, c);
        if(!directed) res[b].emplace_back(a, c);
    }
    return res;
}
void add(Graph &G, int x, int y) { G[x].eb(y), G[y].eb(x); }
template <class S, class T> void add(Wgraph<S> &G, int x, int y, T c) { G[x].eb(y, c), G[y].eb(x, c); }

#define TEST                                                                                                                                                   \
    INT(testcases);                                                                                                                                            \
    while(testcases--)

i128 abs(const i128 &x) { return x > 0 ? x : -x; }
istream &operator>>(istream &is, i128 &v) {
    string s;
    is >> s;
    v = 0;
    for(int i = 0; i < (int)s.size(); i++) {
        if(isdigit(s[i])) { v = v * 10 + s[i] - '0'; }
    }
    if(s[0] == '-') { v *= -1; }
    return is;
}

ostream &operator<<(ostream &os, const i128 &v) {
    if(v == 0) { return (os << "0"); }
    i128 num = v;
    if(v < 0) {
        os << '-';
        num = -num;
    }
    string s;
    for(; num > 0; num /= 10) { s.push_back((char)(num % 10) + '0'); }
    reverse(s.begin(), s.end());
    return (os << s);
}
namespace aux {
template <typename T, unsigned N, unsigned L> struct tp {
    static void output(std::ostream &os, const T &v) {
        os << std::get<N>(v) << (&os == &cerr ? ", " : " ");
        tp<T, N + 1, L>::output(os, v);
    }
};
template <typename T, unsigned N> struct tp<T, N, N> {
    static void output(std::ostream &os, const T &v) { os << std::get<N>(v); }
};
} // namespace aux
template <typename... Ts> std::ostream &operator<<(std::ostream &os, const std::tuple<Ts...> &t) {
    if(&os == &cerr) { os << '('; }
    aux::tp<std::tuple<Ts...>, 0, sizeof...(Ts) - 1>::output(os, t);
    if(&os == &cerr) { os << ')'; }
    return os;
}
template <typename T, typename S, typename U> std::ostream &operator<<(std::ostream &os, const priority_queue<T, S, U> &_pq) {
    auto pq = _pq;
    vector<T> res;
    while(!empty(pq)) res.emplace_back(pq.top()), pq.pop();
    return os << res;
}
template <class T, class S> ostream &operator<<(ostream &os, const pair<T, S> &p) {
    if(&os == &cerr) { return os << "(" << p.first << ", " << p.second << ")"; }
    return os << p.first << " " << p.second;
}
template <class Ch, class Tr, class Container> std::basic_ostream<Ch, Tr> &operator<<(std::basic_ostream<Ch, Tr> &os, const Container &x) {
    bool f = true;
    if(&os == &cerr) os << "[";
    for(auto &y : x) {
        if(&os == &cerr)
            os << (f ? "" : ", ") << y;
        else
            os << (f ? "" : " ") << y;
        f = false;
    }
    if(&os == &cerr) os << "]";
    return os;
}

#define dump(...) static_cast<void>(0)
#define dbg(...) static_cast<void>(0)

void OUT() { cout << endl; }
template <class Head, class... Tail> void OUT(const Head &head, const Tail &...tail) {
    cout << head;
    if(sizeof...(tail)) cout << ' ';
    OUT(tail...);
}

template <typename T> static constexpr T inf = numeric_limits<T>::max() / 2;
template <class T, class S> constexpr pair<T, S> inf<pair<T, S>> = {inf<T>, inf<S>};

template <class T> void OUT2(const T &t, T INF = inf<T>, T res = -1) { OUT(t != INF ? t : res); }
template <class T> void OUT2(vector<T> &v, T INF = inf<T>, T res = -1) {
    fore(e, v) if(e == INF) e = res;
    OUT(v);
    fore(e, v) if(e == res) e = INF;
}

template <class F> struct REC {
    F f;
    REC(F &&f_) : f(forward<F>(f_)) {}
    template <class... Args> auto operator()(Args &&...args) const { return f(*this, forward<Args>(args)...); }
};

template <class S> vector<pair<S, int>> runLength(const vector<S> &v) {
    vector<pair<S, int>> res;
    for(auto &e : v) {
        if(res.empty() or res.back().fi != e)
            res.eb(e, 1);
        else
            res.back().se++;
    }
    return res;
}
vector<pair<char, int>> runLength(const string &v) {
    vector<pair<char, int>> res;
    for(auto &e : v) {
        if(res.empty() or res.back().fi != e)
            res.eb(e, 1);
        else
            res.back().se++;
    }
    return res;
}

struct string_converter {
    char start = 0;
    char type(const char &c) const { return (islower(c) ? 'a' : isupper(c) ? 'A' : isdigit(c) ? '0' : 0); }
    int convert(const char &c) {
        if(!start) start = type(c);
        return c - start;
    }
    int convert(const char &c, const string &chars) { return chars.find(c); }
    template <typename T> auto convert(const T &v) {
        vector<decltype(convert(v[0]))> ret;
        ret.reserve(size(v));
        for(auto &&e : v) ret.emplace_back(convert(e));
        return ret;
    }
    template <typename T> auto convert(const T &v, const string &chars) {
        vector<decltype(convert(v[0], chars))> ret;
        ret.reserve(size(v));
        for(auto &&e : v) ret.emplace_back(convert(e, chars));
        return ret;
    }
    int operator()(const char &v, char s = 0) {
        start = s;
        return convert(v);
    }
    int operator()(const char &v, const string &chars) { return convert(v, chars); }
    template <typename T> auto operator()(const T &v, char s = 0) {
        start = s;
        return convert(v);
    }
    template <typename T> auto operator()(const T &v, const string &chars) { return convert(v, chars); }
} toint;

template <class T, class F> T bin_search(T ok, T ng, const F &f) {
    while(abs(ok - ng) > 1) {
        T mid = ok + ng >> 1;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}
template <class T, class F> T bin_search_double(T ok, T ng, const F &f, int iter = 80) {
    while(iter--) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}

struct Setup_io {
    Setup_io() {
        ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
        cout << fixed << setprecision(11);
    }
} setup_io;

#endif

#pragma endregion

/**
 * @brief Topological-Sort(トポロジカルソート)
 * @docs docs/topological-sort.md
 */
template <typename T> vector<int> topological_sort(const T &g) {
    const int N = (int)g.size();
    vector<int> deg(N);
    for(int i = 0; i < N; i++) {
        for(auto &to : g[i]) ++deg[to];
    }
    stack<int> st;
    for(int i = 0; i < N; i++) {
        if(deg[i] == 0) st.emplace(i);
    }
    vector<int> ord;
    while(!st.empty()) {
        auto p = st.top();
        st.pop();
        ord.emplace_back(p);
        for(auto &to : g[p]) {
            if(--deg[to] == 0) st.emplace(to);
        }
    }
    return ord;
}

using U = uint64_t;

int topbit(U t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }

constexpr int T = 61;
struct B {
    vector<U> a;
    B() : a(1, 0) {}
    void shift() {
        // dump("here");
        int n = si(a);
        per(i, n) {
            a[i] <<= 1;
            if(test(a[i], T)) {
                if(i == n - 1) {
                    a.eb(1);
                } else {
                    a[i + 1] |= 1;
                }
                a[i] -= bit(T);
            }
        }
        // dump("out");
    }

    void add(const B &r) {
        if(si(r.a) > si(a)) { a.resize(si(r.a)); }
        rep(i, si(a)) {
            if(i < si(r.a)) a[i] += r.a[i];
            if(test(a[i], T)) {
                if(i == si(a) - 1) { a.eb(0); }
                a[i + 1]++;
                a[i] -= bit(T);
            }
        }
    }

    ll ans() {
        if(si(a) == 1 and a[0] == 0) { return 0; }

        ll res = (si(a) - 1) * T;
        return res + topbit(a.back()) + 1;
    }
};

int main() {
    INT(n, m);
    VEC(ll, a, n);

    auto g = getG(n, m, true);
    auto id = topological_sort(g);

    vector<B> dp(n);

    rep(i, n) dp[i].a[0] = a[i];

    fore(i, id) {
        if(empty(g[i])) {
            OUT(dp[i].ans());
            break;
        }
        dump(dp[i].a);
        dp[i].shift();
        fore(e, g[i]) { dp[e].add(dp[i]); }
    }
}

详细

Test #1:

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

input:

3 2
1 1 1
1 2
2 3

output:

3

result:

ok 1 number(s): "3"

Test #2:

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

input:

6 8
1 1 4 5 1 4
1 4
1 5
2 3
2 5
3 4
4 5
4 6
5 6

output:

8

result:

ok 1 number(s): "8"

Test #3:

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

input:

5 6
7 2 3 6 6
1 2
1 4
2 3
3 4
3 5
4 5

output:

9

result:

ok 1 number(s): "9"

Test #4:

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

input:

7286 80481
637288250 628935175 588324396 766398783 663989874 865498593 695497968 630237220 19939888 448367842 412696777 111291257 304805809 585852799 58270069 391993802 606944382 827515045 389862501 643981354 160381074 324288921 257053597 980043955 417281046 870855665 360154617 60327683 966755927 55...

output:

7344

result:

ok 1 number(s): "7344"

Test #5:

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

input:

3485 69345
126919335 553087878 429357681 107790917 253972208 821989783 176128117 334722143 34989524 671942525 903789117 265616010 293291124 132216887 707703503 418751992 884191319 759015972 55466567 99122540 354621491 27107365 365748390 577882877 170254553 988104760 599408763 810052334 913007827 481...

output:

3552

result:

ok 1 number(s): "3552"

Test #6:

score: 0
Accepted
time: 20ms
memory: 7164kb

input:

4689 60385
379526147 320297565 290144701 411659092 808895255 349467622 912526797 945207025 147304164 611762485 690455633 593862503 349796612 915342714 685542590 669277310 101349964 538393690 204894013 843271709 963886266 502004930 826860359 607900073 348962474 135953765 54884804 334093983 550287693 ...

output:

4735

result:

ok 1 number(s): "4735"

Test #7:

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

input:

2025 13466
126370676 243460503 613061215 640378899 861853110 677538246 285817021 986777516 15580136 316176404 626530661 279208872 907269727 867851232 311220916 469724204 74428615 32310082 942939721 177419954 762441651 262073944 202700718 559297816 358074717 198943661 440995981 349418644 741410433 25...

output:

2067

result:

ok 1 number(s): "2067"

Test #8:

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

input:

121 199
896493283 602876262 190412813 539005355 361393375 336898107 57573022 860831181 796613695 697555446 474410296 573506583 646847982 775622764 433638751 410211736 706262484 308743162 694033299 787089776 557626422 410409760 666965625 606229683 774588923 213849175 739959306 318954654 193492757 624...

output:

153

result:

ok 1 number(s): "153"

Test #9:

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

input:

5173 82801
972178880 55787886 726219846 273505188 14833165 37678608 275433188 549410719 708739346 1596684 204453484 945870857 502585545 308710857 846757497 570148074 483775449 435833419 921907607 261743006 297037496 320466454 792975799 516106710 589464190 376914168 858666582 446429120 925757959 8940...

output:

5233

result:

ok 1 number(s): "5233"

Test #10:

score: 0
Accepted
time: 22ms
memory: 6696kb

input:

4451 68751
734143298 168118925 474566099 914135738 175189225 854565096 725921855 150734745 33215980 472643707 815730766 973287826 824133531 440222648 918212826 837679353 7350350 167875290 528553053 937486333 615587529 256295189 480035843 489273798 172853737 135811440 968971168 925087779 375176970 33...

output:

4514

result:

ok 1 number(s): "4514"

Test #11:

score: 0
Accepted
time: 13ms
memory: 4976kb

input:

2383 58762
509075060 867612403 21996053 527466121 553589454 671190283 764167183 890805913 862657253 922878788 744689887 170690927 609890819 40416579 350962700 185478150 75096314 104961883 754430815 421417968 778490115 468058225 431868239 303651678 581681467 384545786 339203220 559058549 941951463 37...

output:

2467

result:

ok 1 number(s): "2467"

Test #12:

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

input:

2001 67457
723197687 478813608 232810746 428945491 429136421 648852717 544540978 369132456 184435370 343975895 585223464 502157635 503001231 386021746 670322194 813441457 170390807 934181094 930734781 209174515 312012184 837865721 956809810 794424618 401909501 180498703 669648434 783741686 378051289...

output:

2102

result:

ok 1 number(s): "2102"

Test #13:

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

input:

9489 23514
202036366 696617402 38892756 337095695 965501524 948136577 612864889 980877872 264613206 879049290 604347121 920794549 640708344 587199470 185265444 98746351 581544222 927935481 643077470 812763147 38525503 654269070 256346123 899052544 92441646 264988076 66356031 292735115 922964957 3745...

output:

9521

result:

ok 1 number(s): "9521"

Test #14:

score: 0
Accepted
time: 33ms
memory: 14460kb

input:

9561 54289
98620438 516139680 658256456 902183713 624450302 546644345 93605161 13455288 650974201 859012671 775849200 862051631 839753906 309276811 504053052 106564927 917376230 455638053 499818452 348588006 293037448 892678488 109670419 100714558 133812448 189695864 759580899 825104942 225397418 66...

output:

9603

result:

ok 1 number(s): "9603"

Test #15:

score: 0
Accepted
time: 24ms
memory: 12384kb

input:

8673 41321
713852196 41672085 3578 76498661 193896234 880990702 92363180 297390233 877917509 781447228 559185392 628626089 309281217 479160612 538327677 495012807 230891929 947840408 760224668 4608905 590467332 636974494 563916969 835068689 590302247 655826457 532649798 125153559 521998114 401283582...

output:

8712

result:

ok 1 number(s): "8712"

Test #16:

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

input:

945 29713
352273400 865995982 176653085 727929352 606322564 54353211 560652092 950094104 35932743 672252257 608698156 714904143 464636830 446842133 793178441 165700304 587559973 210748888 475949161 525698243 604063691 220029563 329796511 754927076 272931162 465526795 932512075 597447389 346386244 67...

output:

1035

result:

ok 1 number(s): "1035"

Test #17:

score: 0
Accepted
time: 8ms
memory: 4580kb

input:

1601 43526
328657623 344662164 242182624 662893665 640466981 502913348 345497581 581438973 754099115 328827051 165687194 213281607 767975180 93816219 248298421 561877492 748929658 390895323 957073136 911088737 981351319 138160220 526659627 549050117 99010115 439572276 865895227 821112082 679833882 1...

output:

1692

result:

ok 1 number(s): "1692"

Test #18:

score: 0
Accepted
time: 24ms
memory: 11964kb

input:

8283 51577
942991980 827440413 493475089 923719536 882215143 372159216 789439259 268444689 192292947 207244213 764137033 10523178 947086322 393842511 834658105 514780495 317849618 422191383 819766666 854496034 788744341 703879894 802085844 216654060 446528313 426075265 739963640 984681283 368079365 ...

output:

8323

result:

ok 1 number(s): "8323"

Test #19:

score: 0
Accepted
time: 22ms
memory: 8908kb

input:

6535 61745
461883464 319967141 437979355 392664659 478849011 279140261 869286297 664092498 151063908 651453191 847691042 332414480 590562621 678469113 835313229 498830929 687038363 726868751 131933681 481236765 821004184 828928100 429856395 344702050 828374999 139181918 121900195 813753266 489199098...

output:

6579

result:

ok 1 number(s): "6579"

Test #20:

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

input:

1301 96001
360923566 504434045 568840592 149839072 672039632 442411522 70901261 641485201 511027097 242084208 945354144 993339776 118169822 39643622 183456405 133932691 954217434 865433622 809427908 633877391 342281786 230956039 463090342 328264297 242983917 158025716 769561000 893869221 468865309 9...

output:

1498

result:

ok 1 number(s): "1498"

Test #21:

score: 0
Accepted
time: 25ms
memory: 7020kb

input:

4561 81661
662903377 209601263 459486118 589651306 580105720 877383814 279419889 11603528 538188584 617963323 327968416 267897373 432601956 939666147 442933263 231175482 498451107 593220789 414643481 528796372 556876168 933236318 552381725 310145564 763857377 703961676 553121065 892630758 984717296 ...

output:

4638

result:

ok 1 number(s): "4638"

Test #22:

score: 0
Accepted
time: 13ms
memory: 10684kb

input:

7671 27811
344667830 466488589 713489595 881419363 661677016 253276616 503946555 612451519 697504054 829460968 303362367 14967005 250810714 164999614 267859557 98687834 331901038 637054868 185529097 48400948 923919143 149253468 528367994 678875310 612544713 613825552 653453269 190350776 347431071 38...

output:

7705

result:

ok 1 number(s): "7705"

Test #23:

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

input:

10000 100000
976525271 192759150 120002943 606091575 335395186 757161820 85210225 663267507 839752375 111342071 137956507 240887746 908844565 942226704 169309769 622850535 916415077 539498152 126155848 746028338 780441576 332328843 280663434 303859356 305461811 334859009 553084839 20315178 259007819...

output:

10099

result:

ok 1 number(s): "10099"

Test #24:

score: 0
Accepted
time: 11ms
memory: 11136kb

input:

10000 9999
406085210 282868337 287131237 127054463 392356693 62787819 834870109 285722192 628138017 131171447 31628498 440784019 550189258 974882404 158349512 464000012 303617766 10433009 328100146 345681501 142604978 723443663 741924030 871093537 18697590 566784538 108893137 878947863 798942228 253...

output:

10030

result:

ok 1 number(s): "10030"

Test #25:

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

input:

10000 9999
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...

output:

0

result:

ok 1 number(s): "0"

Test #26:

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

input:

10000 100000
999999996 999999998 999999999 999999991 999999999 999999991 999999998 999999994 999999996 999999997 999999991 1000000000 999999994 999999990 999999994 999999994 999999994 999999991 999999998 999999996 999999993 999999995 999999996 999999997 999999992 999999990 999999994 999999998 999999...

output:

10095

result:

ok 1 number(s): "10095"

Test #27:

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

input:

10000 9999
999999992 999999998 999999994 999999993 999999994 999999990 999999990 999999996 999999995 1000000000 999999993 999999999 999999998 999999996 999999995 999999992 999999993 999999997 999999996 999999995 999999996 999999997 999999997 999999998 999999997 999999996 999999993 999999990 99999999...

output:

10030

result:

ok 1 number(s): "10030"

Test #28:

score: 0
Accepted
time: 8ms
memory: 11100kb

input:

10000 9999
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...

output:

10000

result:

ok 1 number(s): "10000"

Test #29:

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

input:

10000 9999
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2...

output:

10001

result:

ok 1 number(s): "10001"

Test #30:

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

input:

2 1
0 0
2 1

output:

0

result:

ok 1 number(s): "0"

Test #31:

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

input:

2 1
0 1
2 1

output:

2

result:

ok 1 number(s): "2"

Test #32:

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

input:

2 1
1 0
2 1

output:

1

result:

ok 1 number(s): "1"