# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
917463 | PieArmy | Knapsack (NOI18_knapsack) | C++17 | 12 ms | 664 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 fr first
#define sc second
#define pb push_back
#define endl '\n';
typedef long long ll;
const ll inf=2000000000000000005;
using namespace std;
ll pie(ll army){return (1ll<<army);}
ll fpow(ll x,ll y,ll m=0){if(y<0){cout<<"powError";return -1;}if(m)x%=m;ll res=1;while(y>0){if(y&1)res*=x;x*=x;if(m){x%=m;res%=m;}y>>=1;}return res;}
void code(){
int k,n;cin>>k>>n;
int v[n],w[n],cnt[n];
for(int i=0;i<n;i++)cin>>v[i]>>w[i]>>cnt[i];
vector<int>dp(k+1,0);
for(int i=0;i<n;i++){
vector<int>dp2(k+1,0);
vector<multiset<int>>st(k+1,multiset<int>());
for(int j=0;j<=k;j++){
st[j].insert(((k/w[i])-(j/w[i]))*v[i]+dp[j]);
dp2[j]=(*(--st[j].end()))-(((k/w[i])-(j/w[i]))*v[i]);
if(j>=w[i]*cnt[i]){
st[j].erase(st[j].find(((k/w[i])-((j-(w[i]*cnt[i]))/w[i]))*v[i]+dp[j-(w[i]*cnt[i])]));
}
if(j+w[i]<=k)swap(st[j],st[j+w[i]]);
}
swap(dp,dp2);
}
int ans=0;
for(int i=0;i<=k;i++)ans=max(ans,dp[i]);
cout<<ans;
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
int t=1;
if(!t)cin>>t;
while(t--){code();cout<<endl;}
return 0;
}
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... |