# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
926911 | haru09 | Knapsack (NOI18_knapsack) | C++17 | 50 ms | 66456 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>
using namespace std;
#define ll long long
#define fi first
#define se second
#define task "code"
const int ar=2e3+5;
const ll mod=1e9+7;
int n,s;
vector<pair<int,int>> w[ar];
ll dp[ar][ar];
ll sum[ar][ar];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
if (fopen(task".inp","r"))
{
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin>>s>>n;
for (int i=1;i<=n;i++)
{
int v,c,k;
cin>>v>>c>>k;
w[c].push_back({v,k});
}
for (int i=1;i<=s;i++)
{
sort(w[i].begin(),w[i].end(),greater<pair<int,int>>());
sum[i][0]=0;
int j=1;
for (auto [v,k]:w[i])
{
int last=j;
while(i*j<=s && j-last<k)
{
sum[i][i*j]=sum[i][i*(j-1)]+v;
j++;
}
}
}
for (int i=1;i<=s;i++)
{
for (int j=1;j<=s;j++) dp[i][j]=dp[i-1][j];
for (int j=i;j<=s;j+=i)
{
for (int k=j;k<=s;k++)
{
dp[i][k]=max(dp[i][k],dp[i-1][k-j]+sum[i][j]);
}
}
}
ll 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... |