The first ist quite east, just find the median, and then calculate cost.
For the second exercise we need triangle numbers. And the median won't work anymore.