Create two sets
for ( int k = 0 ; k < n ; k ++ ) { if ( k >= i && k <= j ) { curans += a [ k ] ; pq2 . Add ( a [ k ] ) ; } else pq . Add ( a [ k ] ) ; } pq . Sort ( ) ; pq . Reverse ( ) ; pq2 . Sort ( ) ; ans = Math . Max ( ans , curans ) ;