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;
typedef long long ll;
struct Item{
int v, w, k;
bool operator<(const Item& o) const{
return o.v < v;
};
};
const int MAXN = 1e5 + 5, MAXS = 2000 + 5;
int maxW, n;
map<int, vector<Item>> arr;
ll dp[MAXS][MAXS];
int main(){
cin.tie(0)->sync_with_stdio(0);
//freopen("file.in", "r", stdin);
cin >> maxW >> n;
for (int i=0; i<n; i++){
int v, w, k;
cin >> v >> w >> k;
if (arr.find(w) == arr.end()){
arr.insert({w, vector<Item>()});
}
arr[w].push_back({v, w, k});
}
int weightGroup = 1;
for (auto currW : arr){
vector<Item> curr = currW.second;
sort(curr.begin(), curr.end());
int id = 0;
for (int i=0; i<=maxW; i++){
dp[weightGroup][i] = dp[weightGroup-1][i];
int sum = 0;
// Try to use as many of this current weightGroup as possible
int copies = 0;
int ind = 0;
int totCopies = 1;
while (ind < curr.size()){
if (totCopies * currW.first > i){
break;
}
if (copies >= curr[ind].k){
ind++;
copies = 0;
}
if (ind >= curr.size()){
break;
}
sum += curr[ind].v;
dp[weightGroup][i] = max(dp[weightGroup][i], dp[weightGroup-1][i - totCopies * currW.first] + sum);
copies++;
totCopies++;
}
}
weightGroup++;
}
/*for (int i=1; i<weightGroup; i++){
for (int j=0; j<=maxW; j++){
cout << dp[i][j] << " ";
}
cout << "\n";
}*/
ll ans = 0;
for (int i=0; i<=maxW; i++){
ans = max(ans, dp[weightGroup-1][i]);
}
cout << ans << '\n';
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:41:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Item>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | while (ind < curr.size()){
| ~~~~^~~~~~~~~~~~~
knapsack.cpp:49:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Item>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | if (ind >= curr.size()){
| ~~~~^~~~~~~~~~~~~~
knapsack.cpp:33:13: warning: unused variable 'id' [-Wunused-variable]
33 | int id = 0;
| ^~
# | 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... |