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 dp[2005][2005];
struct data
{
int val,cnt;
friend bool operator < (data x,data y)
{
return x.val > y.val;
}
};
int32_t main(){
//freopen("vao.inp","r",stdin);
//freopen("ra.out","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int m,n;
cin >> m >> n;
int n1=n;
vector < vector < data > > vt(m+1);
for (int i=1;i<=n;i++)
{
int v,w,t;
cin >> v >> w >> t;
vt[w].push_back({v,t});
}
int i=0;
for (int w=1;w<=m;w++)
{
i++;
sort(vt[w].begin(),vt[w].end());
int curw=0,curv=0;
for (int j=1;j<=m;j++)
{
dp[i][j]=dp[i-1][j];
}
for (auto f:vt[w])
{
if (curw>m) break;
for (int k=1;k<=f.cnt;k++)
{
curw+=w;
curv+=f.val;
if (curw>m) break;
for (int j=curw;j<=m;j++)
{
dp[i][j]=max(dp[i][j],dp[i-1][j-curw]+curv);
}
}
}
}
int ans=0;
for (int j=1;j<=m;j++) ans=max(ans,dp[i][j]);
cout << ans;
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int32_t main()':
knapsack.cpp:22:9: warning: unused variable 'n1' [-Wunused-variable]
22 | int n1=n;
| ^~
# | 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... |