过关道具
2023-08-19 17:18:36
发布于:河北
2阅读
0回复
0点赞
题解:
#include<bits/stdc++.h>
using namespace std;
int dp[50010];
int a[510];
int main()
{
int n, m;
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
}
cin >> m;
for(int i = 1; i <= m; i++)
{
int minn = 500000;
for(int j = 1; j <= n; j++)
{
if(i >= a[j] && dp[i-a[j]] != -1) minn = min(minn, dp[i-a[j]]+1);
}
if(minn == 500000) dp[i] = -1;
else dp[i] = minn;
}
cout << dp[m] << endl;
return 0;
}
这里空空如也
有帮助,赞一个