Wrong Answer.wrong answer 1st numbers differ - expected: '1000', found: '500000500'
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,x,y) for(ll i=x;i<=y;i++)
#define pll pair<ll,ll>
#define pause system("pause")
#define IL inline
const ll N=2e5+10,MAX=1e9+1000,S=41;
ll n,goal,st[N],t[N],pow2[S];
bool done;
void bs(ll l,ll r) {
if(done or l>r) return;
// cout<<l<<' '<<r<<'\n';
ll mid=l+r>>1ll,ans=0;
// if(mid>goal) {
// bs(l,mid-1);
// return;
// }
rep(i,1,n) {
if(mid>=st[i]+t[i]) {
ll tt=mid-t[i]-st[i]+1ll;
if(tt<S and tt<goal and pow2[tt]<=goal) ans+=pow2[tt];
else {
bs(l,mid-1ll);
return;
}
} else if(mid>=st[i]) ans++;
if(ans>goal) {
bs(l,mid-1ll);
return;
}
}
// if(mid==5) {
// cout<<"mid="<<mid<<'\n'<<"ans="<<ans;
// pause;
// }
if(ans==goal) {
cout<<mid;
done=1;
return;
}
bs(mid+1ll,r);
}
int main() {
pow2[0]=1ll;
rep(i,1,S-1) pow2[i]=pow2[i-1]<<1ll;
// rep(i,0,S-1) cout<<pow2[i]<<' ';
//
// pause;
cin>>n>>goal;
rep(i,1,n) cin>>st[i];
rep(i,1,n) cin>>t[i];
bs(1,MAX);
if(done==0) cout<<"-1";
}