# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
488600 | A_D | Knapsack (NOI18_knapsack) | C++14 | 125 ms | 145556 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 ii pair<int,int>
#define F first
#define S second
using namespace std;
const int N=2e3+100;
const int NN=2e5+100;
const int M=2e4+100;
vector<int> vec;
vector<int> w;
int szz[N];
int sz;
pair<ii,int> a[NN];
int dp[M][N];
void solve()
{
vec.push_back(0);
w.push_back(0);
int s,n;
cin>>s>>n;
for(int i=1;i<=n;i++){
scanf("%d",&a[i].F.F);
scanf("%d",&a[i].F.S);
scanf("%d",&a[i].S);
}
sort(a+1,a+n+1);
reverse(a+1,a+n+1);
for(int i=1;i<=n;i++){
while(a[i].S&&s>a[i].F.S*szz[a[i].F.S]){
a[i].S--;
szz[a[i].F.S]++;
vec.push_back(a[i].F.F);
w.push_back(a[i].F.S);
sz++;
if(sz>=M){
return;
}
}
}
for(int i=1;i<=sz;i++){
for(int j=1;j<w[i];j++){
dp[i][j]=dp[i-1][j];
}
for(int j=w[i];j<=s;j++){
dp[i][j]=max(dp[i-1][j],dp[i-1][j-w[i]]+vec[i]);
}
}
cout<<dp[sz][s]<<endl;
}
main()
{
//freopen("face.in","r",stdin);
//freopen("face.out","w",stdout);
int t=1;
// cin>>t;
while(t--){
solve();
}
}
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... |