If found an index smaller than ind and sum not exceeding X then remove index of smallest value from ans and then add index i to ans
while ( ( sum - A [ ind ] + A [ i ] ) <= X and temp != 0 ) : NEW_LINE INDENT del ( ans [ 0 ] ) ; NEW_LINE ans . append ( i ) ; NEW_LINE temp -= 1 ; NEW_LINE sum += ( A [ i ] - A [ ind ] ) ; NEW_LINE DEDENT ans . sort ( ) ; NEW_LINE return ans ; NEW_LINE