Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
575 B
6
Indexable
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits> 
#define maxn 1009
using namespace std;
int A[maxn];
int dp[10009];
int main() {
    int n, M;
    cin >> n >> M;

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

    for(int i = 0; i < 10000;i++) dp[i] = INT_MAX;
    dp[0] = 0;

    for (int i = 0; i < n; i++) {
        for (int j = M; j >= A[i]; j--) {
            if (dp[j - A[i]] != INT_MAX) {
                dp[j] = min(dp[j], dp[j - A[i]] + 1);
            }
        }
    }

   
    cout << dp[M] << endl; 


    return 0;
}
Leave a Comment