import bisect import itertools def is_ok(v): S = list(range(len(D[0]))) for i in range(N-1): ND = D[i+1] IMOS = [0]*(len(ND)+1) for s in S: d = D[i][s] l = bisect.bisect_left(ND,d) r = bisect.bisect_right(ND,d+v) IMOS[l]+=1 IMOS[r]-=1 NS = [] L = list(itertools.accumulate(IMOS)) for j,l in enumerate(L): if l>0 and j 1): mid = (ok + ng) // 2 if is_ok(mid): ok = mid else: ng = mid if ok == 10**9+100: print(-1) else: print(ok)