#include #include using namespace std; constexpr long long INF=2002002002002002002LL; constexpr double PI=3.14159265358979323846; constexpr long long dx[]={ 1, 0, -1, 0, 1, -1, -1, 1, 0}; constexpr long long dy[]={ 0, 1, 0, -1, 1, 1, -1, -1, 0}; constexpr string yesno[]={"No\n","Yes\n"}; constexpr char DRUL[]="DRUL"; constexpr unsigned long long TEN[]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000,10000000000,100000000000,1000000000000,10000000000000,100000000000000,1000000000000000,10000000000000000,100000000000000000,1000000000000000000,10000000000000000000ULL}; using ll=long long; using ull=unsigned long long; using mint=atcoder::modint998244353; template using min_priority_queue=priority_queue,greater>; template bool chmax(T &a, const T &b) { if (a bool chmin(T &a, const T &b) { if (b pair operator+(const pair &lhs, const pair &rhs) { return {lhs.first+rhs.first,lhs.second+rhs.second}; } template pair operator-(const pair &lhs, const pair &rhs) { return {lhs.first-rhs.first,lhs.second-rhs.second}; } template pair &operator+=(pair &lhs, const pair &rhs) { return lhs=lhs+rhs; } template pair &operator-=(pair &lhs, const pair &rhs) { return lhs=lhs-rhs; } template pair operator*(const pair &lhs, const T &rhs) { return {lhs.first*rhs,lhs.second*rhs}; } template pair operator*(const T &lhs, const pair &rhs) { return {lhs*rhs.first,lhs*rhs.second}; } template pair operator/(const pair &lhs, const T &rhs) { return {lhs.first/rhs,lhs.second/rhs}; } template T operator*(const pair &lhs, const pair &rhs) { return lhs.first*rhs.second-rhs.first*lhs.second; } template inline T hypotsq(const pair &p) { return p.first*p.first+p.second*p.second; } template inline double hypot(const pair &p) { return hypot(p.first,p.second); } template inline T hypotsq(T x, T y) { return x*x+y*y; } template inline T hypotsq(T px, T py, T qx, T qy) { return hypotsq(qx-px,qy-py); } template inline double hypot(T px, T py, T qx, T qy) { return hypot(qx-px,qy-py); } template istream &operator>>(istream &is, atcoder::static_modint &x) { long long _x; is>>_x; x=_x; return is; } template ostream &operator<<(ostream &os, const atcoder::static_modint &x) { return os< istream &operator>>(istream &is, atcoder::dynamic_modint &x) { long long _x; is>>_x; x=_x; return is; } template ostream &operator<<(ostream &os, const atcoder::dynamic_modint &x) { return os<>(istream &is, __int128_t &x) { string S; is>>S; x=0; bool neg=false; for (auto &c : S) { if (c=='-') neg=true; else x=x*10+c-'0'; } if (neg) x=-x; return is; } ostream &operator<<(ostream &os, __int128_t x) { if (x==0) return os<<0; if (x<0) os<<'-', x=-x; string S; while (x) S.push_back('0'+x%10), x/=10; reverse(begin(S),end(S)); return os<>(istream &is, __uint128_t &x) { string S; is>>S; x=0; for (auto &c : S) x=x*10+c-'0'; return is; } ostream &operator<<(ostream &os, __uint128_t x) { if (x==0) return os<<0; string S; while (x) S.push_back('0'+x%10), x/=10; reverse(begin(S), end(S)); return os< istream &operator>>(istream &is, pair &p); template ostream &operator<<(ostream &os, const pair &p); template istream &operator>>(istream &is, tuple &t); template ostream &operator<<(ostream &os, const tuple &t); template istream &operator>>(istream &is, vector &v); template ostream &operator<<(ostream &os, const vector &v); template ostream &operator<<(ostream &os, const optional &x); template istream &operator>>(istream &is, pair &p) { return is>>p.first>>p.second; } template ostream &operator<<(ostream &os, const pair &p) { return os< istream &operator>>(istream &is, tuple &t) { apply([&](auto &...args) { ((is>>args), ...); },t); return is; } template ostream &operator<<(ostream &os, const tuple &t) { apply([&](auto &...args) { int n=0; ((os<<(n++?" ":"")< istream &operator>>(istream &is, vector &v) { for (auto &x : v) is>>x; return is; } template ostream &operator<<(ostream &os, const vector &v) { for (int i=0,s=(int)v.size();i ostream &operator<<(ostream &os, const set &v) { auto it=v.begin(); while (it!=v.end()) os<<*it++<<(it==v.end()?"":" "); return os; } template ostream &operator<<(ostream &os, const unordered_set &v) { auto it=v.begin(); while (it!=v.end()) os<<*it++<<(it==v.end()?"":" "); return os; } template ostream &operator<<(ostream &os, const multiset &v) { auto it=v.begin(); while (it!=v.end()) os<<*it++<<(it==v.end()?"":" "); return os; } template ostream &operator<<(ostream &os, const list &v) { auto it=v.begin(); while (it!=v.end()) os<<*it++<<(it==v.end()?"":" "); return os; } template ostream &operator<<(ostream &os, const stack &v) { stack u=v; while (!u.empty()) { os< ostream &operator<<(ostream &os, const queue &v) { queue u=v; while (!u.empty()) { os< ostream &operator<<(ostream &os, const deque &v) { deque u=v; while (!u.empty()) { os< ostream &operator<<(ostream &os, const priority_queue &v) { priority_queue u=v; while (!u.empty()) { os< ostream &operator<<(ostream &os, const map &m) { auto it=m.begin(); while (it!=m.end()) os<<(it->first)<<':'<<(it++->second)<<(it==m.end()?"":" "); return os; } template ostream &operator<<(ostream &os, const unordered_map &m) { auto it=m.begin(); while (it!=m.end()) os<<(it->first)<<':'<<(it++->second)<<(it==m.end()?"":" "); return os; } template ostream &operator<<(ostream &os, const optional &x) { if (x.has_value()) return os< inline T floor(const T &a, const U &b) { return a/b-(a%b && (a^b)< 0); } template inline T ceil (const T &a, const U &b) { return a/b+(a%b && (a^b)>=0); } inline double radian(double deg) { return (PI/180.0)*deg; } inline double degree(double rad) { return (180.0/PI)*rad; } inline long long popcount(unsigned long long x) { return __builtin_popcountll(x); } inline long long height(unsigned long long x) { if (x==0) return 0LL; return 64LL-__builtin_clzll(x); } inline long long ctz(unsigned long long x) { return __builtin_ctzll(x); } inline long long lsb(long long b) { return b&-b; } template long long sgn(const T &x) { if (x<0) return -1; return x>0; } template inline typename enable_if,T>::type rem(T a, T b) { assert(b>0); T res=a%b; if (res<0) res+=b; return res; } template inline typename enable_if,T>::type rem(T a, T b) { assert(b>0); T res=a-floor(a/b)*b; if (res<0.0) res+=b; return res; } template inline bool inlr(const T &l, const T &x, const T &r) noexcept { return (l<=x && x bool isinc(T first, T second) { return first<=second; } template bool isinc(T first, T second, const Rest ...rest) { return first<=second && isinc(second,rest...); } long long safe_mul() noexcept { return 1LL; } template long long safe_mul(long long a, Rest ...rest) noexcept { if (a==0LL) return 0LL; long long b=safe_mul(rest...); if (b==0LL) return 0LL; if (INF/a/b==0LL) return ((0LL void println(T item) { cout< void println(T item, Rest ...rest) { cout< void dump(T item) { cerr< void dump(T item, Rest ...rest) { cerr<=INF || b>=INF) return INF; return safe_mul(a/gcd(a,b),b); } template vector mkiota(int n, Res val=0) { vector res(n); iota(begin(res),end(res),val); return res; } template T mkrev(const T &v) { T res=v; reverse(res.begin(),res.end()); return res; } template vector mksum(const vector &v) { int s=v.size(); vector res(s+1); for (int i=0;i vector mkdif(const vector &v) { int s=v.size(); vector res(s-1); for (int i=0;i+1 vector mkuni(const vector &v) { auto res=v; sort(res.begin(),res.end()); res.erase(unique(res.begin(),res.end()),res.end()); return res; } template vector mkinv(const vector &v) { vector res(*max_element(v.begin(),v.end())+1,-1); for (int i=0,s=v.size();i vector mkord(int n, F f) { vector res(n); iota(res.begin(),res.end(),0); stable_sort(res.begin(),res.end(),f); return res; } template map mkcnt(const vector &v) { map res; for (const auto &x : v) res[x]++; return res; } template vector> mksplit(const vector &v, F f) { if (v.size()==0) return {}; vector> res{{v[0]}}; for (int i=0,s=v.size();i+1 vector> mkrle(const vector &v) { int n=v.size(); vector> res; for (int i=0;i> mkrle(const string &s) { int n=s.size(); vector> res; for (int i=0;i atcoder::fenwick_tree mkfen(const vector &v) { int s=v.size(); atcoder::fenwick_tree res(s); for (int i=0;i T min(const vector &v) { return *min_element(v.begin(),v.end()); } template T max(const vector &v) { return *max_element(v.begin(),v.end()); } template inline void sort(T &v, bool rev=false) { rev ? sort(v.rbegin(),v.rend()) : sort(v.begin(),v.end()); } template long long count(const T &v, const U &item) { return count(v.begin(),v.end(),item); } template inline long long count_in_range(const vector &v, const T &l, const T &r) { return lower_bound(v.begin(),v.end(),r)-lower_bound(v.begin(),v.end(),l); } template inline long long lob(const vector &v, const T &x) { return lower_bound(v.begin(),v.end(),x)-v.begin(); } template inline long long upb(const vector &v, const T &x) { return upper_bound(v.begin(),v.end(),x)-v.begin(); } template optional minel(const vector &v, size_t k=0) { if (v.size()<=k) return nullopt; return mkord(v.size(),[&](long long a, long long b)->bool { return v[a] optional maxel(const vector &v, size_t k=0) { if (v.size()<=k) return nullopt; return mkord(v.size(),[&](long long a, long long b)->bool { return v[a]>v[b]; })[k]; } template T flatten(const vector &vv) { T res; for (const T &v : vv) res.insert(res.end(),v.begin(),v.end()); return res; } vector binary_split(long long x) { assert(x>=0); vector res; long long k=1; while (x>0) { long long y=min(k,x); res.push_back(y); x-=y; k<<=1; } return res; } template T arithmetic_series(T a, T d, T n) { return (a*2+(n-1)*d)*n/2; } template long long hamming_distance(const T &a, const T &b) { assert(a.size()==b.size()); auto ita=a.begin(); auto itb=b.begin(); int res=0; while (ita!=a.end()) if (*ita++!=*itb++) res++; return res; } template long long get_period(const T &v) { int n=v.size(); vector z=atcoder::z_algorithm(v); for (int i=1;i<=n/2;++i) if (n%i==0 && z[i]==n-i) return i; return n; } template void cumulative_min(vector &v, bool ltr) { int n=v.size(); if (ltr) for (int i=0;i+1=0;--i) v[i]=min(v[i],v[i+1]); } template void cumulative_max(vector &v, bool ltr) { int n=v.size(); if (ltr) for (int i=0;i+1=0;--i) v[i]=max(v[i],v[i+1]); } template void repeat_vector(vector &v, int count=2) { assert(count>=0); if (count==0) { v.clear(); return; } count--; int original_size=v.size(); while (count--) v.insert(v.end(),v.begin(),v.begin()+original_size); } template long long longest_continuum(const vector &v) { if (v.empty()) return 0; long long res=1; long long now=1; int n=v.size(); for (int i=1;i long long longest_continuum(const vector &v, const T &item) { long long res=0; long long now=0; for (auto &x : v) if (x==item) chmax(res,++now); else now=0; return res; } template long long lis_len(const vector &v, bool strict=true) { vector dp; for (auto &x : v) { auto it=strict ? lower_bound(dp.begin(),dp.end(),x) : upper_bound(dp.begin(),dp.end(),x); if (it==dp.end()) dp.push_back(x); else *it=x; } return dp.size(); } template long long inversions(vector &v) { map cc; for (auto &x : v) cc[x]=0; int M=0; for (auto &[key,val] : cc) val=M++; long long res=0; atcoder::fenwick_tree fen(M); for (auto &x : v) { int y=cc[x]; res+=fen.sum(y+1,M); fen.add(y,1); } return res; } long long ctok(char d) { switch (d) { case 'D': return 0; case 'R': return 1; case 'U': return 2; case 'L': return 3; } return -1; } template T min_op(T a, T b) { return min(a,b); } template T max_op(T a, T b) { return max(a,b); } template using min_segtree=atcoder::segtree,numeric_limits::max>; template using max_segtree=atcoder::segtree,numeric_limits::lowest>; template class FenwickTree { private: int _n; std::vector data; U sum(int r) const { U s=U(0); while (r>0) { s+=data[r-1]; r-=r&-r; } return s; } int max_right(U x, bool eq) const { if (_n==0) return 0; int d=1<<(31-__builtin_clz(_n)); int i=0; U now=0; while (d) { int j=i+d-1; if (j<_n && (eq ? now+data[j]<=x : now+data[j]>=1; } return i; }; public: FenwickTree() : _n(0) {} FenwickTree(int n) : _n(n), data(n) {} FenwickTree(const std::initializer_list &init) : _n(init.size()), data(init.size()) { auto it=init.begin(); int i=0; while (it!=init.end()) add(i++,*it++); } FenwickTree(const std::vector &v) : _n(v.size()), data(v.size()) { for (int i=0;i<_n;++i) add(i,v[i]); } void add(int p, U x) { assert(0<=p && p<_n); p++; while (p<=_n) { data[p-1]+=x; p+=p&-p; } } void set(int p, U x) { add(p,x-sum(p,p+1)); } U sum(int l, int r) const { assert(0<=l && l<=r && r<=_n); return sum(r)-sum(l); } int max_right(int l, U x, bool eq) const { assert(0<=l && l<=_n); if (l==0) return max_right(x,eq); return max_right(sum(l)+x,eq); } int min_left(int r, U x, bool eq) const { assert(0<=r && r<=_n); U sum_r=sum(r); if (eq ? sum_r<=x : sum_r class PrioritySum { private: using C=long long; C _all_cnt=0; T _all_sum=0; FenwickTree cnt_fen; FenwickTree sum_fen; std::vector cc; bool initialized=false; void check_cc() const { for (auto x : cc) assert(x>=T(0)); } public: PrioritySum() {} PrioritySum(const std::initializer_list &init) : cc(init) { if (!ALLOWS_NEGATIVE) check_cc(); } PrioritySum(const std::vector &v) : cc(v) { if (!ALLOWS_NEGATIVE) check_cc(); } void prepare(T x) { assert(!initialized); assert(ALLOWS_NEGATIVE || x>=T(0)); cc.push_back(x); } void prepare(const std::vector &v) { for (auto x : v) prepare(x); } void init() { std::sort(cc.begin(),cc.end()); cc.erase(std::unique(cc.begin(),cc.end()),cc.end()); cnt_fen=FenwickTree(cc.size()); sum_fen=FenwickTree(cc.size()); initialized=true; } void add(T x, C count=1) { assert(initialized); unsigned int idx=std::lower_bound(cc.begin(),cc.end(),x)-cc.begin(); assert(idx medians() const { assert(initialized); if (_all_cnt==0) return {}; if (_all_cnt%2==1) return {(*this)[_all_cnt/2]}; return {(*this)[_all_cnt/2-1],(*this)[_all_cnt/2]}; } T smallest_sum(C count) const { assert(initialized); if (_all_cnt<=count) return _all_sum; int k=cnt_fen.max_right(0,count,true); return sum_fen.sum(0,k)+cc[k]*(count-cnt_fen.sum(0,k)); } T largest_sum(C count) const { assert(initialized); if (_all_cnt<=count) return _all_sum; return _all_sum-smallest_sum(_all_cnt-count); } T all_sum() const { assert(initialized); return _all_sum; } C size() const { assert(initialized); return _all_cnt; } void clear() { assert(initialized); cnt_fen.clear(); sum_fen.clear(); _all_cnt=0; _all_sum=0; } C count_lt(T x) const { return cnt_fen.sum(0,std::lower_bound(cc.begin(),cc.end(),x)-cc.begin()); } C count_leq(T x) const { return cnt_fen.sum(0,std::upper_bound(cc.begin(),cc.end(),x)-cc.begin()); } C count_gt(T x) const { return _all_cnt-count_leq(x); } C count_geq(T x) const { return _all_cnt-count_lt(x); } std::optional nearest_geq(T x) const { unsigned int idx=std::lower_bound(cc.begin(),cc.end(),x)-cc.begin(); unsigned int k=cnt_fen.max_right(idx,1,false); if (k==cc.size()) return std::nullopt; return cc[k]; } std::optional nearest_gt(T x) const { unsigned int idx=std::upper_bound(cc.begin(),cc.end(),x)-cc.begin(); unsigned int k=cnt_fen.max_right(idx,1,false); if (k==cc.size()) return std::nullopt; return cc[k]; } std::optional nearest_leq(T x) const { unsigned int idx=std::upper_bound(cc.begin(),cc.end(),x)-cc.begin(); unsigned int k=cnt_fen.min_left(idx,1,false); if (k==0) return std::nullopt; return cc[k-1]; } std::optional nearest_lt(T x) const { unsigned int idx=std::lower_bound(cc.begin(),cc.end(),x)-cc.begin(); unsigned int k=cnt_fen.min_left(idx,1,false); if (k==0) return std::nullopt; return cc[k-1]; } std::vector nearest_both(T x) const { std::optional lt=nearest_lt(x); std::optional geq=nearest_geq(x); T dist=std::numeric_limits::max(); if (lt!=std::nullopt && x-lt.value() res; if (lt!=std::nullopt && x-lt.value()==dist) res.push_back(lt.value()); if (geq!=std::nullopt && geq.value()-x==dist) res.push_back(geq.value()); return res; } C max_count_leq_volume(T volume) const requires(!ALLOWS_NEGATIVE) { assert(T(0)<=volume); if (volume>=_all_sum) return _all_cnt; int k=sum_fen.max_right(0,volume,true); return cnt_fen.sum(0,k)+C((volume-sum_fen.sum(0,k))/cc[k]); }; C min_count_geq_volume(T volume) const requires(!ALLOWS_NEGATIVE) { assert(T(0)<=volume && volume<=_all_sum); return _all_cnt-max_count_leq_volume(_all_sum-volume); } friend std::ostream &operator<<(std::ostream &os, const PrioritySum &v) { bool spacing=false; for (unsigned int i=0;i>N>>B; vector C(N),S(N); cin>>C>>S; PrioritySum ps(C); ps.prepare(1); ps.init(); for (ll i=0;i