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];
int dp[MAX_N][MAX_S];
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) {
for (int j = 1; j <= s; ++j) {
dp[0][j] = (j / WWs[0]) * VVs[0];
}
for (int i = 1; i < WWs.size(); ++i) {
for (int j = 1; j <= s; ++j) {
dp[i][j] = std::max(dp[i][j], dp[i - 1][j]);
if (WWs[i] <= j)
dp[i][j] = std::max(dp[i][j], dp[i - 1][j - WWs[i]] + VVs[i]);
}
}
return dp[WWs.size() - 1][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:38:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | 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... |