import java.util.Arrays; import java.util.HashSet; import java.util.LinkedList; import java.util.Random; import java.util.Scanner; import java.util.Set; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); final long B = sc.nextLong(); final int N = sc.nextInt(); long[] arr = new long[N]; for(int i = 0; i < N; i++){ final long c = sc.nextLong(); arr[i] = c; } Arrays.sort(arr); long score = Long.MAX_VALUE; for(int pos = 0; pos < N; pos++){ long benefit = B, current_score = 0; for(int i = 0; i < N; i++){ benefit += (arr[i] - arr[pos]); current_score += Math.abs(arr[i] - arr[pos]); } if(benefit < 0){ continue; } score = Math.min(score, current_score); } { final long max = (Arrays.stream(arr).sum() + B) / N; long current_score = 0; for(int i = 0; i < N; i++){ current_score += Math.abs(arr[i] - max); } score = Math.min(score , current_score); } System.out.println(score); } }