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 <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#define maxn 2010
//#define int long long
#define taskname "spainting"
using namespace std;
int s,n;
int dp[maxn][maxn];
vector <pair<int,int>> a[maxn];
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
// freopen(taskname".in","r",stdin);
// freopen(taskname".out","w",stdout);
cin>>s>>n;
for (int i=1; i<=n; i++)
{
int v,w,k; cin>>v>>w>>k;
if (w<=s&&k>0) a[w].push_back({v,k});
}
for (int i=0; i<=s; i++) for (int j=0; j<=s; j++) dp[i][j]=-1e18;
dp[0][0]=0;
for (int i=1; i<=s; i++)
{
sort(a[i].begin(),a[i].end(),greater<pair<int,int>>());
for (int j=0; j<=s; j++)
{
dp[i][j]=dp[i-1][j];
if (a[i].empty()) continue;
int cnt=0;
int used=0;
int it=0;
int sum=0;
while ((cnt+1)*i<=j&&it<a[i].size())
{
cnt++;
sum+=a[i][it].first;
if (dp[i-1][j-cnt*i]!=1e18)
dp[i][j]=max(dp[i][j],dp[i-1][j-cnt*i]+sum);
used++;
if (used==a[i][it].second)
{
used=0;
it++;
}
}
}
}
int ans=-1e18;
for (int i=0; i<=s; i++) ans=max(ans,dp[n][i]);
cout<<ans;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:25:64: warning: overflow in conversion from 'double' to 'int' changes value from '-1.0e+18' to '-2147483648' [-Woverflow]
25 | for (int i=0; i<=s; i++) for (int j=0; j<=s; j++) dp[i][j]=-1e18;
| ^~~~~
knapsack.cpp:38:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | while ((cnt+1)*i<=j&&it<a[i].size())
| ~~^~~~~~~~~~~~
knapsack.cpp:53:13: warning: overflow in conversion from 'double' to 'int' changes value from '-1.0e+18' to '-2147483648' [-Woverflow]
53 | int ans=-1e18;
| ^~~~~
# | 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... |