Create two sets
for k in range ( n ) : NEW_LINE if ( k >= i and k <= j ) : NEW_LINE INDENT curans += a [ k ] NEW_LINE pq2 . append ( a [ k ] ) NEW_LINE DEDENT else : NEW_LINE INDENT pq . append ( a [ k ] ) NEW_LINE DEDENT pq . sort ( ) NEW_LINE pq . reverse ( ) NEW_LINE pq2 . sort ( ) NEW_LINE ans = max ( ans , curans ) NEW_LINE