Mailbox assignment distance

Write codeHard40 min

Your task

Given the positions of houses on a street (integers) and a number k of mailboxes to install (each mailbox sits at an integer position), assign each house to its nearest mailbox. Return the minimum possible total distance between all houses and their nearest mailbox. There are at most 100 houses and 1 <= k <= number of houses; positions are 1..10000.

Examples

min_mailbox_distance(houses: list[int], k: int) -> int
Example 1
Inputmin_mailbox_distance([1, 4, 8, 10, 20], 3)
Output5