#include #include #include using namespace std; typedef long long ll; typedef pair P; ll N,L,X[100010],Y[100010]; int use[100010] = {}; vector

v; ll del(ll x,ll y){ if(x> N >> L; for(int i=1;i<=N;i++){ cin >> X[i]; } for(int i=1;i<=N;i++){ cin >> Y[i]; } for(int i=1;i<=N;i++){ v.push_back(P(X[i],1)); v.push_back(P(Y[i],-1)); } sort(v.begin(),v.end()); ll ans = 0,now_d = 0; int cnt = 0,now = 0; bool sushi = false; while(cnt<2*N){ if(!sushi){ for(int j=0;j<=4*N;j++){ if(use[(now+j)%(2*N)]==1) continue; if(v[(now+j)%(2*N)].second==1){ sushi = true; ans += del(now_d,v[(now+j)%(2*N)].first); now_d = v[(now+j)%(2*N)].first; use[(now+j)%(2*N)] = 1; now = (now+j)%(2*N); break; } } }else{ int ocha_id = -1; for(int j=0;j<=4*N;j++){ if(use[(now+j)%(2*N)]==1) continue; if(ocha_id!=-1 && v[(now+j)%(2*N)].second==1){ ans += del(now_d,v[ocha_id].first); now_d = v[ocha_id].first; now = ocha_id; use[ocha_id] = 1; sushi = false; break; } if(v[(now+j)%(2*N)].second==-1){ ocha_id = (now+j)%(2*N); if(cnt==2*N-1){ ans += del(now_d,v[ocha_id].first); now_d = v[ocha_id].first; now = ocha_id; use[ocha_id] = 1; sushi = false; break; } } } } cnt++; } cout << ans << endl; }