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 long solve_smart(long B, int N, long[] 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; System.out.println(max); long current_score = 0; for(int i = 0; i < N; i++){ current_score += Math.abs(arr[i] - max); } score = Math.min(score , current_score); } return score; } public static long solve_naive(long B, int N, long[] arr){ //System.out.println(Arrays.toString(arr)); final long max = Arrays.stream(arr).max().getAsLong(); long score = Long.MAX_VALUE, min_cur = -1; for(long cur = 0; cur <= max; cur++){ long benefit = B, current_score = 0; for(int i = 0; i < N; i++){ benefit += (arr[i] - cur); current_score += Math.abs(arr[i] - cur); } if(benefit < 0){ continue; } if(score > current_score){ score = current_score; min_cur = cur; } score = Math.min(score, current_score); } System.out.println(B + " " + N + " " + Arrays.toString(arr) + " " + min_cur + " " + Arrays.stream(arr).average() + " " + score); return score; } public static void check(){ Random rnd = new Random(); while(true){ final long B = rnd.nextInt(1000); final int N = rnd.nextInt(9) + 1; long[] arr = new long[N]; for(int i = 0; i < N; i++){ arr[i] = rnd.nextInt(100); } final long smart = solve_smart(B, N, arr); final long naive = solve_naive(B, N, arr); if(smart != naive){ System.out.println(">>>> naive = " + naive + ", smart = " + smart); // } } } public static void main(String[] args) { //check(); 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); } }