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;
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define AC ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define fs first
#define sc second
#define pb push_back
const int inf=INT_MAX;
vector<pii> a[2007];
vector<pii> item;
int dp[20007][2007];
int C, n;
int knapsack(int i, int C){
if(C < 0)return -inf;
if(i==item.size())return 0;
if(dp[i][C] != -1)return dp[i][C];
int w = item[i].sc, v= item[i].fs;
return dp[i][C]= max(knapsack(i+1, C), knapsack(i+1, C-w)+v);
}
int main() {
AC
cin >> C >> n;
for(int i=0; i<n; i++){
int v, w, k;
cin >> v >> w >> k;
a[w].pb({v,k});
}
for(int w=1; w<=2000; w++){
sort(a[w].begin(), a[w].end());
int used = ceil(C/w);
while(used and !a[w].empty()){
auto &[v, k] = a[w].back();
item.pb({a[w].back().fs, w});
used--, k--;
if(k==0)a[w].pop_back();
}
}
memset(dp, -1, sizeof(dp));
cout << knapsack(0, C);
}
Compilation message (stderr)
knapsack.cpp: In function 'int knapsack(int, int)':
knapsack.cpp:20:6: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | if(i==item.size())return 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... |