#include using namespace std; using ll=long long; using ull=unsigned long long; using P=pair; templateusing minque=priority_queue,greater>; templatebool chmax(T &a,const T &b){return (abool chmin(T &a,const T &b){return (a>b?(a=b,true):false);} templateistream &operator>>(istream &is,pair&p){is>>p.first>>p.second;return is;} templateistream &operator>>(istream &is,tuple&a){is>>std::get<0>(a)>>std::get<1>(a)>>std::get<2>(a);return is;} templateistream &operator>>(istream &is,array&a){for(auto&i:a)is>>i;return is;} templateistream &operator>>(istream &is,vector &a){for(auto &i:a)is>>i;return is;} templatevoid operator++(pair&a,int n){a.first++,a.second++;} templatevoid operator--(pair&a,int n){a.first--,a.second--;} templatevoid operator++(vector&a,int n){for(auto &i:a)i++;} templatevoid operator--(vector&a,int n){for(auto &i:a)i--;} #define overload3(_1,_2,_3,name,...) name #define rep1(i,n) for(int i=0;i<(int)(n);i++) #define rep2(i,l,r) for(int i=(int)(l);i<(int)(r);i++) #define rep(...) overload3(__VA_ARGS__,rep2,rep1)(__VA_ARGS__) #define reps(i,l,r) rep2(i,l,r) #define all(x) x.begin(),x.end() #define pcnt(x) __builtin_popcountll(x) #define fin(x) return cout<<(x)<<'\n',static_cast(0) #define yn(x) cout<<((x)?"Yes\n":"No\n") #define uniq(x) sort(all(x)),x.erase(unique(all(x)),x.end()) template inline int fkey(vector&z,T key){return lower_bound(z.begin(),z.end(),key)-z.begin();} ll myceil(ll a,ll b){return (a+b-1)/b;} template auto vec(const int (&d)[n],const T &init=T()){ if constexpr (id(d,init)); else return init; } #ifdef LOCAL #include #define SWITCH(a,b) (a) #else #define debug(...) static_cast(0) #define debugg(...) static_cast(0) #define SWITCH(a,b) (b) templateostream &operator<<(ostream &os,const pair&p){os<>testcase; for(int i=0;i #include template constexpr std::enable_if_t::digits<=32,int>msb(T n){return n==0?-1:31-__builtin_clz(n);} template constexpr std::enable_if_t<(std::numeric_limits::digits>32),int>msb(T n){return n==0?-1:63-__builtin_clzll(n);} template constexpr std::enable_if_t::digits<=32,int>lsb(T n){return n==0?-1:__builtin_ctz(n);} template constexpr std::enable_if_t<(std::numeric_limits::digits>32),int>lsb(T n){return n==0?-1:__builtin_ctzll(n);} template constexpr std::enable_if_t,T>floor_pow2(T n){return n==0?0:T(1)< constexpr std::enable_if_t,T>ceil_pow2(T n){return n<=1?1:T(1)<<(msb(n-1)+1);} template constexpr T safe_div(T a,T b){return a/b-(a%b&&(a^b)<0);} template constexpr T safe_ceil(T a,T b){return a/b+(a%b&&(a^b)>0);} template struct SegmentTree{ using S=typename M::S; private: int n,z; std::vectordat; public: SegmentTree():n(0),z(0),dat(){} explicit SegmentTree(int n):n(n),z(ceil_pow2(n)),dat(ceil_pow2(n)*2,M::e()){} explicit SegmentTree(const std::vector&a):n(a.size()),z(ceil_pow2((int)a.size())){ dat.resize(z*2,M::e()); for(int i=0;i=1;i--)dat[i]=M::op(dat[i*2],dat[i*2+1]); } SegmentTree(int n,S init):SegmentTree(std::vector(n,init)){} inline S get(int i)const{return dat[i+z];} void set(int i,S x){ assert(0<=i&&i>=1; while(i){ dat[i]=M::op(dat[i*2],dat[i*2+1]); i>>=1; } } S prod(int l,int r)const{ assert(0<=l&&l<=r&&r<=n); l+=z,r+=z; S left=M::e(),right=M::e(); while(l>=1,r>>=1; } return M::op(left,right); } inline S all_prod()const{return dat[1];} template int max_right(int l,const Func&f)const{ if(l==n)return n; l+=z; S now=M::e(); do{ while((~l)&1)l>>=1; S nxt=M::op(now,dat[l]); if(f(nxt))now=nxt,l++; else{ while(l int min_left(int r,const Func&f)const{ if(r==0)return 0; r+=z; S now=M::e(); do{ r--; while(r>1&&(r&1))r>>=1; S nxt=M::op(dat[r],now); if(f(nxt))now=nxt; else{ while(r>n>>m>>k>>p; vectora(n),c(m); vectorb(n),d(m); cin>>a>>b>>c>>d; b--,d--; vectors(k); cin>>s; vectorz(c); uniq(z); vectororda(n); iota(all(orda),0); sort(all(orda),[&](int lhs,int rhs){return b[lhs]ordb(m); iota(all(ordb),0); sort(all(ordb),[&](int lhs,int rhs){return d[lhs]>>cdcol(k); rep(i,m)cdcol[d[i]].emplace_back(c[i],i); rep(i,k)sort(all(cdcol[i])); vectorkeyc(m); rep(i,m)keyc[i]=fkey(z,c[i]); auto judge=[&](ll x)->tuple { ll res=0; ll mx=-1; int iv=-1,jv=-1; { SegmentTreeseg(z.size()); int ptr=0; for(int i:orda){ while(ptrseg(z.size()); int ptr=0; for(int i:orda){ while(ptrb[i]){ auto now=seg.get(keyc[ordb[ptr]]); now.sz++; now.mx=c[ordb[ptr]]; now.idx=ordb[ptr]; seg.set(keyc[ordb[ptr]],now); ptr++; } auto now=seg.prod(0,fkey(z,x-a[i]+1)); if(now.sz){ res+=now.sz; if(chmax(mx,now.mx+a[i])){ iv=i; jv=now.idx; } } } } reverse(all(orda)),reverse(all(ordb)); rep(i,n){ int ncol=b[i]; ll ns=s[ncol]; //a[i]+c[i]-ns<=x //c[i]<=x+ns-a[i] auto itr=upper_bound(all(cdcol[ncol]),pair{x+ns-a[i],(int)1e9}); if(itr!=cdcol[ncol].begin()){ res+=itr-cdcol[ncol].begin(); itr--; if(chmax(mx,itr->first+a[i]-ns)){ iv=i; jv=itr->second; } } } return {iv,jv,res}; }; // { // auto [iv,jv,res]=judge(126); // debug(res,iv,jv); // return; // } ll ok=1e10,ng=-1; while(ok-ng>1){ ll mid=(ok+ng)/2; ll cnt=get<2>(judge(mid)); if(cnt>=p)ok=mid; else ng=mid; } auto [ansi,ansj,ans]=judge(ok); debug(ans,ok); cout<