提交时间:2023-12-09 10:15:07
运行 ID: 114834
#include<bits/stdc++.h> #define int long long using namespace std; int a[1000005],dp[1000005] = {1},c[1000005]; signed main(){ int n,w; cin>>n>>w; for (int i = 1;i <= n;i++){ cin>>a[i]; } memset(c,0x7f7f7f7f,sizeof(c)); c[0] = 0; for (int i = 1;i <= n;i++){ for (int j = a[i];j <= w;j++){ dp[j] += dp[j - a[i]]; c[j] = min(c[j],c[j - a[i]] + 1); } } if (c[w] >= 0x7f7f7f7f){ c[w] = -1; } cout<<c[w]; return 0; }