Solution
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
class Main {
static int solution(int k, List<Integer> list) {
// 오름차순으로 정렬된 배열을 내림차순으로 정렬
list.sort(Collections.reverseOrder());
// 반복문을 돌면서 k보다 크면 지나가고, k보다 작거나 같으면 차감
int cnt = 0;
for (Integer integer : list) {
if (integer <= k) {
while (true) {
if (k - integer >= 0) {
k -= integer;
cnt++;
} else {
break;
}
}
}
}
return cnt;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
list.add(Integer.parseInt(br.readLine()));
}
System.out.print(solution(k, list));
br.close();
}
}
Leave a comment