This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define vvi vector<vi>
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int W, n;
cin >> W >> n;
vi vals(n), wes(n), copies(n);
for (int i = 0; i < n; i++) {
cin >> vals[i] >> wes[i] >> copies[i];
}
// 使用一维数组进行滚动优化
vi dp(W + 1, 0);
for (int i = 0; i < n; i++) {
int val = vals[i], we = wes[i], cop = copies[i];
// 使用二进制拆分法来处理副本
for (int k = 1; k <= cop; k *= 2) {
int count = min(cop, k);
dp[W] = max(dp[W], dp[W - count * we] + count * val);
for (int j = W; j >= count * we; j--) {
dp[j] = max(dp[j], dp[j - count * we] + count * val);
}
cop -= count;
}
// 处理剩余的副本
if (cop > 0) {
for (int j = W; j >= cop * we; j--) {
dp[j] = max(dp[j], dp[j - cop * we] + cop * val);
}
}
}
cout << dp[W];
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |