#include #include using namespace std; using ll = long long; #define rep(i,n) for(int i=0;i<(int)(n);i++) void solve(){ int n,m; cin>>n>>m; vector l(n),r(n); rep(i,n) cin>>l.at(i); rep(i,n) cin>>r.at(i); ll sml=0,smr=0; rep(i,n){ sml+=l.at(i); smr+=r.at(i); } if(m1){ ll md=(up+dw)/2; ll tmp=0; rep(i,n) tmp+=clamp(md,l.at(i),r.at(i)); if(tmp>m) up=md; else dw=md; } vector v(n); rep(i,n){ v.at(i)=clamp(dw,l.at(i),r.at(i)); m-=v.at(i); } vector p(n); rep(i,n) p.at(i)=i; sort(p.begin(),p.end(),[&](int i,int j){ return v.at(i) < v.at(j); }); for(int i:p){ if(m<=0) break; if(v.at(i)+1<=r.at(i)){ v.at(i)++; m--; } } reverse(v.begin(),v.end()); ll sm=0; ll ans=0; rep(i,n){ ans+=sm*v.at(i); sm+=v.at(i); } cout<>t; rep(i,t) solve(); }