import java.util.*; public class candy{ public static void main(String... args){ Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int m = scan.nextInt(); System.out.println(docandy(n,m,scan)); } public static int docandy(int n,int m,Scanner scan){ int[] candyBox = new int[n]; for(int i = 0; i < n; i++){ candyBox[i] = scan.nextInt(); } Arrays.sort(candyBox); int count=0; int now=0; while(count==m){ if(candyBox[now]!=0){ count++; candyBox[now]--; } if(candyBox[now]==0)now++; } return count; } }