Algorithm-Renewed

This commit is contained in:
e2hang
2025-09-13 22:37:17 +08:00
parent 265b8af720
commit 7b6acef275
14 changed files with 778 additions and 1 deletions

View File

@@ -0,0 +1,26 @@
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n, w;
cin >> n >> w;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
vector<int> dp(w + 1, 1e9);
dp[0] = 0;
for (int j = 0; j < n; j++) {
for (int i = a[j]; i <= w; i++) {
dp[i] = min(dp[i], dp[i - a[j]] + 1);
}
}
if (dp[w] == 1e9) cout << -1 << endl; // <20>޽<EFBFBD>
else cout << dp[w] << endl;
return 0;
}