#define P 1000000007 int n , m , k; int A[MAXN] , B[MAXN]; vi a[6]; vector<ll> s[6];
#define abs( x ) ( (x) > 0 ? (x) : -(x) )
ll check( int x , int y ){ ll ret = 0; B[0] = x , B[1] = y , B[2] = y - x , B[3] = -x , B[4] = -y , B[5] = x - y; rep( i , 0 , 5 ) { if( s[i].empty() ) continue; int ps = upper_bound( all( a[i] ) , B[i] ) - a[i].begin() - 1; ret += ( ps + 1 - ( a[i].size() - ps - 1 ) ) * B[i] + s[i].back(); if( ps != -1 ) ret -= s[i][ps] + s[i][ps]; } return ret; }
ll check( int x ){ int l = -1e9 , r = 1e9 , mid; ll t; while( l <= r ) { mid = l + r >> 1; ll a = check( x , mid ) , b = check( x , mid + 1 ); t = min( a , b ); if( a < b ) r = mid - 1; else l = mid + 1; } return t; }
voidsolve(){ cin >> n; rep( i , 0 , n - 1 ) scanf("%d",A + i) , a[i % 6].pb( A[i] ); rep( i , 0 , 5 ) { sort( all( a[i] ) ); for( int x : a[i] ) { if( s[i].empty() ) s[i].pb( 0 ); else s[i].pb( s[i].back() ); s[i].back() += x; } } int l = -1e9 , r = 1e9 , mid; ll ans = 0x3f3f3f3f; while( l <= r ) { mid = l + r >> 1; ll a = check( mid ) , b = check( mid + 1 ); ans = min( a , b ); if( a < b ) r = mid - 1; else l = mid + 1; } cout << ans << endl; }
structpoi { ll x , y; } p[MAXN] ; int top = 0; int ans[MAXN]; doublegetk( poi& a , poi& b ){ return ( 1.0 * a.y - b.y ) / ( a.x - b.x ); }
intPow( int x , int a ){ int ret = 1; while( a ) { if( a & 1 ) ret = ret * 1ll * x % P; x = x * 1ll * x % P , a >>= 1; } return ret; } ints( int x ){ return x * 1ll * ( x + 1 ) / 2 % P; }
voidadd( int x , int y ){ poi cur = (poi) {x , y}; y %= P; while( top >= 2 && getk( cur , p[top] ) > getk( p[top] , p[top - 1] ) ) -- top; int k = ( y - p[top].y % P + P ) * 1ll * Pow( x - p[top].x , P - 2 ) % P , b = ( y + P - x * 1ll * k % P ) % P; ans[x] = ( ans[p[top].x] + k * 1ll * ( s( x ) - s( p[top].x ) + P ) % P + b * 1ll * ( x - p[top].x ) ) % P; p[++ top] = cur; }
voidsolve(){ cin >> n; rep( i , 1 , n ) { scanf("%lld",A + i); add( i , A[i] ); } rep( i , 1 , n ) printf("%lld ",ans[i] * 1ll * Pow( i , P - 2 ) % P); }