function main(input) { let [b] = getIntegers(input); let [n] = getIntegers(input); let c = input.map(x => Number(x)).sort((a, b) => b - a); let ans = 0; if(n > 1) { if(n % 2 == 1) { const index = Math.round(n / 2) - 1; ans = solve(c[index], b, c); } else { const index1 = Math.round(n / 2) - 1; const index2 = Math.round(n / 2); ans = Math.min(solve(c[index1], b, c), solve(c[index2], b, c)); } } console.log(ans); } function solve(v, b, c) { let num = 0; for(let i=0; i v) { b += (c[i] - v); num += (c[i] - v); } else if(c[i] < v) { b -= (v - c[i]); num += (v - c[i]); } } return num; } //-- functions ------------------------------------ function getIntegers(lines) { return lines.shift().split(" ").map(function(e) { return Number(e); }); } function getStrings(lines) { return lines.shift().split(" "); } function twoDimensionalArray(h, w, value) { const list = new Array(h); for(let i=0; i 0) str += " "; str += e; }); return str; }; main(require("fs").readFileSync("/dev/stdin", "utf8").split("\n"));