Coin 2

 

#include <bits/stdc++.h>
#define M 10000

using namespace std;

int n,k,A[M+5],dp[M+5];

void input(){
    cin>>n>>k;
    for(int i=0;i<n;i++){
        cin>>A[i];
    }
}

void DP(){
    for(int i=1;i<=k;i++){
        dp[i]=1e9;
    }
    for(int i=0;i<n;i++){
        for(int j=A[i];j<=k;j++){
            dp[j]=min(dp[j],dp[j-A[i]]+1);
        }
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    //freopen("in.txt","r",stdin);
    input();
    DP();
    if(dp[k]==1e9){
        cout<<-1<<endl;
        return 0;
    }
    cout<<dp[k]<<endl;
    return 0;
}

Comments