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;
struct item
{
int v, w, k;
};
int n, s;
item a[100002];
vector<item> b;
long long dp[40000][2002];
bool cmp(item &a, item &b)
{
if(a.w == b.w) return a.v > b.v;
return a.w < b.w;
}
int main()
{
//freopen("test.in","r",stdin);
//freopen("test.out","w",stdout);
cin>>s>>n;
for(int i = 0; i < n; i++){
cin>>a[i].v>>a[i].w>>a[i].k;
if(a[i].k > s / a[i].w) a[i].k = s / a[i].w;
}
sort(a, a + n, cmp);
//for(int i = 0; i < n; i++) cout<<a[i].v<<" "<<a[i].w<<" "<<a[i].k<<endl;
int cnt = 0;
b.push_back({0, 0, 0});
for(int i = 0; i < n; i++){
//if(a[i].w == wei){
if(s / a[i].w >= cnt + a[i].k){
//lis[a[i].w][a[i].v] = a[i].k;
int t = a[i].k;
while(t-- > 0) b.push_back({a[i].v, a[i].w, 1});
cnt += a[i].k;
}
else{
int t = s / a[i].w - cnt;
while(t-- > 0) b.push_back({a[i].v, a[i].w, 1});
//lis[a[i].w][a[i].v] = s / a[i].w - cnt;
cnt = 0;
}
//}
if(a[i + 1].w != a[i].w) cnt = 0;
}
//print();
//for(int i = 0; i < b.size(); i++) cout<<b[i].w<<" "<<b[i].v<<endl;
for(int i = 1; i < b.size(); i++){
long long v = b[i].v, w = b[i].w;
for(int j = 0; j <= s; j++){
dp[i][j] = dp[i - 1][j];
if(j - w >= 0) dp[i][j] = max(dp[i][j], dp[i - 1][j - w] + v);
}
}
cout<<dp[b.size() - 1][s];
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:58:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<item>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for(int i = 1; i < b.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... |