using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace No198 { class Program { static void Main(string[] args) { var hand = long.Parse(Console.ReadLine()); var n = long.Parse(Console.ReadLine()); var data = new long[n]; for (int i = 0; i < n; i++) { data[i] = long.Parse(Console.ReadLine()); } var sum = hand + data.Sum(); long minValue = 0; long maxValue = sum/n; while (maxValue - minValue > 1) { var minOperation = Calculate(data, minValue); var middleValue = (minValue + maxValue) / 2; var middleOperation = Calculate(data, middleValue); var maxOperation = Calculate(data, maxValue); var currentMin = new[] {minOperation, middleOperation, maxOperation}.Min(); if (currentMin == middleOperation) { if (minOperation < maxOperation) { maxValue = middleValue; } else { minValue = middleValue; } } else { break; } } var result = Math.Min(Calculate(data, minValue), Calculate(data, maxValue)); Console.WriteLine(result); } private static long Calculate(long[] data, long lastValue) { var operations = data.Select(element => Math.Abs(element - lastValue)).Sum(); return operations; } } }