For upper median traverse the set pairs from right
sums = 0 NEW_LINE for index in range ( len ( pairs ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT element = pairs [ index ] [ 0 ] NEW_LINE weight = pairs [ index ] [ 1 ] NEW_LINE DEDENT