최대값부터 차례대로 들어갈 수 있는만큼 넣도록 구현하기
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in= new Scanner(System.in);
int N,K,sum=0, answer=0;
int type[];
N = in.nextInt();
K = in.nextInt();
type = new int[N];
for(int i=0;i<N;i++)
{
type[i]=in.nextInt();
}
while(sum!=K) //K와 같아질 때 까지
{
while(sum<K)
{
sum+=type[N-1]; //sum에 지폐의 가치중 가장 큰 순서대로 한번씩 넣어본다.
answer++; // 들어갔다면 개수 추가
if(sum>K) // 값이 너무 크다면
{
sum-=type[N-1]; //원래상태로 복구
N--; //지폐 가치 한단계 낮게
answer--; //개수 복구
}
}
}
System.out.println(answer);
}
}