#include using namespace std; using Int = long long; template inline void chmin(T1 &a,T2 b){if(a>b) a=b;} template inline void chmax(T1 &a,T2 b){if(a void induce(Int l,Int r,Int a,Int b,vector &dp,F dist){ if(l==r) return; Int m=(l+r)>>1; Int &idx=(dp[m]=a); T res=dist(idx,m); for(Int i=a;i(l,m,a,idx+1,dp,dist); induce(m+1,r,idx,b,dp,dist); } template vector args(Int n,F dist){ vector dp(n,-1); induce(0,n,0,n,dp,dist); return dp; } } template struct SegmentTree{ using H = function; Int n,height; H h; E ei; vector laz; SegmentTree(H h,E ei):h(h),ei(ei){} void init(Int n_){ n=1;height=0; while(n>i); } void update(Int a,Int b,E x){ thrust(a+=n); thrust(b+=n-1); for(Int l=a,r=b+1;l>=1,r>>=1){ if(l&1) laz[l]=h(laz[l],x),l++; if(r&1) --r,laz[r]=h(laz[r],x); } } E get_val(Int a){ thrust(a+=n); return laz[a]; } void set_val(Int a,E x){ thrust(a+=n); laz[a]=x; } }; struct FastIO{ FastIO(){ cin.tie(0); ios::sync_with_stdio(0); } }fastio_beet; using i16 = int16_t; using i32 = int32_t; using i64 = int64_t; using u16 = uint16_t; using u32 = uint32_t; using u64 = uint64_t; // AtCoder const i64 CYCLES_PER_SEC = 2200000000; struct Timer { i64 start; Timer(){reset();} void reset(){start=getCycle();} inline double get(){return (double)(getCycle()-start)/CYCLES_PER_SEC;} inline i64 getCycle(){ u32 low,high; __asm__ volatile ("rdtsc" : "=a" (low), "=d" (high)); return ((i64)low)|((i64)high<<32); } }; Timer timer; //INSERT ABOVE HERE signed main(){ Int n; cin>>n; vector as(n); for(Int i=0;i>as[i]; const Int INF = 1e18; auto g=[&](Int a,Int b){return min(a,b);}; SegmentTree seg(g,INF); seg.init(n); for(Int uku=0;uku<2;uku++){ vector sm(n+1,0); for(Int i=0;iInt{ if(i(n,dist); auto latte=[&](Int k){return uku?n-k:k;}; for(Int i=0;i ps; for(Int i=0;i ud(0,m-1); vector sm(n+1,0); for(Int i=0;iInt{ if(ir) swap(l,r); seg.update(ps[l],ps[r]+1,dist(ps[r],ps[l])); } } } for(Int i=0;i