import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int B = sc.nextInt(); int N = sc.nextInt(); int[] C = new int[N]; for (int i = 0; i < C.length; i++) { C[i] = sc.nextInt(); } sc.close(); long min = Long.MAX_VALUE; for (int i = 0; i < C.length; i++) { int V = C[i]; int stock = B; long ope = 0; for (int j = 0; j < C.length; j++) { // 取り上げ stock += Math.max(0, C[j] - V); ope += Math.max(0, C[j] - V); } for (int j = 0; j < C.length; j++) { // 配る stock -= Math.max(0, V - C[j]); ope += Math.max(0, V - C[j]); } if (stock >= 0) { min = Math.min(min, ope); } } System.out.println(min); } }