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 int long long
const int inf=1e12;
signed main(){
int S,n; cin>>S>>n;
vector<multiset<int>> items(S+1);
for (int i=0; i<n; i++){
int v,w,k; cin>>v>>w>>k;
k=min(k, S/w+1);
while(k--) items[w].insert(v);
while((int)(items[w].size()) > S/w+1) {
items[w].erase(items[w].begin());
}
}
vector<int> dp(S+1,-inf); //current maximum value of a subset with total weight w
dp[0]=0;
for (int w=0; w<=S; w++){
for (auto v: items[w]){
for (int i=S; i>=w; i--){
dp[i]=max(dp[i], dp[i-w]+v);
}
}
}
int ans=0;
for (auto i: dp) ans=max(ans, i);
cout<<ans<<'\n';
}
# | 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... |