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;
int main()
{
long long n, s;
cin >> s >> n;
map<long long, vector<pair<long long, long long>>> items;
for (long long i = 0; i < n; i++)
{
long long a, b, c;
cin >> a >> b >> c;
items[b].push_back({a, c});
}
vector<long long> dp(s + 1, INT64_MIN);
dp[0] = 0;
for (auto p : items)
{
long long w = p.first;
vector<pair<long long, long long>> item = p.second;
sort(item.begin(), item.end(), greater<pair<int, int>>());
for (int i = s; i > 0; i--)
{
long long copies = 0;
long long type = 0;
long long value = 0;
long long curr = 0;
while ((copies + 1) * w <= i && type < item.size())
{
value += item[type].first;
copies++;
curr++;
if (dp[i - copies * w] != INT64_MIN)
{
dp[i] = max(dp[i], dp[i - copies * w] + value);
}
if (curr == item[type].second)
{
curr = 0;
type++;
}
}
}
}
cout << *max_element(dp.begin(), dp.end());
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:34:50: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | while ((copies + 1) * w <= i && type < item.size())
| ~~~~~^~~~~~~~~~~~~
# | 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... |