Create two sets
for ( let k = 0 ; k < n ; k ++ ) { if ( k >= i && k <= j ) { curans += a [ k ] ; pq2 . push ( a [ k ] ) ; } else pq . push ( a [ k ] ) ; } pq . sort ( ) ; pq . reverse ( ) ; pq2 . sort ( ) ; ans = Math . max ( ans , curans ) ;