# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
652329 | lam | Knapsack (NOI18_knapsack) | C++14 | 116 ms | 62256 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;
const int maxn = 1e6 + 10;
typedef pair<int,int> ii;
#define ff first
#define ss second
int s,n;
int v[maxn],w[maxn],k[maxn];
vector <ii> a[maxn];
int dp[2010][2010];
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
cin>>s>>n;
for (int i=1; i<=n; i++)
{
cin>>v[i]>>w[i]>>k[i];
a[w[i]].push_back({v[i],k[i]});
}
for (int i=1; i<=s; i++)
{
sort(a[i].begin(),a[i].end(),greater<ii>());
for (int j=1; j<=s; j++)
{
dp[i][j]=dp[i-1][j];
if (a[i].empty()) continue;
int it,cnt,used,val;
it=cnt=val=0;
used=a[i][it].ss;
while (it<(int)(a[i].size()) && (cnt+1)*i <=j )
{
used--;
cnt++;
val+=a[i][it].ff;
dp[i][j]=max(dp[i][j],dp[i-1][j-cnt*i]+val);
if (used==0)
{
it++;
if (it<a[i].size()) used=a[i][it].ss;
}
}
}
}
int ans=0;
for (int i=1; i<=s; i++) ans=max(ans,dp[s][i]); cout<<ans;
}
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... |