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>
#define ll long long
#define file "knapsack"
using namespace std;
const int maxN = 1e5 + 5;
int t, n, s;
int v[maxN], w[maxN], k[maxN];
ll dp[2005];
vector< pair<int, int> > obj[maxN];
void solve(){
for (int i = 1; i <= s; ++i){
if (obj[i].size() == 0)
continue;
sort(obj[i].begin(), obj[i].end());
int id = 0;
while (id < obj[i].size()){
for (int f = s; f >= i; --f){
dp[f] = max(dp[f], dp[f - i] + obj[i][id].first);
}
--obj[i][id].second;
if (obj[i][id].second == 0)
++id;
}
}
cout << dp[s];
}
signed main(){
// freopen(file".inp", "r", stdin);
// freopen(file".out", "w", stdout);
ios_base::sync_with_stdio(false); cin.tie(0);
t = 1;
while (t){
cin >> s >> n;
for (int i = 1; i <= n; ++i){
cin >> v[i] >> w[i] >> k[i];
obj[w[i]].push_back({v[i], min(k[i], s / w[i])});
}
solve();
--t;
}
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'void solve()':
knapsack.cpp:19:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | while (id < obj[i].size()){
| ~~~^~~~~~~~~~~~~~~
# | 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... |