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 <cmath>
#include <iostream>
#include <set>
#include <climits>
#include <algorithm>
#include <cassert>
#include <vector>
#include <iomanip>
#include <type_traits>
#include <string>
#include <queue>
#include <map>
using namespace std;
#define ll long long
struct item {
long long weight;
long long worth;
long long cnt;
};
int main() {
long long S, N;
cin >> S >> N;
vector<item> items;
for (int i = 0; i < N; i++) {
long long wei, wor;
cin >> wor >> wei;
long long cnt;
cin >> cnt;
//for (int j = 0; j < min(cnt, S/wei); j++) {
items.push_back({wei, wor, min(cnt, S/wei)});
//}
}
//reverse(items.begin(), items.end());
long long dp[items.size() + 1][S + 1]; //taking i items, what is the best worth that we can get
for (int i = 0; i <= items.size(); i++) {
for (int j = 0; j <= S; j++) {
dp[i][j] = 0;
}
}
long long myMax = 0;
for (int i = 1; i <= items.size(); i++) {
for (int j = 0; j <= S; j++) {
dp[i][j] = dp[i - 1][j];
int cntr = 0;
while (cntr * items[i - 1].weight <= j && cntr <= items[i - 1].cnt) {
dp[i][j] = max(dp[i][j], dp[i - 1][j - cntr * items[i - 1].weight] + cntr * items[i - 1].worth);
cntr++;
}
myMax = max(myMax, dp[i][j]);
}
}
cout << myMax << endl;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:35:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<item>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for (int i = 0; i <= items.size(); i++) {
| ~~^~~~~~~~~~~~~~~
knapsack.cpp:41:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<item>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for (int i = 1; i <= items.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... |