#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
struct mit
{
int v,k,w;
bool operator < (const mit &x)
{
return v > x.v;
}
} a[N];
vector <int> v[2005];
int dp[2005];
main()
{
int s,n;
cin >> s >> n;
for(int i=1;i<=n;i++)
{
cin >> a[i].v >> a[i].w >> a[i].k;
a[i].k = min(a[i].k,s/a[i].w);
}
sort(a+1,a+1+n);
for(int i=1;i<=n;i++)
{
while (a[i].k-- && v[a[i].w].size() <= s/a[i].w)
v[a[i].w].push_back(a[i].v);
}
vector <int> W,V;
for(int w=1;w<=s;w++)
{
for(auto x:v[w])
{
W.push_back(w);
V.push_back(x);
}
}
for(int i=0;i<V.size();i++)
for(int j=s;j>=W[i];j--)
{
dp[j] = max(dp[j],dp[j-W[i]] + V[i]);
}
cout << *max_element(dp,dp+1+s);
}
Compilation message (stderr)
knapsack.cpp:15:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
15 | main()
| ^~~~
# | 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... |