#include using namespace std; using Int = long long; template inline void chmin(T1 &a,T2 b){if(a>b) a=b;} template inline void chmax(T1 &a,T2 b){if(a vector compress(vector v){ sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end()); return v; } template map dict(const vector &v){ map res; for(int i=0;i<(int)v.size();i++) res[v[i]]=i; return res; } template struct BIT{ int n; vector bit; //1-indexed BIT():n(-1){} BIT(int n_,T d):n(n_),bit(n_+1,d){} T sum(int i){ T s=bit[0]; for(int x=i;x>0;x-=(x&-x)) s+=bit[x]; return s; } void add(int i,T a){ if(i==0) return; for(int x=i;x<=n;x+=(x&-x)) bit[x]+=a; } int lower_bound(int w){ if(w<=0) return 0; int x=0,r=1; while(r0;k>>=1){ if(x+k<=n&&bit[x+k]>n; vector a(n); for(int i=0;i>a[i]; auto c=compress(a); auto d=dict(c); for(int &x:a) x=d[x]; BIT bit(n,0); int cnt=0; for(int i=0;i