# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
945850 | hihihihaw | Knapsack (NOI18_knapsack) | C++17 | 202 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.
#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define pii pair<int,int>
#define sz(v) (int)v.size()
#define fi first
#define se second
#define INF 1223372036854775807
#define MOD 1000000007
#define cint(x) int x;cin>>x;
#define cinarr(a,n) int a[n];for (int i=0;i<n;i++) cin>>a[i];
#define coutarr(a) for (auto d:a) cout<<d<<" "; cout<<endl;
#define coutarrD(a) for (auto d:a) cout<<d.fi<<","<<d.se<<" "; cout<<endl;
#define AYBERK_SARICA ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define endl '\n'
#define ld long double
#define mid (start+end)/2
int t=1;
int testCase=1;
void solve(){
int s,n;
cin>>s>>n;
int v[n],w[n],k[n];
for (int i=0;i<n;i++) cin>>v[i]>>w[i]>>k[i];
vector<pii> z[s+1];
for (int i=0;i<n;i++) z[w[i]].pb({v[i],k[i]});
for (int i=0;i<=s;i++){
sort(z[i].begin(),z[i].end());
reverse(z[i].begin(),z[i].end());
}
vector<int> vRiyal;
vector<int> wRiyal;
for (int i=1;i<=s;i++){
int a=0;
int p=0;
while (a<=s/i+3){
if (p>=sz(z[i]))break;
if (z[i][p].se==0) p++;
else{
vRiyal.pb(z[i][p].fi);
wRiyal.pb(i);
z[i][p].se--;
a++;
}
}
}
int nn=sz(vRiyal);
int dp[nn][s+1]={};
for (int j=1;j<=s;j++){
if (j>=wRiyal[0]) dp[0][j]=vRiyal[0];
}
for (int i=1;i<nn;i++){
for (int j=1;j<=s;j++){
if (j>=wRiyal[i]) dp[i][j]=max(dp[i-1][j-wRiyal[i]]+vRiyal[i],dp[i-1][j]);
else dp[i][j]=dp[i-1][j];
}
}
cout<<dp[nn-1][s];
}
int32_t main(){
//AYBERK_SARICA;
if (0){
freopen("in.txt", "r", stdin);
//freopen("wormsort.out", "w", stdout);
}
if (t==1) solve();
else{
cin>>t;
while (t--) solve();
}
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... |