INF=10**18+1; #bool chmin(ll&a,ll b){return a>b?a=b,true:false;} def check(X): b=[] for i in range(K-1,-1,-1): if B[i]>=X: b+=[K-i] if len(b)==0: return N=X M=b[0] dist=[INF for i in range(K+M)] seen=[False for i in range(K+M)] for i in range(K): if A[i]>=X: dist[i]=i for i in range(K+M): idx=-1 mn=INF+1 for j in range(K+M): if mn>dist[j] and (not seen[j]): idx=j mn=dist[j] seen[idx]=True for j in b: if idx=K: dist[K+(idx+j)%M]=min(dist[K+(idx+j)%M],dist[idx]+j) else: dist[K+(idx-K+j)%M]=min(dist[K+(idx-K+j)%M],dist[idx]+j) if N1: mid=(ok+ng)//2 if check(mid): ok=mid else: ng=mid print(ok)