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 int long long
int s,n,ans;
pair<int,int> dp[2005][2005][2];
struct item
{
int val,weight,cnt;
};
item a[100005];
vector<int> p[2005];
bool comp(item a, item b)
{
return (a.weight < b.weight or (a.weight == b.weight and a.val > b.val));
}
signed main()
{
cin>>s>>n;
for (int i = 1; i <= n; i++)
{
cin>>a[i].val>>a[i].weight>>a[i].cnt;
}
sort(a+1,a+1+n,comp);
for (int i = 1; i <= n; i++)
{
int w=a[i].weight,k=a[i].cnt;
while (p[w].size() < s and k > 0)
{
p[w].push_back(a[i].val);
k--;
}
}
for (int i = 1; i <= s; i++) for (int j = 1; j <= s; j++) dp[i][j][0].first=dp[i][j][1].first=-1;
for (int i = 1; i <= s; i++)
{
for (int j = 0; j <= s; j++)
{
for (int bl = 0; bl < 2; bl++)
{
if (dp[i][j][bl].first == -1) continue;
ans=max(ans,dp[i][j][bl].first);
int cnt = dp[i][j][bl].second;
if (dp[i+1][j][0].first < dp[i][j][bl].first) dp[i+1][j][0].first=dp[i][j][bl].first;
if (cnt >= p[i].size() or j + i > s) continue;
int nextitem = p[i][cnt];
if (dp[i][j][bl].first + nextitem > dp[i][j + i][1].first) dp[i][j+i][1].first=dp[i][j][bl].first + nextitem, dp[i][j+i][1].second=dp[i][j][bl].second + 1;
}
}
}
// for (int i = 1; i <= s; i++) cout<<p[i].size()<<' ';cout<<endl;
/* for (int i = 1; i <= s; i++)
{
for (int j = 1; j <= s; j++)
{
cout<<'{'<<dp[i][j].first<<','<<dp[i][j].second<<'}';
}
cout<<endl;
}*/
cout<<ans;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:27:22: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
27 | while (p[w].size() < s and k > 0)
| ~~~~~~~~~~~~^~~
knapsack.cpp:45:13: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | if (cnt >= p[i].size() or j + i > s) continue;
| ~~~~^~~~~~~~~~~~~~
# | 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... |