#!/usr/bin/env python3 # In a "all fuel is equal" situation, the optimal solution will # be the median, since it's effectively the closest to the "middle" # one can get. I forget the proof, but it works. Something like # # T = |x1 - m| + ... + |xi - m| # # If m is the median, there are i/2 points on each side. If m # was NOT the median, it might get "closer" to a subset of points # but the "larger" split will be >i/2 points, and it is getting # "farther" from those points. The # of points farther > # closer, # so it's a less optimal positioning. import sys crabs = [] for l in sys.stdin: crabs = [int(x) for x in l.split(",")] s = sorted(crabs) m = s[int(len(crabs) / 2)] print(sum([abs(x - m) for x in crabs]))