#!/usr/bin/env perl use strict; use warnings; my $n = <>; my @ys = split / /, <>; @ys = sort { $a <=> $b } @ys; my $target; if ($n % 2) { $target = $ys[$n / 2]; } else { my $lpos = int($n / 2); $target = ($ys[$lpos] + $ys[$lpos + 1]) / 2; } my $ans = 0; for my $e (@ys) { $ans += abs($e - $target); } print "$ans\n";