Codeforces Round#200(Div.1)C.Read Time二分


/**
Codeforces Round #200 (Div. 1) C. Read Time   
  :https://codeforces.com/contest/343/problem/C
  :n    m    n                   m        ;

   :
                                                    ;
                  r                              ;

******tricks*********
     n            ;
  0          ;    0           ;

*/

#include
#define ll long long
using namespace std;

const int maxn=1e5+7;
ll h[maxn],p[maxn];
int n,m;

bool judge(ll x){
	int l=1,r=1;
	for(int i=1;i<=n;i++){
		ll tmp=abs(p[r]-p[l])+min(abs(p[r]-h[i]),abs(p[l]-h[i]));
		while(r<=m&&tmp<=x){
			r++;
			tmp=abs(p[r]-p[l])+min(abs(p[r]-h[i]),abs(p[l]-h[i]));
		}
		l=r;
		if(r==m+1) return true;
	}
	return false;
}


int main (){
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;i++) scanf("%lld",&h[i]);
	for(int i=1;i<=m;i++) scanf("%lld",&p[i]);
	if(judge(0)) {
		cout<<0<1){
		mid=(l+r)/2;
		if(judge(mid)) r=mid;
		else l=mid;
	}
	printf("%lld
",r); return 0; }