#include <bits/stdc++.h>
#define int long long
using namespace std;
const long long N = 2e5 + 5;
int a[N];
int dp[N]; // stands for maximum sum and weight
struct item
{
int weight, value, quantity;
};
void solve()
{
int n, s;
cin >> s >> n;
vector<item> a(n);
int k = 1;
for (int i = 0; i < n; i++)
{
cin >> a[i].value >> a[i].weight >> a[i].quantity;
a[i].quantity = min(a[i].quantity, s / a[i].weight);
}
sort(a.begin(), a.end(), [&](item a, item b)
{ return a.weight < b.weight; });
int cnt = 0;
vector<item> b;
for (int i = 0; i < n; i++)
{
int k = 1;
while (a[i].quantity)
{
k = min(k, a[i].quantity);
b.push_back({a[i].weight * k, a[i].value * k, 0});
a[i].quantity -= k;
k *= 2;
}
}
for (auto j : b)
{
for (int i = s; i >= j.weight; i--)
{
dp[i] = max(dp[i], dp[i - j.weight] + j.value);
}
}
// for (int i = 0; i < s; i++)
// {
// }
cout << dp[s] << ' ';
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
// cin >> t;
for (int i = 1; i <= t; i++)
{
// cout << "Case #" << i << ':' << ' ';
solve();
cout << endl;
}
return 0;
}
| # | 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... |