// eddy1021 #pragma GCC optimize("O3") #include using namespace std; typedef double D; typedef long double LD; typedef long long LL; typedef pair PII; typedef pair PLL; #define mod9 1000000009LL #define mod7 1000000007LL #define INF 1023456789LL #define INF16 10000000000000000LL #define eps 1e-9 #define SZ(x) (int)(x).size() #define ALL(x) (x).begin(), (x).end() #define IOS ios_base::sync_with_stdio(0); cin.tie(0) #ifndef ONLINE_JUDGE #define debug(...) printf(__VA_ARGS__) #else #define debug(...) #endif inline LL getint(){ LL _x=0,_tmp=1; char _tc=getchar(); while( (_tc<'0'||_tc>'9')&&_tc!='-' ) _tc=getchar(); if( _tc == '-' ) _tc=getchar() , _tmp = -1; while(_tc>='0'&&_tc<='9') _x*=10,_x+=(_tc-'0'),_tc=getchar(); return _x*_tmp; } inline LL add( LL _x , LL _y , LL _mod = mod7 ){ _x += _y; return _x >= _mod ? _x - _mod : _x; } inline LL sub( LL _x , LL _y , LL _mod = mod7 ){ _x -= _y; return _x < 0 ? _x + _mod : _x; } inline LL mul( LL _x , LL _y , LL _mod = mod7 ){ _x *= _y; return _x >= _mod ? _x % _mod : _x; } LL mypow( LL _a , LL _x , LL _mod ){ if( _x == 0 ) return 1LL; LL _ret = mypow( mul( _a , _a , _mod ) , _x >> 1 , _mod ); if( _x & 1 ) _ret = mul( _ret , _a , _mod ); return _ret; } LL mymul( LL _a , LL _x , LL _mod ){ if( _x == 0 ) return 0LL; LL _ret = mymul( add( _a , _a , _mod ) , _x >> 1 , _mod ); if( _x & 1 ) _ret = add( _ret , _a , _mod ); return _ret; } inline bool equal( D _x , D _y ){ return _x > _y - eps && _x < _y + eps; } void sleep( double sec = 1021 ){ clock_t s = clock(); while( clock() - s < CLOCKS_PER_SEC * sec ); } #define Bye exit(0) int __ = 1 , _cs; /*********default*********/ #define N 50505 #define lgN 16 inline int gcd( int x , int y ){ x = abs(x); y = abs(y); if( !x or !y ) return x + y; return __gcd( x , y ); } #define L(X) (X<<1) #define R(X) (1+(X<<1)) #define mid ((l+r)>>1) LL st[ N << 2 ]; int who[ N << 2 ]; void modify( int no , int l , int r , int p , LL v ){ if( l == r ){ st[ no ] = v; who[ no ] = l; return; } if( p <= mid ) modify( L( no ) , l , mid , p , v ); else modify( R( no ) , mid + 1 , r , p , v ); if( st[ L( no ) ] > st[ R( no ) ] ){ st[ no ] = st[ L( no ) ]; who[ no ] = who[ L( no ) ]; }else{ st[ no ] = st[ R( no ) ]; who[ no ] = who[ R( no ) ]; } } pair query( int no , int l , int r , int ql , int qr ){ if( l == ql and r == qr ) return { st[ no ] , who[ no ] }; if( qr <= mid ) return query( L( no ) , l , mid , ql , qr ); if( mid < ql ) return query( R( no ) , mid + 1 , r , ql , qr ); return max( query( L( no ) , l , mid , ql , mid ) , query( R( no ) , mid + 1 , r , mid + 1 , qr ) ); } int g[ lgN ][ N ]; int mx[ lgN ][ N ]; void build(){ } int n; LL s[ N ]; inline int GCD( int l , int r ){ int bt = __lg( r - l + 1 ); return gcd( g[ bt ][ l ] , g[ bt ][ r - (1 << bt) + 1 ] ); } inline int MAX( int l , int r ){ int bt = __lg( r - l + 1 ); return max( mx[ bt ][ l ] , mx[ bt ][ r - (1 << bt) + 1 ] ); } LL abss[ N ]; void init(){ n = getint(); for( int i = 1 ; i <= n ; i ++ ){ mx[ 0 ][ i ] = g[ 0 ][ i ] = getint(); s[ i ] = s[ i - 1 ] + g[ 0 ][ i ]; abss[ i ] = abss[ i - 1 ] + abs( g[ 0 ][ i ] ); } for( int i = 1 ; i <= n ; i ++ ) modify( 1 , 1 , n , i , s[ i ] ); for( int j = 1 ; j < lgN ; j ++ ) for( int i = 1 ; i + (1 << j) - 1 <= n ; i ++ ){ g[ j ][ i ] = gcd( g[ j - 1 ][ i ] , g[ j - 1 ][ i + (1 << (j - 1)) ]); mx[ j ][ i ] = max( mx[ j - 1 ][ i ] , mx[ j - 1 ][ i + (1 << (j - 1)) ]); } } #define K 3 int cand[ K ]; LL ori[ K ]; void solve(){ LL ans = 0; for( int i = 1 ; i <= n ; i ++ ){ int lft = i; while( lft <= n ){ int curg = GCD( i , lft ); if( curg * ( abss[ n ] - abss[ i - 1 ] ) <= ans ) break; int bl = i + 1 , br = n , til = lft; while( bl <= br ){ int bmid = (bl + br) >> 1; if( GCD( i , bmid ) == curg ) til = bmid , bl = bmid + 1; else br = bmid - 1; } int ct = min( K , til - lft + 1 ); for( int j = 0 ; j < ct ; j ++ ){ pair r = query( 1 , 1 , n , lft , til ); cand[ j ] = r.second; ori[ j ] = r.first; modify( 1 , 1 , n , cand[ j ] , -INF16 ); ans = max( ans , curg * ( (LL)( s[ cand[ j ] ] - s[ i - 1 ] ) - MAX( i , cand[ j ] ) ) ); } for( int j = 0 ; j < ct ; j ++ ) modify( 1 , 1 , n , cand[ j ] , ori[ j ] ); lft = til + 1; } } printf( "%lld\n" , ans ); } int main(){ build(); //__ = getint(); while( __ -- ){ init(); solve(); } }