#include <bits/stdc++.h>
#include "shortcut.h"
#define ff first
#define ss second
using namespace std;
long long find_shortcut(int N, vector<int> l, vector<int> d, int C) {
vector<long long> max_lft0(N,0),max_rt0(N,0),max_lft(N,0),max_rt(N,0),pos(N,0);
long long x =d[0], minD =0;
for(int i =0; i < N; i++) minD =max(minD,d[i]-1LL);
max_lft0[0] =max_lft[0] =d[0];
for(int i =1; i < N; i++) {
pos[i] =pos[i-1]+l[i-1];
max_lft0[i] =max(pos[i]+x,1LL*d[i]);
max_lft[i] =max(max_lft[i-1],pos[i]+d[i]+x);
x =max(x,d[i]-pos[i]);}
minD =max(minD,min(1LL*C,pos[N-1])-1LL);
x =d[N-1]+pos[N-1];
max_rt0[N-1] =max_rt[N-1] =d[N-1];
for(int i =N-2; i >= 0; i--) {
max_rt0[i] =max(-pos[i]+x,1LL*d[i]);
max_rt[i] =max(max_rt[i+1],-pos[i]+d[i]+x);
x =max(x,d[i]+pos[i]);}
srand(pos[N-1]+pos[N/2]);
for(int i =0; i < N; i++) minD =max(minD,min(pos[i],pos[N-1]-pos[i])-1);
vector<long long> posmd_suf(N), pospd_pref(N);
for(int i =0; i < N; i++) posmd_suf[i] =pos[i]-d[i], pospd_pref[i] =pos[i]+d[i];
for(int i =1; i < N; i++) pospd_pref[i] =max(pospd_pref[i],pospd_pref[i-1]);
for(int i =N-2; i >= 0; i--) posmd_suf[i] =min(posmd_suf[i],posmd_suf[i+1]);
long long maxD =max_lft[N-1];
vector< pair<long long,int> > st;
while(maxD-minD > 1) {
long long D =(minD+maxD)/2;
long long mindif =-D+C+max_lft[N-1], maxdif =1e18, minsum =-1e18, maxsum =1e18;
/*
pos[up]+d[up] > D+(pos[low]-d[low])
minsum == -D+C + max (pos[up]+d[up])+(pos[low]+d[low])
maxsum == D-C + min (pos[up]-d[up])+(pos[low]-d[low])
mindif == -D+C + max_lft[N-1]
maxdif == D-C + min (pos[up]-d[up])-(pos[low]+d[low])
*/
long long realD =0;
int a =-1;
x =1e18;
st.clear();
if(mindif > pos[N-1]) {minD =D; continue;}
for(int i =N-1; i >= 0; i--) {
x =D+pos[i]-d[i];
while(a < (int)st.size()-1 && st[a+1].ff > x) a++;
if(a >= 0) {
maxsum =min(maxsum,D-C+pos[i]-d[i]+posmd_suf[st[a].ss]);
maxdif =min(maxdif,D-C-pos[i]-d[i]+posmd_suf[st[a].ss]);
}
if(a < (int)st.size()-1) realD =max(realD,st[a+1].ff-pos[i]+d[i]);
if(maxdif < max(0LL,mindif) || maxsum < 0) break;
while(!st.empty() && st.back().ff <= pos[i]+d[i]) st.pop_back();
a =min(a,(int)st.size()-1);
st.push_back(make_pair(pos[i]+d[i],i));
}
if(maxdif < mindif || maxsum < 0) {minD =D; continue;}
a =-1;
x =-1e18;
st.clear();
for(int i =0; i < N; i++) {
x =pos[i]+d[i]-D;
while(a < (int)st.size()-1 && st[a+1].ff < x) a++;
if(a >= 0)
minsum =max(minsum,-D+C+pos[i]+d[i]+pospd_pref[st[a].ss]);
while(!st.empty() && st.back().ff >= pos[i]-d[i]) st.pop_back();
a =min(a,(int)st.size()-1);
st.push_back(make_pair(pos[i]-d[i],i));
}
if(maxsum < minsum) {minD =D; continue;}
bool ok =false;
int sumlft =N-1, sumrt =N-1, diflft =0, difrt =0;
for(int i =0; i < N; i++) {
while(diflft < N && pos[diflft] < pos[i]+mindif) diflft++;
while(difrt < N && pos[difrt] <= pos[i]+maxdif) difrt++;
while(sumrt >= 0 && pos[sumrt]+pos[i] > maxsum) sumrt--;
while(sumlft >= 0 && pos[sumlft]+pos[i] >= minsum) sumlft--;
if(diflft == N || sumrt < 0) break;
int lft =max(i,max(diflft,sumlft+1)), rt =min(difrt-1,sumrt);
if(lft <= rt) {
realD =max(realD,D+(pos[(lft+rt)/2]+pos[i])-maxsum);
realD =max(realD,D-(pos[(lft+rt)/2]+pos[i])+minsum);
realD =max(realD,D-(pos[(lft+rt)/2]-pos[i])+mindif);
realD =max(realD,D+(pos[(lft+rt)/2]-pos[i])-maxdif);
D =realD;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