提交时间:2023-12-09 09:47:52

运行 ID: 114810

#include<iostream> using namespace std; const int N = 40, M = 1e4 + 10; int w[N], n, m, f[M]; int main(){ cin >> m >> n; for(int i = 1; i <= m; i++){ cin >> w[i]; } for(int i = 1; i < M; i++) f[i] = 1e9; for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ if(i - w[j] >= 0) f[i] = min(f[i], f[i-w[j]] + 1); } } if(f[n] == (int)1e9){ cout << -1; return 0; } cout << f[n]; return 0; }