# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
881514 | andro | Knapsack (NOI18_knapsack) | C++14 | 200 ms | 262144 KiB |
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 int long long
using namespace std;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n,s;
cin>>s>>n;
int v[n+1];
int w[n+1];
int k[n+1];
for(int i=1;i<=n;i++)cin>>v[i]>>w[i]>>k[i];
vector<pair<int,int>>A[s+1];
for(int i=1;i<=n;i++)A[w[i]].push_back({v[i],k[i]});
vector<pair<int,int>>u;
for(int i=1;i<=s;i++){
int mx=s/i;
sort(A[i].begin(),A[i].end());
reverse(A[i].begin(),A[i].end());
for(auto it:A[i]){
if(mx<0)break;
for(int j=1;j<=it.second;j++){
if(mx<0)break;
u.push_back({it.first,i});
mx--;
}
}
}
int dp[u.size()][s+1];
for(int i=0;i<u.size();i++)for(int j=0;j<=s;j++)dp[i][j]=0;
dp[0][u[0].second]=u[0].first;
for(int i=1;i<u.size();i++){
for(int j=0;j<=s;j++)dp[i][j]=dp[i-1][j];
for(int j=u[i].second;j<=s;j++)dp[i][j]=max(dp[i][j],dp[i-1][j-u[i].second]+u[i].first);
}
int ans=0;
for(int i=1;i<=s;i++)ans=max(ans,dp[u.size()-1][i]);
cout<<ans;
}
/*
dobijas v[i]
*/
Compilation message (stderr)
# | 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... |