| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 557337 | ShivanshJ | Knapsack (NOI18_knapsack) | C++17 | 1085 ms | 56268 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.
/*Segment tree really makes clean code look ugly :P*/
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN=2e5+5;
const int INF=1e18;
const int MOD=1e9+7;
const int MAXL=15;
const int MAXW=2e3+5;
int dp[2][MAXW];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//cout<<setprecision(6)<<fixed;
//freopen("feast.in","r",stdin);
//freopen("feast.out","w",stdout);
int s,n;
cin>>s>>n;
vector<vector<int>>sack;
for(int i=0;i<n;i++) {
int v,w,k;
cin>>v>>w>>k;
k=min(k,s/w);
int j=MAXL-1;
for(j=MAXL-1;((1LL<<j)&k)==0;j--);
int x=k-(1LL<<j)+1;
sack.push_back({v*x,w*x});
j--;
while(j>=0) {
sack.push_back({v*(1LL<<j),w*(1LL<<j)});
j--;
}
}
int curr=1;
for(int i=0;i<sack.size();i++) {
int nxt=(curr^1);
for(int j=0;j<MAXW;j++) {
dp[curr][j]=0;
if(sack[i][1]<=j) {
dp[curr][j]=max(dp[curr][j],dp[nxt][j-sack[i][1]]+sack[i][0]);
}
dp[curr][j]=max(dp[curr][j],dp[nxt][j]);
}
curr=nxt;
}
curr^=1;
int ans=0;
for(int j=0;j<=s;j++) {
ans=max(ans,dp[curr][j]);
}
cout<<ans;
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... | ||||
