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(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int S,n; cin>>S>>n;
vector<vector<int>> a(n);
for (int i=0; i<n; i++){
int v,w,k; cin>>v>>w>>k;
a[i]={v,w,k};
}
sort(a.begin(),a.end());
vector<vector<int>> items(S+1);
for (int i=0; i<n; i++){
int v=a[i][0], w=a[i][1], k=a[i][2];
while (k-- and ((int)(items[w].size())<S/w+1)) items[w].push_back(v);
}
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... |