# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
469494 | Croco | Knapsack (NOI18_knapsack) | C++17 | 114 ms | 35936 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 int
#define ll long long
using namespace std;
const int N = 5e5+5;
vector<pair<int,int>> item[2005];
int dp[2005][2005];
signed main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int s,n;
cin>>s>>n;
for(int i=0;i<n;i++)
{
int val,w,amt;
cin>>val>>w>>amt;
if(w <= s && amt > 0)
item[w].push_back({val,amt});
}
for(int i=0;i<2005;i++)
for(int j=0;j<2005;j++)
dp[i][j] = INT32_MIN;
dp[0][0] = 0;
int ans = 0;
for(int i=1;i<=s;i++)
{
sort(item[i].begin(),item[i].end(),greater<pair<int,int>>());
for(int j=0;j<=s;j++)
{
dp[i][j] = dp[i-1][j];
int copies = 0;
int profit = 0;
int at = 0;
int used = 0;
while((copies + 1)*i <= j && at < item[i].size())
{
copies++;
profit += item[i][at].first;
if(dp[i-1][j - copies*i] != INT32_MIN)
{
dp[i][j] = max(dp[i][j],dp[i-1][j-copies*i] + profit);
ans = max(ans,dp[i][j]);
}
used++;
if(used == item[i][at].second)
{
used = 0;
at++;
}
}
}
}
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... |