package main import ( "fmt" "math" ) func main() { solve() } var B, N, ans int var C [10]int func solve() { fmt.Scan(&B, &N) for i := 0; i < N; i++ { fmt.Scan(&C[i]) } ans = math.MaxInt64 lo := 0 hi := math.MaxInt64 / 3 for hi-lo > 10 { loMid := (lo + lo + hi) / 3 hiMid := (lo + hi + hi) / 3 if calc(loMid) <= calc(hiMid) { hi = hiMid } else { lo = loMid } } for i := max(0, lo-100); i < lo+100; i++ { calc(i) } fmt.Println(ans) } func calc(a int) int { rest := B temp := 0 for i := 0; i < N; i++ { temp += abs(a - C[i]) rest += C[i] - a } if rest < 0 { return math.MaxInt64 / 3 } ans = min(temp, ans) return temp } func abs(a int) int { if a < 0 { a *= -1 } return a } func min(a, b int) int { if a > b { a = b } return a } func max(a, b int) int { if a < b { a = b } return a }