1 solutions

  • 0
    @ 2026-11-13 20:21:18

    C++ :

    #include <bits/stdc++.h>
    #define N 10005
    #define INF 0x3f3f3f3f
    using namespace std;
    
    int a[20],dp[N];
    
    int main(){
        int n,m;
        cin >> n;
        for (int i = 1;i<=n;i++) cin >> a[i];
        cin >> m;
        
        memset(dp,0x3f,sizeof dp);
        dp[0] = 0;
        for (int i = 1;i<=n;i++){
            for (int j = a[i];j<=m;j++){
                if (dp[j - a[i]] == INF) continue; //j - a[i]元暂无方案
                dp[j] = min(dp[j],dp[j - a[i]] + 1);
            }
        }
        if (dp[m] == INF) cout << -1;
        else cout << dp[m];
        return 0;
    }
    
    • 1

    Information

    ID
    107
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    10
    Tags
    # Submissions
    1
    Accepted
    1
    Uploaded By