I simply came up with this logic
Put a cable in a given Y0 place.
=> if you move that cable Y up (and not going past any house)
-You'll save (Y times the amount of houses above it) worth of cable, but:
-You'll lose (Y times the amount of houses below it) worth of cable
So the optimal Y place is where there's the same amount of houses above and below.
The median will works, but it's not always the only best Y. In fact, it is, when there's an odd number of house. But when working with an even amount of houses, picking a value anywhere between the two middle houses (inclusive) will always lead to the correct distance being computed. In fact, it's better to not pick the median in that case and stick with one of the two best houses, so you'll avoid decimal calculations
Example: you have houses with coordinates -3, -1, 1, 3. The correct Y is anywhere between -1 and 1.
sum of distances with -1: 2 + 0 + 2 + 4 = 8
sum of distances with 0 : 3 + 1 + 1 + 3 = 8
sum of distances with 1 : 4 + 2 + 0 + 2 = 8