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 <iostream>
#include <vector>
#include <cmath>
typedef long long ll;
const int MAX_N = 1000*100 + 10;
const int MAX_S = 2*1000 + 10;
int Vs[MAX_N];
int Ws[MAX_N];
int Ks[MAX_N];
void build_e(std::vector<int> &WWs, std::vector<int> &VVs, int num, int w, int v) {
if (num <= 0)
return;
int c = std::log2(num + 1);
int t = 1;
for (int j = 0; j < c; j++) {
WWs.push_back(t * w);
VVs.push_back(t * v);
t *= 2;
}
build_e(WWs, VVs, num - (t - 1), w, v);
}
std::pair<std::vector<int>, std::vector<int> > build(int n) {
std::vector<int> WWs, VVs;
for (int i = 0; i < n; ++i)
build_e(WWs, VVs, Ks[i], Ws[i], Vs[i]);
return {WWs, VVs};
}
int solve(const std::vector<int> &WWs, const std::vector<int> &VVs, int s) {
int dp[s + 1] = {};
for (int j = 1; j <= s; ++j) {
if (j >= WWs[0])
dp[j] = VVs[0];
}
for (int i = 1; i < WWs.size(); ++i) {
int dp2[s + 1] = {};
for (int j = 1; j <= s; ++j) {
dp2[j] = std::max(dp2[j], dp[j]);
if (WWs[i] <= j)
dp2[j] = std::max(dp2[j], dp[j - WWs[i]] + VVs[i]);
}
for (int j = 1; j <= s; ++j)
dp[j] = dp2[j];
}
return dp[s];
}
int main() {
int s, n; std::cin >> s >> n;
for (int i = 0; i < n; ++i) {
std::cin >> Vs[i] >> Ws[i] >> Ks[i];
Ks[i] = std::min(Ks[i], s);
}
auto vecs = build(n);
std::cout << solve(vecs.first, vecs.second, s) << std::endl;
}
Compilation message (stderr)
knapsack.cpp: In function 'int solve(const std::vector<int>&, const std::vector<int>&, int)':
knapsack.cpp:39:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for (int i = 1; i < WWs.size(); ++i) {
| ~~^~~~~~~~~~~~
# | 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... |